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 November 11, 2025 18:09
Ones and Zeroes

Question

Approach

I treat this as a 0/1 knapsack with two capacity dimensions: number of zeros m and number of ones n. For each string I count how many zeros and ones it needs. I maintain a 2D DP table dp[i][j] = maximum number of strings selectable using at most i zeros and j ones. For each string I update the DP in reverse (from m down to zeros and n down to ones) so each string is used at most once. The answer is dp[m][n].

Implementation

class Solution:
 def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
@Ifihan
Ifihan / main.md
Created November 11, 2025 18:01
Minimum Operations to Convert All Elements to Zero

Question

Approach

I process values from largest to smallest and maintain which indices are currently active (having value ≥ current threshold) using a DSU (union–find) over indices. When I add all indices whose value equals v, I activate them and union with active neighbors so the DSU components represent contiguous segments of positions with value ≥ v. All indices with value v that end up in the same DSU root can be zeroed with one operation (choose that whole component). So for each value v I add the number of distinct DSU roots among the newly added indices to the answer. Summing over values yields the minimum number of operations.

Implementation

from typing import List, Dict
@Ifihan
Ifihan / main.md
Created November 9, 2025 22:17
Count Operations to Obtain Zero

Question

Approach

I keep performing the subtraction operation until either number becomes zero. In each step, I subtract the smaller number from the larger one and count how many operations it takes. Since this process is similar to finding the GCD using subtraction, it ends when one number hits zero.

Implementation

class Solution:
 def countOperations(self, num1: int, num2: int) -> int:
@Ifihan
Ifihan / main.md
Created November 8, 2025 21:05
Minimum One Bit Operations to Make Integers Zero
@Ifihan
Ifihan / main.md
Created November 7, 2025 22:58
Maximize the Minimum Powered City
@Ifihan
Ifihan / main.md
Created November 6, 2025 22:04
Power Grid Maintenance

Question

Approach

I first use a Disjoint Set Union (Union-Find) to determine the connected component (power grid) of each station. For every component root I build a min-heap of all station ids in that component (initially all stations are online). I keep an online boolean array. When a station goes offline I simply mark online[id] = False. For a query [1, x]:

  • if x is online I return x,
  • otherwise I look up the heap of x's component and pop offline ids from the top until the heap is empty or the top is an online station; if the heap is empty I return -1, else I return the heap top (the smallest online id in that component).
@Ifihan
Ifihan / main.md
Created November 5, 2025 22:39
Find X-Sum of All K-Long Subarrays II
@Ifihan
Ifihan / main.md
Created November 4, 2025 22:58
Find X-Sum of All K-Long Subarrays I

Question

Approach

I slide a window of length k over nums. For each window I count frequencies of values (values are bounded <= 50), build pairs (freq, value) for values present in the window, sort those pairs by frequency descending and value descending (so ties favor the larger value), then keep only the top x pairs and sum freq * value for those pairs. If the window has fewer than x distinct values, the x-sum is simply the sum of the entire window.

Implementation

class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
@Ifihan
Ifihan / main.md
Created November 3, 2025 22:54
Minimum Time to Make Rope Colorful

Question

Approach

I sweep the string left to right and treat each run of consecutive same-colored balloons as a group. To avoid adjacent equal colors I must remove all but one balloon in each group; the minimum cost for a group is the sum of its removal times minus the maximum time in that group. Equivalently, while scanning, whenever the current balloon has the same color as the previous one, I remove the cheaper of the two (add min(prev_time, cur_time) to the answer) and keep the larger time as the survivor for further comparisons.

Implementation

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created November 2, 2025 22:32
Count Unguarded Cells in the Grid

Question

Approach

I build a 2D grid representation where I mark walls and guards, then for each guard I scan outward in the four cardinal directions until I hit a wall or another guard, marking every empty cell I pass as guarded. Finally I count grid cells that are still empty and unguarded. This works because the product m * n ≤ 1e5, so an explicit grid and linear scans are efficient.

Implementation

class Solution:
    def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int: