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
Last active May 2, 2025 18:05
Push Dominoes

Question

Approach

When solving this, I imagined the dominoes as experiencing "forces" from left and right. Each domino receives a force from the closest 'R' to its left or 'L' to its right. The closer the source, the stronger the force. I assigned a large positive value when a domino was pushed to the right ('R') and a large negative value when pushed to the left ('L'). Then I scanned the string twice:

  • Left to Right: I assigned decreasing positive forces after each 'R' and reset after 'L'.
  • Right to Left: I assigned decreasing negative forces after each 'L' and reset after 'R'.

Finally, I added the forces at each position:

@Ifihan
Ifihan / main.md
Created May 1, 2025 21:37
Maximum Number of Tasks You Can Assign
@Ifihan
Ifihan / main.md
Created April 30, 2025 21:14
Find Numbers with Even Number of Digits

Question

Approach

To solve this, I just iterated through the nums array and for each number, I counted the number of digits using either len(str(num)) or logarithmic math. If the number of digits was even, I incremented a counter.

Implementation

class Solution:
 def findNumbers(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created April 29, 2025 17:07
Count Subarrays Where Max Element Appears at Least K Times

Question

Approach

I iterated through the array with a right pointer and maintained a left pointer to denote the start of the current window. As I moved the right pointer, I tracked how many times the current max value appeared in the window.

Each time the count of the max value reached at least k, I knew that all subarrays ending at this right and starting from any index ≤ left would be valid. So I added (left + 1) to the result.

If the count of the max fell below k, I shrank the window from the left until the condition was satisfied again.

@Ifihan
Ifihan / main.md
Created April 28, 2025 21:36
Count Subarrays With Score Less Than K
@Ifihan
Ifihan / main.md
Created April 27, 2025 22:30
Count Subarrays of Length Three With a Condition

Question

Approach

When solving this, I noticed that I only needed to check all subarrays of length 3. So I iterated over the array, looking at every group of three consecutive numbers. For each triplet, I checked if the sum of the first and third numbers was exactly half of the second number. If the condition was satisfied, I incremented my answer counter.

Implementation

@Ifihan
Ifihan / main.md
Created April 26, 2025 22:09
Count Subarrays With Fixed Bounds

Question

Approach

When solving this, I realized I needed to find subarrays where the minimum is exactly minK and the maximum is exactly maxK.
To do this efficiently, I tracked three things while scanning nums:

  • The last index where the number was out of range (out_idx), meaning it was less than minK or greater than maxK.
  • The last index where I saw minK (min_idx).
  • The last index where I saw maxK (max_idx).
@Ifihan
Ifihan / maind.md
Created April 25, 2025 21:43
Count of Interesting Subarrays

Question

Approach

To solve the problem, I kept track of how many numbers so far satisfy nums[i] % modulo == k using a prefix count. At each position, I calculated the current prefix modulo and checked how many previous prefix sums would form an interesting subarray ending at the current index. I used a hashmap to record the frequency of each prefix modulo. By rearranging the modulo condition, I quickly found how many matches existed without checking every subarray. I updated the answer at each step and continued this process across the array.

Implementation

class Solution:
    def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
@Ifihan
Ifihan / main.md
Created April 24, 2025 22:29
Count Complete Subarrays in an Array

Question

Approach

First, I computed the number of distinct elements in the entire array. This gave me the target count that each "complete" subarray must satisfy.

Then, I used a sliding window approach: I moved the left and right pointers to track subarrays, using a hash map to count the frequency of elements in the current window. Each time the number of distinct elements in the window matched the total distinct count, I knew that all subarrays starting from the current left and ending anywhere from the right to the end would also be valid, so I counted them accordingly.

To simplify the problem even more, I actually iterated over all subarrays and used a set to track distinct elements within each subarray (acceptable since n ≤ 1000).

@Ifihan
Ifihan / main.md
Created April 23, 2025 19:07
Count Largest Group

Question

Approach

I grouped each number from 1 to n based on the sum of its digits. I used a dictionary to keep track of how many numbers belonged to each digit-sum group. For each number in the range, I calculated the digit sum by converting the number to a string, summing the integer values of its characters, and incremented the count in the corresponding group.

After processing all numbers, I looked at the values in the dictionary to determine the size of the largest group — this was just the maximum frequency among all groups. Then, I counted how many groups had this maximum size. That gave me the number of groups with the largest size, which I returned as the final answer.

Implementation