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 22, 2025 21:57
Count Elements With Maximum Frequency

Question

Approach

I count how many times each number appears in the array using a frequency counter (like collections.Counter). Then I find the maximum frequency among all elements. Finally, I add up the frequencies of all elements that appear with this maximum frequency. This gives the total number of elements in the array that have the maximum frequency.

Implementation

class Solution:
 def maxFrequencyElements(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created September 21, 2025 22:18
Design Movie Rental System

Question

Approach

I tried several solutions. Unfortunately, no editorial today, haha. So I checked out one of the solutions dropped by a nice leetcoder!

image
@Ifihan
Ifihan / main.md
Created September 20, 2025 22:42
Implement Router

Question

Approach

I maintain the packets in a FIFO deque to respect insertion/forwarding order, and a set of (source, destination, timestamp) to detect duplicates in O(1). For destination/time range queries, I keep a sorted list of timestamps per destination (dest2times[destination]). On insert, I insort the timestamp to keep the list sorted; on delete (due to eviction or forwarding), I remove a single occurrence using bisect_left. getCount(dest, l, r) is then the count of timestamps in [l, r] via two binary searches (bisect_left/bisect_right). When memory would be exceeded on addPacket, I evict the oldest packet from the deque and update all indices. All operations are efficient: O(log k) for range counts/updates per destination (plus list shift cost on deletion), and O(1) for duplicate checks.

Implementation

class Router:
@Ifihan
Ifihan / main.md
Created September 19, 2025 19:49
Design Spreadsheet

Question

Approach

  • Storage: I use a dictionary self.cells where keys are (col, row) tuples (like ("A", 1)) and values are integers. This avoids building a full grid of zeros.

  • setCell: Updates the dictionary with the given value.

@Ifihan
Ifihan / main.md
Created September 18, 2025 20:40
Design Task Manager

Question

Approach

I keep a global max-heap of tasks keyed by (-priority, -taskId) so the highest priority (and, on ties, the largest taskId) is on top. I also maintain a hashmap info[taskId] = (userId, priority) as the source of truth. All updates are done by lazy heap updates: on add or edit I push a fresh heap entry; on rmv I delete from the map. In execTop, I pop heap entries until I find one that still matches the current mapping (i.e., taskId exists and priority matches); then I remove it from the map and return its userId. If the heap runs out, I return -1.

Implementation

class TaskManager:

    def __init__(self, tasks: List[List[int]]):
@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: