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 17, 2025 21:27
Design a Food Rating System

Question

Approach

I maintain three structures: (1) a map foodInfo from food → (cuisine, currentRating), (2) for each cuisine, a max-heap keyed by (-rating, name) so highest rating (and lexicographically smallest name on ties) is on top, and (3) a map heapByCuisine from cuisine → its heap. When a rating changes, I update foodInfo and push a new (-newRating, name) into that cuisine’s heap—no deletions. In highestRated, I lazy-clean the heap top: while the heap’s top pair doesn’t match the current rating in foodInfo, I pop it; the first matching top is the correct answer. All operations are $O(\log n)$ per push/pop, satisfying the constraints.

Implementation

class FoodRatings:
@Ifihan
Ifihan / main.md
Created September 16, 2025 22:21
Replace Non-Coprime Numbers in Array

Question

Approach

I solve this problem with a stack-based approach. I process the array from left to right, pushing each number onto the stack. Whenever the new number and the top of the stack are non-coprime (gcd > 1), I pop the stack, replace the two numbers with their LCM, and continue checking with the new top. This merging continues until the top of the stack is coprime with the current value (or the stack is empty). This ensures all adjacent non-coprime numbers collapse into their LCMs step by step, and by the problem statement, the result is unique regardless of order.

Implementation

class Solution:
    def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
@Ifihan
Ifihan / main.md
Created September 15, 2025 22:37
Maximum Number of Words You Can Type

Question

Approach

I split the text into words and check each one to see if it contains any broken letters. If a word contains a broken letter, it cannot be typed, so I skip it. Otherwise, I count it as typeable. To make the check fast, I put all broken letters into a set for O(1) membership tests.

Implementation

class Solution:
 def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
@Ifihan
Ifihan / main.md
Created September 14, 2025 21:38
Vowel Spellchecker

Question

Approach

I build three lookup structures in precedence order: (1) an exact set for case-sensitive matches, (2) a case map from lowercase word → first occurrence in wordlist, and (3) a vowel map from a devoweled lowercase pattern (replace any vowel with *) → first occurrence in wordlist. For each query, I first check the exact set; if not found, I check the lowercase map; if still not found, I check the devoweled pattern; otherwise I return "". This respects the required precedence and returns the earliest wordlist match for case/vowel rules.

Implementation

class Solution:
    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
@Ifihan
Ifihan / main.md
Created September 13, 2025 22:21
Find Most Frequent Vowel and Consonant

Question

Approach

I count the frequencies of all characters in the string. Then I separate vowels (a, e, i, o, u) from consonants (all other lowercase letters). I track the maximum frequency among vowels and the maximum among consonants. If the string has no vowels or no consonants, I treat the missing side as 0. Finally, I return the sum of the two maxima.

Implementation

class Solution:
 def maxFreqSum(self, s: str) -> int:
@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]: