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 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
@Ifihan
Ifihan / main.md
Created March 12, 2025 21:27
Maximum Count of Positive Integer and Negative Integer

Question

Approach

In my approach, I iterate through the list and count the number of positive and negative numbers separately. If a number is greater than zero, I increase the positive count. If a number is less than zero, I increase the negative count. Finally, I return the maximum count between positive and negative numbers.

Implementation

class Solution:
 def maximumCount(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created March 11, 2025 22:15
Number of Substrings Containing All Three Characters

Question

Approach

I use a sliding window approach to count substrings containing at least one 'a', 'b', and 'c'. I expand the window to include characters until all three are present. Then, I count all possible substrings ending at right and reduce the window from left.

Implementation

class Solution:
 def numberOfSubstrings(self, s: str) -> int: