Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-206

Reference Counting Increment

Problem

Simulate reference counting on object ids.

operations contains "add_ref" (increment the count of targets[i]) or "release" (decrement it, never below zero). After every operation, record the current reference count of that same target.

Return the list of counts.

Hint

Releasing an object with count zero must stay at zero.

Test cases

case 1
in: [["add_ref","release"],[7,7]]
expected: [1,0]
case 2
in: [["release"],[3]]
expected: [0]
case 3
in: [["add_ref","add_ref","release","release","release"],[2,2,2,2,2]]
expected: [1,2,1,0,0]
case 4
in: [[],[]]
expected: []
Starter codePython
def refcount_ops(operations, targets):
    # Your code here
    pass

4 test cases · run your code against them in the editor