One Pass
Looking for Missing Number (268)? Its one-pass solution is an XOR fold, so it lives with the rest of the XOR technique in Bit Manipulation / XOR.
Running Extremes
Second Largest
Find the second largest element in a single scan, without sorting and without a second pass. We carry two running maxima and update them in a cascade as larger values appear.
Scan:
- For each value, if it beats the current
first_max, the oldfirst_maxcascades down intosecond_maxand the new value becomesfirst_max. - Otherwise, if it sits strictly between
second_maxandfirst_max, it becomes the newsecond_max. The strict checks (first_max > i and second_max < i) skip duplicates of the maximum so they never pollute the runner-up.
Result:
second_maxstays-1when there is no valid runner-up (for example, when every element is identical).
- Time
- O(N)
Nis the number of elements in the array. Each element is compared a constant number of times.- Space
- O(1)
- Only the two running maxima are stored.
1796. Second Largest Digit in a String
The same two-running-maxima idea as Second Largest, specialised to the digit characters embedded in a string. Non-digit characters are simply skipped, and the comparison happens directly on the character codes (which order correctly for the digits '0'-'9').
Scan:
- Skip any character that is not a digit with
continue. - On a larger digit, cascade the old
first_maxdown intosecond_maxand store the new one. - Otherwise, if the digit lands strictly between the two maxima, it becomes the new
second_max.
Result:
- The sentinel
"-"marks "no digit yet". Ifsecond_maxnever advances past it, the answer is-1; otherwise the surviving digit character is converted back to anint.
- Time
- O(n)
n = len(s). Thefor i in sloop inspects each character once.- Space
- O(1)
- Only the two running maxima,
first_maxandsecond_max, are stored.
Greedy Scan
2259. Remove Digit From Number to Maximize Result
We must delete exactly one occurrence of digit to leave the largest possible number. Removing a digit shifts everything after it one place left, so the result grows most when the digit we drop is immediately followed by a larger digit.
Greedy scan:
- Walk left to right. At each occurrence of
digit, peek at the next characternumber[i + 1]. - If that next character is larger, deleting here promotes a bigger digit into this position - that is provably optimal, so return immediately.
- Otherwise remember this index in
last_occ; a later occurrence might still pay off.
Fallback:
- If no occurrence had a larger successor, the best move is to delete the last occurrence (including the final character, handled after the loop), since that disturbs the highest-value prefix the least.
- Time
- O(N)
Nis the number of digits. A single left-to-right scan finds the deletion point.- Space
- O(N)
- The returned string slices allocate a new string of length
N - 1.
605. Can Place Flowers
Flowers can't be adjacent, so a 0 is plantable only when both neighbors are also empty (or off the edge). Planting there the moment we see it is always safe: it never blocks a future placement, since the next possible slot is at least two cells away either way.
Greedy scan:
- Walk left to right. At each
0, checkflowerbed[i - 1]andflowerbed[i + 1](treating out-of-bounds as empty). - If both sides are empty, plant here (
flowerbed[i] = 1) and decrementn. - Stop early once
n <= 0.
- Time
- O(size)
- The
for i in range(size)loop runs unconditionally to the end (it never breaks early even aftern <= 0), doingO(1)work per cell, so total isO(size), wheresize = len(flowerbed). - Space
- O(1)
- Planting happens in place on the input array; no extra structures are allocated.
Neighbor Lookup
Neither a window nor a running extreme - each position's answer depends only on the value sitting right next to it, so a single pass that reads one neighbor per index is enough, whichever direction it walks.
1844. Replace All Digits with Characters
s always has odd length with a letter at every even index and a digit at every odd index, so the last character is always a letter with nothing after it - ans.append(s[-1]) handles it up front, and n is dropped by one to exclude it from the loop. Walking i backward two at a time then lands on exactly the digit positions: s[i] is the shift amount, s[i-1] is the letter it applies to, and shifted_char is that letter advanced shift places with %26 wraparound. Both the untouched letter and its shifted neighbor are pushed onto the front of ans together, in that order, so each pair lands in the deque exactly where it sat in s.
- Time
- O(2n + 26)
- The backward pairwise scan and the final
"".join(ans)are each a separateO(n)pass -2ncombined, wheren = len(s). alphabet_mapis built once from the 26-character alphabet,O(26), independent ofn.- Space
- O(n + 26)
ansholds allncharacters before the join.alphabet_mapholds exactly the 26 letters of the alphabet,O(26), independent ofn.