Skip to main content

One Pass

Running Extremes​

Second Largest​

Easy·
FIG. SECOND LARGEST● INTERACTIVE
visualization loads as you reach it
class Solution:
def getSecondLargest(self, arr):
first_max, second_max = -1, -1
for i in arr:
if first_max < i:
second_max = first_max
first_max = i
elif first_max > i and second_max < i:
second_max = i
return second_max

1796. Second Largest Digit in a String​

Easy·
FIG. 1796 SECOND LARGEST DIGIT IN A STRING● INTERACTIVE
visualization loads as you reach it
class Solution:
def secondHighest(self, s: str) -> int:
first_max = second_max = "-"
for i in s:
if not i.isdigit():
continue
if first_max < i:
second_max = first_max
first_max = i
elif first_max > i and second_max < i:
second_max = i
 
# print(first_max, second_max)
return int(second_max) if second_max != "-" else -1

Greedy Scan​

2259. Remove Digit From Number to Maximize Result​

Easy·
FIG. 2259 REMOVE DIGIT FROM NUMBER TO MAXIMIZE RESULT● INTERACTIVE
visualization loads as you reach it
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
n = len(number)
last_occ = 0
for i in range(n - 1):
if number[i] == digit:
if number[i + 1] > digit:
return number[:i] + number[i + 1 :]
last_occ = i
if number[-1] == digit:
last_occ = n - 1
return number[:last_occ] + number[last_occ + 1 :]

605. Can Place Flowers​

Easy·
FIG. 605 CAN PLACE FLOWERS● INTERACTIVE
visualization loads as you reach it
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
size = len(flowerbed)
for i in range(size):
if flowerbed[i] == 0:
empty_left = (i == 0) or (flowerbed[i - 1] == 0)
empty_right = (i == size - 1) or (flowerbed[i + 1] == 0)
if empty_right and empty_left:
flowerbed[i] = 1
n -= 1
return n <= 0

1827. Minimum Operations to Make the Array Increasing​

Easy·
FIG. 1827 MINIMUM OPERATIONS TO MAKE THE ARRAY INCREASING● INTERACTIVE
visualization loads as you reach it
class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
for i in range(1, len(nums)):
if nums[i] <= nums[i - 1]:
diff = nums[i - 1] - nums[i] + 1
ans += diff
nums[i] += diff
return ans

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​

Easy·
2 Approaches
FIG. 1844 REPLACE ALL DIGITS WITH CHARACTERS BACKWARD● INTERACTIVE
visualization loads as you reach it
class Solution:
def replaceDigits(self, s: str) -> str:
n = len(s)
ans = collections.deque()
alphabets = "abcdefghijklmnopqrstuvwxyz"
alphabet_map = {j: i for i, j in enumerate(alphabets)}
if n % 2:
ans.append(s[-1])
n -= 1
for i in range(n - 1, -1, -2):
# print(i)
shift = int(s[i])
char = s[i - 1]
shifted_char = alphabets[(alphabet_map[char] + shift) % 26]
ans.appendleft(shifted_char)
ans.appendleft(char)
return "".join(ans)

Index Arithmetic​

The positions that matter are computable from the loop counter, so the scan can skip straight to them instead of testing every element on the way past.

1572. Matrix Diagonal Sum​

Easy·
2 Approaches
FIG. 1572 DIAGONAL SUM SCAN● INTERACTIVE
visualization loads as you reach it
class Solution:
def diagonalSum(self, mat: list[list[int]]) -> int:
total = 0
n = len(mat)
for i in range(n):
for j in range(n):
if i == j or j == n - i - 1:
total += mat[i][j]
return total

2319. Check if Matrix Is X-Matrix​

Easy·
FIG. 2319 CHECK X MATRIX● INTERACTIVE
visualization loads as you reach it
class Solution:
def checkXMatrix(self, grid: list[list[int]]) -> bool:
n = len(grid)
isDiagonal = lambda i, j: i == j or j == n - i - 1
for i in range(n):
for j in range(n):
if isDiagonal(i, j) and grid[i][j] == 0:
return False
if not isDiagonal(i, j) and grid[i][j] != 0:
return False
return True

Running Total​

The pass accumulates one number instead of comparing positions - what the array should total, minus what it actually totals, is the answer.

268. Missing Number​

Easy·
4 Approaches · 3 patterns
class Solution:
def missingNumber(self, arr):
n = len(arr) + 1
actual_sum = n * (n + 1) // 2
total = sum(arr)
return actual_sum - total