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 March 31, 2025 22:11
Put Marbles in Bags
@Ifihan
Ifihan / main.md
Created March 30, 2025 22:20
Partition Labels

Question

Approach

I first recorded the last occurrence of each character in the string. Then, I iterated through the string while maintaining a window that represents the current partition. For each character, I updated the right boundary of the window to the farthest last occurrence seen so far. Once I reached the end of this window, it meant that all characters in the window were unique to it—so I closed the partition, recorded its size, and started a new one.

Implementation

class Solution:
 def partitionLabels(self, s: str) -> List[int]:
@Ifihan
Ifihan / main.md
Created March 29, 2025 22:28
Apply Operations to Maximize Score
@Ifihan
Ifihan / maind.md
Created March 28, 2025 20:05
Maximum Number of Points From Grid Queries
@Ifihan
Ifihan / main.md
Created March 27, 2025 22:21
Minimum Index of a Valid Split

Question

Approach

I started by finding the dominant element in the entire array using the Boyer-Moore majority vote algorithm, since we’re told there's exactly one dominant element. Once I had the dominant element, I went through the array while keeping a count of how many times the dominant appeared in the prefix up to the current index.

For each index i, I checked if the dominant element was dominant in both the prefix nums[:i+1] and the suffix nums[i+1:]. The first index where both parts have the dominant element is the minimum valid split index, so I returned that. If no valid split exists, I returned -1.

Implementation

@Ifihan
Ifihan / main.md
Created March 26, 2025 11:40
Minimum Operations to Make a Uni-Value Grid

Question

Approach

To solve this problem, I first flattened the grid into a single list and sorted it. Then I checked whether all differences between elements and the first element are divisible by x—if not, it's impossible to make the grid uni-value and I return -1. If not, I used the median of the list as the target value (since it minimizes the total number of absolute differences), and computed the total number of operations needed by summing up abs(value - median) // x for each value.

Implementation

class Solution:
    def minOperations(self, grid: List[List[int]], x: int) -> int:
@Ifihan
Ifihan / main.md
Created March 25, 2025 21:36
Check if Grid can be Cut into Sections

Question

Approach

To solve this, I grouped the rectangles by sorting them based on their x or y coordinates. Then, I scanned through the sorted list to find points where I could split them into three non-overlapping groups—each with at least one rectangle and no rectangle spanning across the cut. I checked both horizontal and vertical directions, and if either allowed such a split, I returned True; if not, I returned False.

That didn't pass all the cases which is weird and didn't find why it was like that. So I checked the editiorial and saw my mistaskes.

@Ifihan
Ifihan / main.md
Created March 24, 2025 18:16
Count Days Without Meetings

Question

Approach

To solve the problem, I sorted all the meeting intervals by their start day and merged any overlapping ones to avoid double-counting days. After merging, I calculated the total number of unique meeting days by summing the length of each merged interval. Finally, I subtracted the total meeting days from the given number of days to get the count of days when the employee is available but has no meetings.

Implementation

class Solution:
 def countDays(self, days: int, meetings: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created March 23, 2025 22:16
Number of Ways to Arrive at Destination

Question

Approach

To solve the problem, I used Dijkstra’s algorithm to find the shortest path from node 0 to node n - 1. While doing that, I kept track of the number of ways to reach each node using a ways array. I initialized the distance to node 0 as 0 and the number of ways to reach it as 1. As I traversed the graph using a min-heap, I updated the shortest distance to each node. If I found a shorter path, I reset the ways count; if I found an equally short path, I added to the existing count. Finally, I returned the number of ways to reach the destination node in the shortest time, modulo (10^9 + 7).

Implementation

class Solution:
    def countPaths(self, n: int, roads: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created March 22, 2025 21:26
Count the Number of Complete Components

Question

Approach

I perform BFS on each unvisited node to gather all nodes in a connected component. I then count the number of nodes and edges in that component. A complete component of size k must have exactly k*(k-1)/2 edges.

Implementation

class Solution:
    def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int: