Skip to content

Instantly share code, notes, and snippets.

View Ifihan's full-sized avatar
🔧
Work in Progress

Ifihanagbara Olusheye Ifihan

🔧
Work in Progress
View GitHub Profile
@Ifihan
Ifihan / main.md
Created September 6, 2025 21:43
Minimum Operations to Make Array Elements Zero

Question

Approach

Approach (concise, first person)

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

@Ifihan
Ifihan / main.md
Created September 5, 2025 22:39
Minimum Operations to Make the Integer Zero

Question

Approach

The key idea is to model each operation as subtracting $2^i + \text{num2}$ from num1. After $k$ operations, this means we subtract $k \cdot \text{num2} + S$, where $S$ is the sum of $k$ powers of two (with repetition allowed). Rearranging gives $S = \text{num1} - k \cdot \text{num2}$. For a given $k$, this $S$ must be non-negative and representable as a sum of exactly $k$ powers of two. The smallest number of powers of two needed for $S$ is its binary popcount, and the maximum number is $S$ itself (all ones). So the condition is $\text{popcount}(S) \leq k \leq S$. I loop over $k$ from 1 up to 60 (enough since $2^{60}$ is larger than the constraints), compute $S$, and return the first valid $k$. If no such $k$ works, the answer is $-1$.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created September 4, 2025 21:45
Find Closest Person

Question

Approach

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.

Implementation

class Solution:
 def findClosest(self, x: int, y: int, z: int) -&gt; int:
@Ifihan
Ifihan / main.md
Last active September 4, 2025 21:44
@Ifihan
Ifihan / main.md
Created September 2, 2025 21:46
Find the Number of Ways to Place People I

Question

Approach

I consider a pair of points $(A, B)$ valid if $A$ lies to the upper-left of $B$, which means the $x$-coordinate of $A$ is less than or equal to that of $B$, and the $y$-coordinate of $A$ is greater than or equal to that of $B$. Equality on one axis is allowed, so cases where the two points form a straight vertical or horizontal line are also valid. Once such a pair is identified, I then check that no other point lies inside or on the border of the rectangle defined by $A$ and $B$ as opposite corners. In practice, this means for each candidate pair $(i, j)$, I scan all other points $k \neq i, j$ and verify that none of them satisfy the condition $x_i \leq x_k \leq x_j$ and $y_j \leq y_k \leq y_i$.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created September 1, 2025 19:37
Maximum Average Pass Ratio

Question

Approach

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

@Ifihan
Ifihan / main.md
Created August 31, 2025 22:21
Sudoku Solver

Question

Approach

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.

Implementation

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
@Ifihan
Ifihan / main.md
Created August 30, 2025 22:36
Valid Sudoku

Question

Approach

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.

Implementation

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
@Ifihan
Ifihan / main.md
Created August 29, 2025 22:56
Alice and Bob Playing Flower Game

Question

Approach

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.

Implementation

class Solution:
 def flowerGame(self, n: int, m: int) -&gt; int:
@Ifihan
Ifihan / main.md
Created August 28, 2025 21:26
Sort Matrix by Diagonals

Question

Approach

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.

Implementation

class Solution:
    def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]: