Editorial way!
I scan the grid once and track the bounding box of all 1s. Specifically, I keep the minimum and maximum row and column indices where I see a 1. After the scan, the smallest axis-aligned rectangle containing all 1s has height max_row - min_row + 1 and width max_col - min_col + 1. The area is their product.
class Solution:
def minimumArea(self, grid: List[List[int]]) -> int:I treat each row as the base of a histogram of consecutive 1s above it. For each row, I update a heights array where heights[j] is the number of continuous 1s ending at this row in column j. Then, I count how many all-ones submatrices have their bottom edge at this row using a monotonic non-decreasing stack. While scanning columns left to right, I keep pairs (height, count) on the stack and maintain a running sum cur of submatrices ending at the current column. When I see a smaller height, I pop taller bars, subtract their contribution from cur, merge counts, push the new bar, and add its contribution back. Summing cur over all columns gives the number of valid submatrices with bottom at this row; accumulating over rows yields the total.
class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:For each cell (i, j) in the matrix, I asked: what’s the size of the largest square of all ones ending at this cell (bottom-right corner)? If matrix[i][j] == 1, then the size is 1 + min(top, left, top-left) where top, left, and top-left are the DP values of the cells directly above, directly to the left, and diagonally up-left. Otherwise, it’s 0. Each DP value represents the number of squares ending at that cell, so by summing them all, I get the total count of square submatrices filled with ones.
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:I scan the array once and keep a running length of the current zero streak. Each time I see a zero, I extend the streak (run += 1) and add it to the answer because every new zero at the end creates exactly run new zero-filled subarrays ending here. If I see a non-zero, I reset the streak to 0. This counts all zero-only subarrays in linear time.
class Solution:
def zeroFilledSubarray(self, nums: List[int]) -> int:I solve this with recursive backtracking. I treat the multiset of current numbers as a state; at each step I pick two numbers a and b, replace them with the result of one of the binary operations (a+b, a-b, b-a, ab, a/b if b≠0, b/a if a≠0), and recurse on the new list. When the list size reduces to 1, I check if it’s within an epsilon (e.g., 1e-6) of 24. To reduce duplicate work, I skip commutative duplicates by only trying a+b and ab once per unordered pair, and I use floats to handle real division. Because there are only 4 cards, this exhaustive search with small pruning is fast enough.
class Solution:
def judgePoint24(self, cards: List[int]) -> bool:I approached the New 21 Game problem using dynamic programming with a sliding window technique. I started by defining dp[i] as the probability of having exactly i points. Since Alice only continues drawing while her score is less than k, I knew only those states could generate new outcomes. For each i, the probability depends on the average of the previous maxPts probabilities, but only from states less than k. To avoid recalculating sums repeatedly, I maintained a running sliding window of these valid probabilities: I added the most recent one and removed the one that just went out of range. The result is then the sum of probabilities of terminal scores between k and n. Finally, I handled special cases: if k is 0 or if n is large enough (n >= k - 1 + maxPts), the probability is guaranteed to be 1.0.
class Solution:Since I’m only allowed to change at most one digit, the optimal strategy is simple: I scan the number from left to right and change the first 6 I encounter into a 9. This guarantees the largest possible number because the leftmost digits contribute more to the value. If no 6 exists, I just return the number unchanged.
class Solution:
def maximum69Number (self, num: int) -> int:To check if n is a power of four, I first ensure that n is positive. Then, I use the property that a power of four is also a power of two, but with its single 1 bit in an odd position (0-indexed). The bitwise check (n & (n-1)) == 0 ensures it’s a power of two. To verify that the 1 bit is in an odd position, I use a mask: 0x55555555 (binary 01010101...), which only has 1s in odd bit positions. If n & 0x55555555 != 0, then n is a power of four.
I scanned through the string num looking at every substring of length 3. For each substring, I checked if all three characters are the same by comparing it to its first character repeated three times. If it is valid, I kept track of the maximum such substring using lexicographic comparison (since digit characters compare in numeric order). At the end, I returned the largest found substring or "" if none was found.
class Solution:
def largestGoodInteger(self, num: str) -> str: