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 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:
@Ifihan
Ifihan / main.md
Created March 21, 2025 18:10
Find All Possible Recipes from Given Supplies

Question

Approach

I use a topological sorting (Kahn's algorithm) approach with a graph-based representation.

  1. Construct a directed graph where each ingredient points to the recipes that depend on it.
  2. Maintain an in-degree counter to track how many ingredients are needed for each recipe.
  3. Use a queue to process supplies and propagate the ability to cook recipes.
  4. If a recipe's in-degree reaches zero, it can be made and is added to the queue.
  5. The process continues until no more recipes can be created.
@Ifihan
Ifihan / main.md
Created March 20, 2025 18:47
Minimum Cost Walk in Weighted Graph
@Ifihan
Ifihan / main.md
Created March 19, 2025 21:16
Minimum Operations to Make Binary Array Elements Equal to One I

Question

Approach

In this approach, I iterate through the array and flip the first encountered '0' along with the next two elements. This ensures I always maximize the number of '1's as it moves forward. If any '0' remains in the last two positions, it is impossible to make all elements '1'.

Implementation

class Solution:
 def minOperations(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created March 18, 2025 18:02
Longest Nice Subarray

Question

Approach

I use a sliding window approach with bitwise operations to find the longest nice subarray. The key observation is that a valid subarray must have all elements with unique bits (bitwise AND must be 0). I expand the right pointer and shrink the left pointer when a conflict arises.

Implementation

class Solution:
 def longestNiceSubarray(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created March 17, 2025 18:26
Divide Array Into Equal Pairs

Question

Approach

I use a frequency counter to count the occurrences of each number in nums. If every number appears an even number of times, then nums can be divided into valid pairs. Else it's not possible to divide the array into required pairs.

Implementation

class Solution:
 def divideArray(self, nums: List[int]) -> bool:
@Ifihan
Ifihan / main.md
Created March 16, 2025 19:24
Minimum Time to Repair Cars

Question

Approach

I use a binary search approach to determine the minimum time needed to repair all cars. The search range is from 1 to the maximum possible repair time, calculated as min(ranks) * cars^2. The helper function canRepairWithinTime(t) checks if the given time t allows enough cars to be repaired.

Implementation

class Solution:
 def repairCars(self, ranks: List[int], cars: int) -> int:
@Ifihan
Ifihan / main.md
Created March 15, 2025 22:01
House Robber IV

Question

Approach

For this question, I used a binary search approach to determine the minimum capability the robber needs. The search space is between the smallest and largest house values.

The helper function canRob(capability) checks if it's possible to rob at least k houses without stealing from adjacent ones.

Implementation

@Ifihan
Ifihan / main.md
Created March 14, 2025 22:09
Maximum Candies Allocated to K Children

Question

Approach

I used a binary search approach to determine the maximum number of candies each child can receive. The search range is between 1 and the maximum pile size.

The helper function canAllocate checks if it's possible to allocate at least k piles of size mid.

Implementation

@Ifihan
Ifihan / main.md
Created March 13, 2025 21:46
Zero Array Transformation II

Question

Approach

In this approach, I use a difference array to efficiently apply range updates. Instead of modifying nums at each query, I accumulate updates in the difference array. After processing each query, I apply the updates and check if nums becomes a zero array.

This approach didn't work for some edge cases so i used the editorial to see my mistakes.

image