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 December 31, 2025 10:42
Last Day Where You Can Still Cross

Question

Approach

I think of the matrix as getting more and more flooded each day, so instead of checking every day naively, I binary-search on the answer. For any given day d, I temporarily mark the first d cells in cells as water, then I check whether there is still a path of land from the top row to the bottom using BFS (or DFS). I start the search from every land cell in the top row and try to walk only through land. If I can still reach the bottom, then crossing is possible on day d, so I try a later day; if not, I try an earlier day. This way, I efficiently narrow down to the last day when crossing is still possible.

Implementation

from collections import deque
@Ifihan
Ifihan / main.md
Created December 30, 2025 22:38
Magic Squares In Grid

Question

Approach

I slide a 3×3 window over every possible position in the grid and check whether that 3×3 subgrid forms a valid magic square. For each subgrid, I first extract its nine numbers and make sure they are exactly the distinct integers from 1 to 9, since a magic square cannot contain anything else. If that condition fails, I skip it immediately. Otherwise, I compute the sums of its three rows, three columns, and the two diagonals. If all eight of these sums are equal, then the subgrid satisfies the magic square property, so I count it. I repeat this for every valid 3×3 starting position and finally return the total count.

Implementation

class Solution:
    def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created December 29, 2025 21:50
Pyramid Transition Matrix

Question

Approach

I first preprocess the allowed triples to build a mapping from every valid pair of adjacent blocks (like "AB") to all possible blocks that can sit above them. This makes it easy to quickly look up what I’m allowed to place as I build the pyramid. Then I use recursion with backtracking to try to build the pyramid level by level from the given bottom row up to the top. At any point, I keep track of the current row I’m filling from and the partially-built next row. If the current row has length one, it means I successfully built all the way to the top, so I return true. If the next row is already one shorter than the current one, I recursively move up a level and start building the row above it. Otherwise, I look at the next adjacent pair in the current row, find all allowed blocks that can go above that pair, and try each one by appending it to the next row and recursing. If any re

@Ifihan
Ifihan / main.md
Created December 28, 2025 21:55
Count Negative Numbers in a Sorted Matrix

Question

Approach

I take advantage of the fact that the matrix is sorted in non-increasing order both row-wise and column-wise. Instead of checking every value, I start from the bottom-left corner of the grid. If the current value is negative, then I know every element to its right in that row must also be negative, so I add all of them to my count at once and then move one row up. But if the current value is non-negative, then I move one column to the right to look for negatives. I keep doing this until I either run out of rows or columns, and by the end I’ll have counted all negative numbers efficiently.

Implementation

class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created December 27, 2025 20:55
Meeting Rooms III
@Ifihan
Ifihan / main.md
Created December 26, 2025 21:59
Minimum Penalty for a Shop

Question

Approach

To minimize the penalty, I think of each possible closing hour j from 0 to n and compute how many wrong decisions happen: every 'N' before j adds 1 to the penalty because the shop stayed open for no reason, and every 'Y' from j onward adds 1 because the shop was closed when customers came. Instead of recomputing this every time, I track the penalty as I move j from left to right. I start with the penalty equal to the total number of 'Y' characters (meaning: if I closed at hour 0, I miss all customers). Then, for each hour i, if the character is 'Y', moving the closing time past this hour reduces the penalty by 1 (because this customer is now served). If it is 'N', the penalty increases by 1 (because now we are open during a no-customer hour). As I sweep through the string, I keep track of the minimum penalty seen and the earliest hour where it happens. The answer is that earli

@Ifihan
Ifihan / main.md
Created December 25, 2025 22:01
Maximize Happiness of Selected Children

Question

Approach

To solve this, I first sort the happiness array in descending order so that I always consider the children with the highest happiness values first. Since every time I pick a child the happiness of all remaining unpicked children decreases by 1 (but never below 0), the child I pick on the i-th turn (0-indexed) effectively contributes happiness[i] - i to the total. I then iterate through the first k children in the sorted list, reduce each value by its pick index, and add only the positive results to the total (because happiness cannot go negative, haha).

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created December 24, 2025 21:15
Apple Redistribution into Boxes

Question

Approach

First, I compute the total number of apples across all packs. To minimize the number of boxes, I should always pick the largest-capacity boxes first, because bigger boxes cover more apples with fewer boxes.

So I:

  • Sum all apples → need
  • Sort capacity in descending order
  • Keep adding box capacities until the running sum ≥ need
@Ifihan
Ifihan / main.md
Created December 23, 2025 22:25
Two Best Non-Overlapping Events

Question

Approach

I sort the events by their start time. As I iterate through events in increasing start order, I want to know the maximum value of any event that has already ended before the current event starts.

To do this efficiently:

  • I keep another list of events sorted by end time.
  • I maintain a pointer over the end-sorted list and a running maximum best_so_far of values for all events whose end < current_start.
  • For each event, I compute:
@Ifihan
Ifihan / main.md
Created December 22, 2025 22:04
Delete Columns to Make Sorted III