I noted that applying one operation to a number x
replaces it with ⌊x/4⌋
, so the number of operations needed to turn a single x
into 0
is exactly the number of base-4 digits of x
, i.e., steps(x) = 1 + ⌊log₄ x⌋
(intervals: 1..3→1
, 4..15→2
, 16..63→3
, …). Since each operation acts on two numbers at once, the minimum operations for an interval [l, r]
are constrained by (i) the total step demand S = Σ steps(x)
and (ii) the largest per-item demand M = max steps(x)
. Each operation can reduce the remaining total by at most 2
(one step per chosen item), but a single item still needs at least M
operations applied to it across time. Therefore, the optimal number of operations for a query is max(M, ceil(S/2))
. Over many queries, I precompute powers of 4 and the prefix sums of steps(x)
grouped by `[4^{t-1}, 4
The key idea is to model each operation as subtracting num1
. After
class Solution:
I compute the absolute distances from each moving person to Person 3: dx = |x - z| and dy = |y - z|. Since both move at the same speed, the one with the smaller distance arrives first. If dx < dy, I return 1; if dy < dx, I return 2; otherwise, they arrive simultaneously and I return 0.
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
I consider a pair of points
class Solution:
To maximize the average pass ratio, I approached the problem using a greedy strategy. The key idea is that each time I assign a brilliant student to a class, the benefit to the average pass ratio depends on how much that student increases the pass ratio of the chosen class. For a class with p
passing students out of t
total, the gain from adding one more passing student is (p+1)/(t+1) - p/t
. This value decreases as more students are added to the same class, so it’s optimal to always assign the next student to the class with the current largest gain. To implement this efficiently, I used a max-heap (priority queue) where each entry stores the negative of the gain (since Python’s heapq
is a min-heap) along with the class data. I repeatedly popped the class with the best gain, updated its values by adding one student, recomputed its gain, and pushed it back. After all extra students
I solve Sudoku with backtracking accelerated by bitmasks and a “minimum remaining values” (MRV) heuristic. I keep three bitmasks for rows, columns, and 3×3 boxes to record which digits are already used. For any empty cell, the set of candidates is the complement of the union of its row/col/box masks. On each step, I pick the empty cell with the fewest candidates (MRV) to prune the search aggressively, try each candidate (set bits), update masks, recurse, and backtrack on failure. Since the puzzle is guaranteed to have a unique solution, the search terminates quickly.
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
I scan the 9×9 board once and validate three constraints simultaneously. I keep three collections: one for rows, one for columns, and one for the 3×3 sub-boxes. For each filled cell (r, c) with digit d, I check if d already exists in row[r], col[c], or box[r//3][c//3]. If it does, the board is invalid; otherwise I insert d into all three.
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
I noticed each move removes exactly one flower from either lane, so the total number of moves is x + y. Since I move first, I win iff the total number of moves is odd (I take moves 1,3,5,…,x+y). Therefore, I just need to count pairs (x, y) with opposite parity. Let oddN = (n+1)//2, evenN = n//2, and similarly for m. The answer is oddN * evenM + evenN * oddM.
class Solution:
def flowerGame(self, n: int, m: int) -> int:
I treat each top-left–to–bottom-right diagonal as a group keyed by i - j. For every offset d = i - j, the whole diagonal lies either on/below the main diagonal (d ≥ 0) or above it (d < 0). So I collect each diagonal: if d ≥ 0 (bottom-left, including main), I sort it in non-increasing order; if d < 0 (top-right), I sort it in non-decreasing order. Then I write the sorted values back along the same diagonal. This processes all diagonals independently and satisfies both ordering rules.
class Solution:
def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]: