A buffer with a contract

Buffer [9,4,9,7,4], discard 9, preserve survivor order, reuse storage.

What is specified after the call? Which cells may be ignored?

1 / 11

Derive the safe write

Copying survivors gives a simple specification.

Repeatedly shifting after deletion moves the same values many times.

Remember only the next accepted-output slot. Why can it never pass the read position?

2 / 11

Read/write compaction

Before reading r: nums[:w] is the stable survivor sequence from the original prefix.

w <= r protects unread values.

Return w; the tail is unspecified.

3 / 11

Merge from the spare end

[2,4,0,0] + [1,3]
write 4 -> [2,4,0,4]
write 3 -> [2,4,3,4]
write 2 -> [2,2,3,4]
write 1 -> [1,2,3,4]
4 / 11

The overwrite proof

w = i + j + 1

While j >= 0, w > i. The destination is strictly after unread nums1.

When j < 0, w = i: the untouched prefix is already correct.

5 / 11

Rotation is a block identity

AB -> reverse(B) reverse(A) -> B reverse(A) -> BA

Normalize k modulo n only after handling n=0.

6 / 11

Reachable is not the same as large

nums = [1,0,100]
farthest after index 1 = 1
index 2 is unreachable

Check reachability before using a jump.

7 / 11

Minimum jumps are BFS layers

[2,3,1,1,4]
0 jumps: end=0
1 jump : end=2
2 jumps: end=4

currentEnd is committed; farthest is under construction.

8 / 11

Cancellation and decomposition

Majority: unequal votes cancel, under a strict-majority promise.

Unlimited stock trades: profits telescope into adjacent differences, only without transaction costs.

9 / 11

Gas Station: existence is not uniqueness

gas=[2,2], cost=[1,1]

Both starts work.

A negative candidate prefix discards a block of starts; a nonnegative total guarantees some start.

10 / 11

Reconstruct, then check

Keep at most three duplicates: compare with which accepted position?

Product of [2,0,4] except self: which entry can be nonzero?

Finish with the module quiz.

11 / 11
←/→ or click edges to navigate · ? help · N notes · F fullscreen