Skip to content

Commit f149b90

Browse files
committed
LC 2665. Counter II (JS)
1 parent 00660b0 commit f149b90

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
474474
| [2244. Minimum Rounds to Complete All Tasks][lc2244] | 🟠 Medium | [![python](res/py.png)][lc2244py] |
475475
| [2246. Longest Path With Different Adjacent Characters][lc2246] | 🔴 Hard | [![python](res/py.png)][lc2246py] |
476476
| [2256. Minimum Average Difference][lc2256] | 🟠 Medium | [![python](res/py.png)][lc2256py] |
477+
| [2665. Counter II][lc2265] | 🟢 Easy | [![python](res/py.png)][lc2265py] |
477478
| [2279. Maximum Bags With Full Capacity of Rocks][lc2279] | 🟠 Medium | [![python](res/py.png)][lc2279py] |
478479
| [2300. Successful Pairs of Spells and Potions][lc2300] | 🟠 Medium | [![python](res/py.png)][lc2300py] [![rust](res/rs.png)][lc2300rs] |
479480
| [2306. Naming a Company][lc2306] | 🔴 Hard | [![python](res/py.png)][lc2306py] [![rust](res/rs.png)][lc2306rs] |
@@ -1510,6 +1511,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
15101511
[lc2246py]: leetcode/longest-path-with-different-adjacent-characters.py
15111512
[lc2256]: https://leetcode.com/problems/minimum-average-difference/
15121513
[lc2256py]: leetcode/minimum-average-difference.py
1514+
[lc2265]: https://leetcode.com/problems/counter-ii/
1515+
[lc2265py]: leetcode/counter-ii.js
15131516
[lc2279]: https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/
15141517
[lc2279py]: leetcode/maximum-bags-with-full-capacity-of-rocks.py
15151518
[lc2300]: https://leetcode.com/problems/successful-pairs-of-spells-and-potions/

leetcode/counter-ii.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 2665. Counter II
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/counter-ii/
5+
//
6+
// Tags: Javascript
7+
8+
// Use the parameter as state inside the closure and add another variable
9+
// initialized with the same value, return an object with the three functions
10+
// that the problem asks for.
11+
//
12+
// Time complexity: O(1)
13+
// Space complexity: O(1)
14+
//
15+
// Runtime 69 ms Beats 46.34%
16+
// Memory 44.9 MB Beats 40.38%
17+
/**
18+
* @param {integer} init
19+
* @return { increment: Function, decrement: Function, reset: Function }
20+
*/
21+
var createCounter = function (init) {
22+
let n = init;
23+
return {
24+
increment: () => ++n,
25+
decrement: () => --n,
26+
reset: () => (n = init),
27+
};
28+
};
29+
30+
/**
31+
* const counter = createCounter(5)
32+
* counter.increment(); // 6
33+
* counter.reset(); // 5
34+
* counter.decrement(); // 4
35+
*/

0 commit comments

Comments
 (0)