I consider a pair of points
class Solution: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]]:I iterate through each rectangle and compare diagonals without using square roots by computing d2 = ll + ww (the squared diagonal). I track the best squared diagonal seen so far and, on ties, I pick the rectangle with the larger area l * w. At the end, I return the area corresponding to the rectangle with the longest (squared) diagonal, breaking ties by maximum area.
class Solution:
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:I traverse the matrix in a single pass while zig-zagging along diagonals. I keep a current position (r, c) and a direction flag dir (+1 for moving up-right, -1 for down-left). At each step I write mat[r][c] to the result, then move according to the direction. When I hit a boundary (top row, bottom row, leftmost, or rightmost column), I “bounce” by adjusting (r, c) to the next valid start cell and flipping the direction. This visits each cell exactly once without extra storage for diagonals.
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:I use a sliding window that allows at most one zero inside it—because I can delete one element. I expand the right pointer over the array, counting zeros in the current window. If the zero count exceeds one, I move the left pointer forward and decrement the zero count when I pass a zero. For each step I record the window length; since I must delete one element, the best achievable all-ones subarray length for the current window is window_len - 1. Taking the maximum over all windows gives the answer. This naturally handles edge cases like “all ones” (I must delete one, so length reduces by 1) and “all zeros” (answer becomes 0).
class Solution:
def longestSubarray(self, nums: List[int]) -> int: