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 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:
@Ifihan
Ifihan / main.md
Created March 10, 2025 22:51
Count of Substrings Containing Every Vowel and K Consonants II

Question

Approach

I use a sliding window approach that dynamically expands and shrinks. Then I track vowel occurrences and ensure all 5 vowels appear at least once.

After that, I count consonants, ensuring exactly k consonants exist in the substring. When conditions are met, I count all valid substrings that start from left to right. This failed for around 22 testcases. I tried other methods but didn't get a solution so I visited the editorial

image
@Ifihan
Ifihan / main.md
Created March 9, 2025 22:49
Alternating Groups II

Question

Approach

I iterate through all possible starting positions in the circular array. For each starting position, I check if the k-length subarray follows the alternating pattern.

Since the array is circular, I use modulo indexing to wrap around. If a subarray follows the alternating pattern, I increment the count. I kept on getting TLE and all my other approaches were not working so I used the editorial

@Ifihan
Ifihan / main.md
Created March 8, 2025 22:14
Minimum Recolors to Get K Consecutive Black Blocks

Question

Approach

I use the sliding window technique to find the minimum number of white blocks ('W') that need to be changed in any substring of length k.

I then iterate through all possible windows of size k in the string and count the number of 'W' characters, keeping track of the minimum count.

Implementation

@Ifihan
Ifihan / main.md
Created March 7, 2025 19:29
Closest Prime Numbers in Range

Question

Approach

I used the Sieve of Eratosthenes algorithm*. I used it to first find all prime numbers up to 'right'. Then, I filter primes within the given range [left, right]. If fewer than two primes exist, I return [-1, -1]. If not, I iterate through the primes to find the pair with the smallest difference.

You can read about the Sieve of Eratosthenes here.

Implementation

@Ifihan
Ifihan / main.md
Created March 6, 2025 22:11
Find Missing and Repeated Values

Question

Approach

In this approach, I traverse the grid to count the occurrences of each number using a dictionary. Then, I iterate through the expected range [1, n^2] to identify the repeated and missing numbers.

Implementation

Complexities

@Ifihan
Ifihan / main.md
Created March 5, 2025 21:51
Count Total Number of Colored Cells

Question

Approach

I observe that the pattern of growth follows a square expansion. At each step, new layers of cells are added around the previous configuration.

The formula for the total number of colored cells after n minutes is derived as: Total colored cells = n^2 + (n-1)^2

Implementation

@Ifihan
Ifihan / main.md
Created March 4, 2025 22:18
Check if Number is a Sum of Powers of Three

Question

Approach

I used a greedy approach by repeatedly checking the remainder when dividing n by 3. If at any step, the remainder is 2, it means that n cannot be represented as a sum of distinct powers of three. Otherwise, I keep dividing n by 3 until it becomes zero.

Implementation

class Solution:
 def checkPowersOfThree(self, n: int) -> bool: