awelgorithms

Decide values vs indices vs pairs

Updated

Values: when only the top value matters (min stack of values, collision magnitudes). Indices: when you must assign into answer[i] or compute distance j-i. Pairs: when two facts must pop together (value+min, frag+count, node+phase).

What to focus on

Wrong choice: values for next-greater (lost i), indices when you only needed a running min (overkill), two parallel stacks you forget to pop together (use a pair).

Worked examples

values

a = [4, 1, 9], getMin with pairs of values
  1. min is a value

Result: pairs (v,min)

indices

a = [2, 1, 3], next greater
  1. need i

Result: stack of i

pairs

s = "3[ab]"
  1. frag and count

Result: (frag, count)

Try it

Write chooseItem(kind). kind is "getMin", "nextGreater", or "decode". Return "values", "indices", or "pairs". Say what breaks if you pick values for next-greater. a = [4, 1, 9], getMin with pairs of values → pairs (v,min). a = [2, 1, 3], next greater → stack of i. s = \"3[ab]\" → (frag, count).

a is non-empty. s is a well-formed decode string. Choose the item type.