Difficulty: Easy | Acceptance: 81.20% | Paid: No Topics: N/A
Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.
The three functions are:
-
increment() increments the current value by 1 and then returns it.
-
decrement() decrements the current value by 1 and then returns it.
-
reset() resets the current value to init and then returns it.
-
Examples
-
Constraints
Examples
Example 1
Input: init = 5, calls = ["increment","reset","decrement"]
Output: [6,5,4]
Explanation:
const counter = createCounter(5);
counter.increment(); // 6
counter.reset(); // 5
counter.decrement(); // 4
Example 2
Input: init = 0, calls = ["increment","decrement","increment","decrement","reset","reset","decrement"]
Output: [1,-1,1,-1,0,0,-1]
Explanation:
const counter = createCounter(0);
counter.increment(); // 1
counter.decrement(); // -1
counter.increment(); // 1
counter.decrement(); // -1
counter.reset(); // 0
counter.reset(); // 0
counter.decrement(); // -1
Constraints
-1000 <= init <= 1000
0 <= calls.length <= 1000
calls[i] is one of "increment", "decrement", and "reset"
Closure Approach
Intuition Use a closure to maintain the current counter state, allowing the returned functions to access and modify the private variable.
Steps
- Store the initial value and current value in the closure scope
- Return an object with three functions that modify and return the current value
- Each function has access to the private current variable through closure
def createCounter(init: int):
current = init
def increment():
nonlocal current
current += 1
return current
def decrement():
nonlocal current
current -= 1
return current
def reset():
nonlocal current
current = init
return current
return {"increment": increment, "decrement": decrement, "reset": reset}Complexity
- Time: O(1) for each operation
- Space: O(1) for storing the counter state
- Notes: Most idiomatic approach for JavaScript/TypeScript, uses closure to maintain private state
Class-based Approach
Intuition Use a class to encapsulate the counter state and methods, providing a clean object-oriented solution.
Steps
- Create a class with init and current as instance variables
- Implement increment, decrement, and reset as instance methods
- Return an instance of the class with the required methods
class Counter:
def __init__(self, init: int):
self.init = init
self.current = init
def increment(self) -> int:
self.current += 1
return self.current
def decrement(self) -> int:
self.current -= 1
return self.current
def reset(self) -> int:
self.current = self.init
return self.current
def createCounter(init: int):
counter = Counter(init)
return {
"increment": counter.increment,
"decrement": counter.decrement,
"reset": counter.reset
}Complexity
- Time: O(1) for each operation
- Space: O(1) for storing the counter state
- Notes: Clean OOP approach, useful when you need multiple counter instances
Object with Internal State
Intuition Use a simple object with properties to store state, directly accessing and modifying the current value.
Steps
- Create an object with init and current properties
- Attach methods that directly modify the current property
- Return the object with all methods
def createCounter(init: int):
class CounterDict(dict):
def __init__(self, init):
super().__init__()
self["init"] = init
self["current"] = init
def increment(self):
self["current"] += 1
return self["current"]
def decrement(self):
self["current"] -= 1
return self["current"]
def reset(self):
self["current"] = self["init"]
return self["current"]
counter = CounterDict(init)
return {
"increment": counter.increment,
"decrement": counter.decrement,
"reset": counter.reset
}Complexity
- Time: O(1) for each operation
- Space: O(1) for storing the counter state
- Notes: Simple and straightforward, but exposes internal state in some implementations