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 September 12, 2025 20:43
Vowels Game in a String

Question

Approach

I observe that Alice can make a move iff the string contains at least one vowel, because she can always remove a single-letter substring that is a vowel (which has 1—odd—vowel). If there are no vowels, Alice has no valid first move and immediately loses. If there is at least one vowel, Alice can win: she can either delete the whole string if the total number of vowels is odd, or delete a suitable odd-vowel substring (e.g., any single vowel) to force a position where Bob has no winning reply. Therefore, the outcome reduces to a simple check—Alice wins iff the string contains any vowel.

Implementation

class Solution:
 def doesAliceWin(self, s: str) -> bool:
@Ifihan
Ifihan / main.md
Created September 11, 2025 22:40
Sort Vowels in a String

Question

Approach

I scan the string once, collecting the indices and characters of all vowels (treating vowels as a,e,i,o,u in both cases). I then sort just the vowel characters by their ASCII values (so uppercase come before lowercase), and write them back into the original string positions while leaving all consonants untouched.

Implementation

class Solution:
 def sortVowels(self, s: str) -> str:
@Ifihan
Ifihan / main.md
Created September 10, 2025 22:55
Minimum Number of People to Teach

Question

Approach

I first identify all friendship pairs that cannot currently communicate—i.e., the two users share no common language. Only users appearing in such “broken” pairs could possibly need teaching; everyone else is irrelevant. Let A be the set of these affected users. If A is empty, the answer is 0. Otherwise, for each language L (from 1..n), I count how many users in A already know L. If I choose to teach language L, I would need to teach it to |A| - count_L users. I take the minimum over all languages.

Implementation

class Solution:
    def minimumTeachings(self, n: int, languages: List[List[int]], friendships: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created September 9, 2025 18:29
Number of People Aware of a Secret

Question

Approach

I use dynamic programming where new[d] is how many people learn the secret on day d. The first person gives new[1] = 1. A person who learned on day t starts sharing from day t+delay and stops after day t+forget-1. So the number of people actively sharing on day d is a sliding-window sum over new[d-delay .. d-forget+1]. I maintain this window with two updates per day: add new[d-delay] when it enters, subtract new[d-forget] when it leaves. Then new[d] = sharers because every sharer tells exactly one new person that day. At the end, the number of people who still know the secret is those who learned in the last forget-1 days: sum of new[d] for d ∈ [n-forget+1, n] (mod $10^9+7$).

Implementation

MOD = 10**9 + 7
@Ifihan
Ifihan / main.md
Created September 8, 2025 22:19
Convert Integer to the Sum of Two No-Zero Integers

Question

Approach

To find two No-Zero integers that sum to n, I can try splitting n into two numbers a and b = n - a. Starting from a = 1, I check whether both a and b contain no zero digits in their decimal representation. If they do, I return [a, b]. Since the problem guarantees at least one solution exists, this simple loop always works. The check for a No-Zero integer can be done by converting the number to a string and verifying that '0' does not appear in it.

Implementation

class Solution:
 def getNoZeroIntegers(self, n: int) -> List[int]:
@Ifihan
Ifihan / main.md
Created September 7, 2025 22:10
Find N Unique Integers Sum up to Zero

Question

Approach

To construct an array of n unique integers that sum to zero, I use a simple symmetric pairing method. If n is even, I can generate pairs like (1, -1), (2, -2), …, giving exactly n elements whose sum is zero. If n is odd, I do the same but also include a 0 to balance the count, since adding zero doesn’t affect the sum.

Implementation

class Solution:
 def sumZero(self, n: int) -> List[int]:
@Ifihan
Ifihan / main.md
Created September 6, 2025 21:43
Minimum Operations to Make Array Elements Zero

Question

Approach

Approach (concise, first person)

I noted that applying one operation to a number x replaces it with ⌊x/4⌋, so the number of operations needed to turn a single x into 0 is exactly the number of base-4 digits of x, i.e., steps(x) = 1 + ⌊log₄ x⌋ (intervals: 1..3→1, 4..15→2, 16..63→3, …). Since each operation acts on two numbers at once, the minimum operations for an interval [l, r] are constrained by (i) the total step demand S = Σ steps(x) and (ii) the largest per-item demand M = max steps(x). Each operation can reduce the remaining total by at most 2 (one step per chosen item), but a single item still needs at least M operations applied to it across time. Therefore, the optimal number of operations for a query is max(M, ceil(S/2)). Over many queries, I precompute powers of 4 and the prefix sums of steps(x) grouped by `[4^{t-1}, 4

@Ifihan
Ifihan / main.md
Created September 5, 2025 22:39
Minimum Operations to Make the Integer Zero

Question

Approach

The key idea is to model each operation as subtracting $2^i + \text{num2}$ from num1. After $k$ operations, this means we subtract $k \cdot \text{num2} + S$, where $S$ is the sum of $k$ powers of two (with repetition allowed). Rearranging gives $S = \text{num1} - k \cdot \text{num2}$. For a given $k$, this $S$ must be non-negative and representable as a sum of exactly $k$ powers of two. The smallest number of powers of two needed for $S$ is its binary popcount, and the maximum number is $S$ itself (all ones). So the condition is $\text{popcount}(S) \leq k \leq S$. I loop over $k$ from 1 up to 60 (enough since $2^{60}$ is larger than the constraints), compute $S$, and return the first valid $k$. If no such $k$ works, the answer is $-1$.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created September 4, 2025 21:45
Find Closest Person

Question

Approach

I compute the absolute distances from each moving person to Person 3: dx = |x - z| and dy = |y - z|. Since both move at the same speed, the one with the smaller distance arrives first. If dx < dy, I return 1; if dy < dx, I return 2; otherwise, they arrive simultaneously and I return 0.

Implementation

class Solution:
 def findClosest(self, x: int, y: int, z: int) -&gt; int:
@Ifihan
Ifihan / main.md
Last active September 4, 2025 21:44