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 August 17, 2025 19:30
New 21 Game

Question

Approach

I approached the New 21 Game problem using dynamic programming with a sliding window technique. I started by defining dp[i] as the probability of having exactly i points. Since Alice only continues drawing while her score is less than k, I knew only those states could generate new outcomes. For each i, the probability depends on the average of the previous maxPts probabilities, but only from states less than k. To avoid recalculating sums repeatedly, I maintained a running sliding window of these valid probabilities: I added the most recent one and removed the one that just went out of range. The result is then the sum of probabilities of terminal scores between k and n. Finally, I handled special cases: if k is 0 or if n is large enough (n >= k - 1 + maxPts), the probability is guaranteed to be 1.0.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created August 16, 2025 22:12
Maximum 69 Number

Question

Approach

Since I’m only allowed to change at most one digit, the optimal strategy is simple: I scan the number from left to right and change the first 6 I encounter into a 9. This guarantees the largest possible number because the leftmost digits contribute more to the value. If no 6 exists, I just return the number unchanged.

Implementation

class Solution:
 def maximum69Number (self, num: int) -> int:
@Ifihan
Ifihan / main.md
Created August 16, 2025 22:04
Power of Four

Question

Approach

To check if n is a power of four, I first ensure that n is positive. Then, I use the property that a power of four is also a power of two, but with its single 1 bit in an odd position (0-indexed). The bitwise check (n & (n-1)) == 0 ensures it’s a power of two. To verify that the 1 bit is in an odd position, I use a mask: 0x55555555 (binary 01010101...), which only has 1s in odd bit positions. If n & 0x55555555 != 0, then n is a power of four.

Implementation

@Ifihan
Ifihan / main.md
Created August 14, 2025 22:56
Largest 3-Same-Digit Number in String

Question

Approach

I scanned through the string num looking at every substring of length 3. For each substring, I checked if all three characters are the same by comparing it to its first character repeated three times. If it is valid, I kept track of the maximum such substring using lexicographic comparison (since digit characters compare in numeric order). At the end, I returned the largest found substring or "" if none was found.

Implementation

class Solution:
 def largestGoodInteger(self, num: str) -> str:
@Ifihan
Ifihan / main.md
Created August 14, 2025 22:52
Power of Three

Question

Approach

The fact that the largest power of three within the 32-bit signed integer range is 3 19 = 1162261467 3 19 =1162261467. If n is positive and divides this largest power evenly, then n must be a power of three.

Implementation

class Solution:
 def isPowerOfThree(self, n: int) -> bool:
@Ifihan
Ifihan / main.md
Created August 12, 2025 18:53
Ways to Express an Integer as Sum of Powers

Question

Approach

I treat this as a 0/1 knapsack counting problem. First, I generate all powers 𝑝 = 𝑖 𝑥 p=i x such that 𝑝 ≤ 𝑛 p≤n. Each power can be used at most once. I use a 1-D DP array dp[s] = number of ways to reach sum s. I set dp[0]=1, then for each power p, I iterate sums backwards from n down to p and do dp[s] += dp[s-p] (mod 10 9 + 7 10 9 +7). The backward loop enforces uniqueness (each power used at most once). The answer is dp[n].

Implementation

MOD = 10**9 + 7
@Ifihan
Ifihan / main.md
Created August 12, 2025 18:47
Range Product Queries of Powers

Question

Approach

I form powers by taking the binary decomposition of n: the minimum multiset of powers of two that sums to n is exactly the set bits of n. If bit i is set, 2^i appears once, and the list sorted non-decreasing is just increasing i. Instead of multiplying powers directly, I store their exponents i. So I precompute a prefix sum of exponents and answer each query

Implementation

MOD = 10**9 + 7
@Ifihan
Ifihan / main.md
Created August 10, 2025 22:41
Reordered Power of 2

Question

Approach

I compare the digit multiset of n to those of all powers of two within the range <= 10^9. Any valid reordering must match a power of two’s digits exactly (same counts for 0–9), and matching a power of two automatically avoids leading-zero issues because the number of digits must be the same. So I precompute the digit “signature” (a 10-tuple of counts) for every 2^k where k=0..29, then check if n’s signature is in that set.

Implementation

class Solution:
 def reorderedPowerOf2(self, n: int) -&gt; bool:
@Ifihan
Ifihan / main.md
Created August 9, 2025 22:39
Power of Two

Question

Approach

I used a bitwise trick. A number is a power of two if:

  • It is positive.
  • It has exactly one bit set in its binary representation.

The property (n & (n - 1)) == 0 checks this, because subtracting 1 from a power of two flips the single 1 bit to 0 and sets all lower bits to 1, so the bitwise AND becomes 0. For any number that isn’t a power of two, this condition fails.

@Ifihan
Ifihan / main.md
Created August 8, 2025 22:45
Soup Servings

Question

Approach

I reduce the problem to 25 mL units because all operations are multiples of 25. Let m = ceil(n/25). I use DP with memoization on states (a,b) meaning “a units of A and b units of B remain.” The base cases are:

  • if a<=0 and b<=0 → return 0.5 (both empty same turn),
  • if a<=0 → return 1.0 (A empties first),
  • if b<=0 → return 0.0 (B empties first).