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 December 12, 2025 22:57
Count Mentions Per User

Question

Approach

I simulate events in increasing timestamp order and, for events sharing a timestamp, I process all OFFLINE events before MESSAGE events (because status changes are applied before messages at the same time). I keep an offline_until time for each user (the time they become online again); a user is online at time T iff T >= offline_until[user]. On an OFFLINE event at time t for user id I set offline_until[id] = t + 60. On a MESSAGE event at time t I parse the mentions string token-by-token:

  • ALL increments every user's count (offline or online).
  • HERE increments the count for every user currently online at time t.
  • idX increments user X (count each occurrence; duplicates count separately).
@Ifihan
Ifihan / main.md
Created December 12, 2025 22:49
Count Covered Buildings

Question

Approach

I store all buildings by row and by column and only need to know, for each row x, the smallest and largest column index y present and, for each column y, the smallest and largest row index x present. A building at (x,y) is covered iff there exists at least one building strictly above (some x' < x in same column), strictly below (x' > x in same column), strictly left (y' < y in same row) and strictly right (y' > y in same row). That is exactly the condition min_row_y < y < max_row_y and min_col_x < x < max_col_x. I precompute those mins/maxs for every row and column in O(m) time (m = number of buildings) and then count how many buildings satisfy both strict inequalities.

Implementation

class Solution:
    def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created December 10, 2025 22:00
Count the Number of Computer Unlocking Permutations

Question

Approach

I observe that for any index i > 0 to be unlockable there must exist some j < i with complexity[j] < complexity[i]. In particular j = 0 is always a candidate, so a sufficient (and in fact necessary) condition for all i > 0 to be unlockable is that complexity[0] is strictly smaller than every other complexity. If any i > 0 has complexity[i] <= complexity[0] then i has no earlier index with strictly smaller complexity and the task is impossible (answer 0). If complexity[0] is strictly the global minimum, then initially only computer 0 is unlocked but it can be used to unlock any other computer (since 0 < i and complexity[0] < complexity[i]), so the remaining n-1 computers can be unlocked in any order. Hence the number of valid permutations is (n-1)! modulo 10^9+7.

Implementation

class Solution:
    def countPermutations(self, complexity: List[int]) -> 
@Ifihan
Ifihan / main.md
Created December 9, 2025 22:31
Count Special Triplets

Question

Approach

I scan the array once and treat each index j as the middle of a potential triplet. I keep two frequency maps: right initially counts all elements to the right of j (including j at start) and left counts elements to the left of j. For each j I remove nums[j] from right, compute target = 2 * nums[j], then the number of special triplets with this j as middle equals left[target] * right[target]. I add that to the answer and then add nums[j] into left before moving on. I take results mod 10^9+7.

Implementation

class Solution:
    def specialTriplets(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created December 8, 2025 19:24
Count Square Sum Triples

Question

Approach

I iterate over all ordered pairs (a,b) with 1 ≤ a,b ≤ n and compute s = aa + bb. I take the integer square root c = isqrt(s) and check whether c*c == s and c ≤ n. If so, (a,b,c) is a valid square triple — I count it.

Implementation

class Solution:
 def countTriples(self, n: int) -&gt; int:
@Ifihan
Ifihan / main.md
Created December 7, 2025 23:00
Count Odd Numbers in an Interval Range

Question

Approach

I calculate how many odd numbers appear up to high and subtract how many appear up to low - 1. Any number x has (x + 1) // 2 odd numbers from 0 to x, so the total odds in the range is simply (high + 1) // 2 - (low // 2).

Implementation

class Solution:
 def countOdds(self, low: int, high: int) -&gt; int:
@Ifihan
Ifihan / main.md
Created December 6, 2025 21:52
Count Partitions With Max-Min Difference at Most K

Question

Approach

I use dynamic programming where dp[r] is the number of ways to partition the first r elements (r from 0..n) and dp[0]=1. For each right end r (1-based for prefix length) I find the smallest l (0-based index) such that the subarray nums[l..r-1] has max - min ≤ k; then dp[r] = sum(dp[i] for i in [l..r-1]). I compute that range-sum in O(1) with a running prefix-sum array pref. To get every l in amortized O(1) time I maintain a sliding window with two monotonic deques for max and min and advance the left pointer until the window becomes valid.

Implementation

class Solution:
    def countPartitions(self, nums: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created December 5, 2025 22:28
Count Partitions with Even Sum Difference

Question

Approach

I notice that the parity of the difference sum_left - sum_right equals the parity of sum_left + sum_right (because subtraction ≡ addition mod 2), and sum_left + sum_right is just the total sum of the array. So the difference is even exactly when the total sum is even.

Implementation

class Solution:
 def countPartitions(self, nums: List[int]) -&gt; int:
@Ifihan
Ifihan / main.md
Created December 4, 2025 20:13
Count Collisions on a Road

Question

Approach

I realize that cars that will never meet are the leading cars that move left ('L') and the trailing cars that move right ('R') — they run away from everyone else. If I trim those off the ends, every remaining moving car ('L' or 'R') will eventually be involved in a collision (and each contributes exactly one to the total count after accounting the opposed-pair +2 behavior). So I find the first index from the left that is not 'L' and the first index from the right that is not 'R', then count how many characters in that middle segment are not 'S'. That count is the answer.

Implementation

class Solution:
    def countCollisions(self, directions: str) -> int:
@Ifihan
Ifihan / main.md
Created December 3, 2025 22:15
Count Number of Trapezoids II