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 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:
@Ifihan
Ifihan / main.md
Created November 1, 2025 22:18
Delete Nodes From Linked List Present in Array

Question

Approach

I first convert nums into a set for O(1) lookups. Then, I use a dummy node to simplify edge cases like when the head itself needs to be removed. I traverse the linked list with a pointer. If the current node’s value is in the set, I skip it by linking to curr.next; otherwise, I move forward. Finally, I return dummy.next as the new head of the modified list.

Implementation

class Solution:
 def modifiedList(self, nums: List[int], head: Optional[ListNode]) -&gt; Optional[ListNode]:
@Ifihan
Ifihan / main.md
Created October 31, 2025 17:23
The Two Sneaky Numbers of Digitville

Question

Approach

I scan the array once while keeping a set of numbers I have already seen. Whenever I encounter a number that is already in the set, it must be one of the sneaky repeated numbers, so I add it to the result list. Since there are exactly two repeats, I stop once I found both.

Implementation

class Solution:
 def getSneakyNumbers(self, nums: List[int]) -&gt; List[int]:
@Ifihan
Ifihan / main.md
Created October 30, 2025 22:25
Minimum Number of Increments on Subarrays to Form a Target Array

Question

Approach

My approach is to track how many times the array increases. I start by setting the number of operations to the first element since that’s how many increments are needed to reach it from zero. Then, as I move through the array, I only add the positive differences between consecutive elements—because whenever a value increases compared to the previous one, that increase represents new operations needed. This gives the minimum number of operations efficiently in one pass.

Implementation

class Solution:
 def minNumberOperations(self, target: List[int]) -&gt; int: