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 October 22, 2025 21:37
Maximum Frequency of an Element After Performing Operations II
@Ifihan
Ifihan / main.md
Created October 21, 2025 21:11
Maximum Frequency of an Element After Performing Operations I
@Ifihan
Ifihan / main.md
Created October 20, 2025 22:07
Final Value of Variable After Performing Operations

Question

Approach

I start with X = 0. Each operation either increments (++X or X++) or decrements (--X or X--) the value of X by 1. So, I just iterate through the operations, adding 1 for "++" and subtracting 1 for "--".

Implementation

@Ifihan
Ifihan / main.md
Created October 19, 2025 22:55
Lexicographically Smallest String After Applying Operations

Question

Approach

I explore all reachable strings from s using BFS (or DFS) on the state space of strings, applying the two allowed operations until no new string appears. I keep a visited set to avoid cycles and always track the lexicographically smallest string seen. Since |s| ≤ 100 and every operation maps a string to another string of the same length with digits 0..9, the reachable state space is finite and BFS will terminate quickly in practice for the given constraints.

Implementation

class Solution:
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
@Ifihan
Ifihan / main.md
Created October 18, 2025 22:42
Maximum Number of Distinct Elements After Operations

Question

Approach

The key idea to solve this problem efficiently is to treat each element x in the array as an interval [x - k, x + k], representing all the possible values it can take after performing the allowed operation. To maximize the number of distinct elements, we can use a greedy approach: sort all these intervals by their right endpoint, then iterate through them while maintaining a pointer cur that tracks the smallest integer not yet used. For each interval [l, r], we first move cur to the maximum of its current value and the interval’s left endpoint (cur = max(cur, l)), ensuring we stay within the valid range. If cur is still less than or equal to r, we can “use” this value — we increment our answer count and move cur forward by one (cur += 1) to avoid reusing numbers.

Implementation

@Ifihan
Ifihan / main.md
Created October 17, 2025 21:29
Maximize the Number of Partitions After Operations
@Ifihan
Ifihan / main.md
Created October 16, 2025 21:01
Smallest Missing Non-negative Integer After Operations

Question

Approach

I observe that every number can be transformed to any integer having the same residue modulo value (by adding/subtracting value repeatedly). So each element only contributes one token to its residue class r = x % value. To maximize the MEX we need to be able to form all integers 0,1,2,...,m-1. Forming integer i requires consuming one token from residue i % value. Thus I count how many tokens we have per residue, then greedily try to form integers i = 0,1,2,...: for each i I check whether counts[i % value] > 0; if yes I consume one and continue, otherwise the current i is the MEX.

Implementation

class Solution:
    def findSmallestInteger(self, nums: List[int], value: int) -> int:
@Ifihan
Ifihan / main.md
Created October 15, 2025 22:04
Adjacent Increasing Subarrays Detection II

Question

Approach

I first compute lenInc[i] = the length of the longest strictly-increasing subarray starting at index i (so lenInc[i] >= 1). A subarray of length k starting at i is strictly increasing iff lenInc[i] >= k. Given that, two adjacent strictly increasing subarrays of length k starting at a and a+k exist iff there is an index a with lenInc[a] >= k and lenInc[a+k] >= k.

Implementation

class Solution:
    def maxIncreasingSubarrays(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created October 14, 2025 22:42
Adjacent Increasing Subarrays Detection I

Question

Approach

I note a subarray of length k is strictly increasing iff each adjacent pair inside it is increasing, i.e. there are k-1 positions i..i+k-2 where nums[t] < nums[t+1]. So I build a binary array incpair where incpair[i] = 1 iff nums[i] < nums[i+1]. Then I build a prefix sum over incpair so I can test in O(1) whether any length-k window has k-1 increasing pairs. Finally I scan a from 0 to n - 2*k and check whether both windows starting at a and a+k are strictly increasing.

Implementation

class Solution:
    def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:
@Ifihan
Ifihan / main.md
Created October 13, 2025 22:42
Find Resultant Array After Removing Anagrams

Question

Approach

I scan the list and keep a result stack. For each word, I compare its sorted characters to the sorted characters of the last word kept. If they are anagrams (sorted equal), I skip the current word (simulate deleting it). Otherwise I append it to the stack. At the end the stack is the final array after all deletions. This works because deletions only depend on the most recent kept word and the process is order-independent.

Implementation

class Solution:
 def removeAnagrams(self, words: List[str]) -&gt; List[str]: