Skip to content

Practice Counter II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2665
LeetCode Problem

Counter II

Problem Statement

createCounter(init) returns an object holding a current value initially equal to init. increment() adds 1 and returns the new value. decrement() subtracts 1 and returns the new value. reset() restores init and returns it.

Source: Counter II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
init = 5, calls = ["increment","reset","decrement"]
Output
[6,5,4]

# Example 2

Input
init = 0, calls = ["increment","increment","decrement","reset","reset"]
Output
[1,2,1,0,0]

Constraints

-1000 <= init <= 1000
0 <= calls.length <= 1000
calls[i] is one of "increment", "decrement" and "reset"

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.