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 October 12, 2025 22:09
Find Sum of Array Product of Magical Sequences
@Ifihan
Ifihan / main.md
Created October 11, 2025 22:24
Maximum Total Damage With Spell Casting

Question

Approach

I group spells by damage and replace each damage x with a single weight total[x] = x * count(x). Because choosing damage x forbids x±1 and x±2, I can only combine damages that differ by at least 3. I sort the unique damage values and run a dynamic programming over that sorted list: for each index i with value vals[i], either skip it (carry dp[i-1]) or take total[vals[i]] plus the best dp from the largest index j with vals[j] ≤ vals[i] - 3. I find that j with bisect_right and use a prefix_max array for O(1) lookup of the best dp up to any index. The answer is the last dp value.

Implementation

class Solution:
    def maximumTotalDamage(self, power: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created October 10, 2025 17:31
Taking Maximum Energy From the Mystic Dungeon

Question

Approach

I notice the teleport pattern partitions indices into k independent chains: indices r, r+k, r+2k, ... for each residue r = 0..k-1. Starting at any index means picking a suffix of one of those chains and summing all elements in that suffix. So for each chain I compute the maximum suffix sum (scanning its elements from the end toward the front), and the answer is the maximum among those k values.

Implementation

class Solution:
    def maximumEnergy(self, energy: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created October 9, 2025 22:59
Find the Minimum Amount of Time to Brew Potions
@Ifihan
Ifihan / main.md
Created October 8, 2025 18:05
Successful Pairs of Spells and Potions

Question

Approach

I sort the potions array once. For each spell, I compute the minimum potion strength needed so that spell * potion >= success, which is needed = ceil(success / spell). Then I binary-search (bisect_left) in the sorted potions to find the first index with value >= needed; the number of successful potions for that spell is m - index.

Implementation

class Solution:
 def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
@Ifihan
Ifihan / main.md
Created October 7, 2025 22:05
Avoid Flood in The City

Question

Approach

I used a hash map to track which lakes are currently full and a min-heap to manage the available dry days. When it rains over a lake, I check if it's already full—if so, I must have previously scheduled a dry day for it; otherwise, a flood occurs. On dry days, I store their indices in a heap so I can later assign them efficiently to the lakes that need drying before their next rain. This ensures I always dry the most urgent lake first and avoid floods optimally.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created October 6, 2025 22:49
Swim in Rising Water

Question

Approach

I approach this problem by treating the grid as a graph where each cell represents a node, and I aim to find a path from the top-left to the bottom-right that minimizes the highest elevation encountered along the way. Since water rises over time, the minimum time required to reach the destination equals the smallest possible “maximum elevation” on any valid path. To achieve this, I use a min-heap (priority queue) similar to Dijkstra’s algorithm, always exploring the cell with the lowest elevation first while keeping track of the maximum elevation seen so far. Once I reach the bottom-right cell, that maximum elevation represents the minimum time needed to swim across.

Implementation

class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created October 5, 2025 14:33
Pacific Atlantic Water Flow

Question

Approach

I start two BFSs (or DFSs): one from all Pacific-border cells (top row + left column) and one from all Atlantic-border cells (bottom row + right column). For each search I only move from a cell to a neighbor if the neighbor's height is greater than or equal to the current cell's height (this simulates water flowing downhill from neighbor → current, so reversing the direction finds all cells that can flow to that ocean). Cells reachable by both searches can flow to both oceans.

Implementation

class Solution:
    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
@Ifihan
Ifihan / main.md
Created October 4, 2025 22:13
Container With Most Water

Question

Approach

I use two pointers at the edges and calculate the area formed by the two heights. Since the container height is limited by the shorter line, I move the pointer pointing to the shorter line inward to try and maximize the area. I repeat until the two pointers meet, keeping track of the maximum area found.

Implementation

class Solution:
 def maxArea(self, height: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created October 3, 2025 22:36
Trapping Rain Water II