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 May 31, 2025 22:25
Snakes and Ladders

Question

Approach

I used a Breadth-First Search (BFS) starting from square 1 to simulate the minimum number of dice rolls needed to reach the final square. I converted the square numbers into board coordinates by carefully accounting for the alternating row direction (boustrophedon layout). At each step, I explored all possible dice rolls (1 to 6), checked if a ladder or snake redirected the move, and added the resulting square to the queue if it hadn’t been visited.

Implementation

class Solution:
    def snakesAndLadders(self, board: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created May 30, 2025 21:21
Find Closest Node to Given Two Nodes

Question

Approach

To solve this, I first compute the distances from node1 and node2 to all other nodes using a simple traversal, since each node has at most one outgoing edge. I record these distances in two separate arrays. Then, I iterate through all nodes to find the ones that are reachable from both node1 and node2. For each such node, I calculate the maximum of the two distances and keep track of the node with the smallest such maximum. If there’s a tie, I pick the node with the smaller index.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created May 29, 2025 16:17
Maximize the Number of Target Nodes After Connecting Trees II
@Ifihan
Ifihan / main.md
Created May 28, 2025 18:23
Maximize the Number of Target Nodes After Connecting Trees I

Question

Approach

I actually tried solving this question but I failed severally. So I used the editorial.

image
@Ifihan
Ifihan / main.md
Created May 27, 2025 22:07
Divisible and Non-divisible Sums Difference

Question

Approach

I calculated the total sum from 1 to n using the formula n * (n + 1) // 2. Then, I found how many numbers are divisible by m (n // m) and used the arithmetic series formula to get their sum. Finally, I subtracted the divisible sum from the total to get the difference between non-divisible and divisible sums.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created May 26, 2025 22:05
Largest Color Value in a Directed Graph

Question

Approach

I solved this problem using a topological sort because the presence of a cycle would make it impossible to define valid paths. I used Kahn's algorithm to detect cycles and simultaneously calculated the most frequent color occurrences using dynamic programming. For each node, I maintained a frequency table for all 26 lowercase letters. When visiting a node's neighbor, I propagated the color frequencies accordingly. Finally, if all nodes were visited, I returned the maximum color frequency seen on any path; otherwise, I returned -1 for the cycle case.

Implementation

class Solution:
    def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
@Ifihan
Ifihan / main.md
Created May 25, 2025 21:54
Longest Palindrome by Concatenating Two Letter Words

Question

Approach

I use a Counter to count occurrences of each word. For non-palindromic words (like "ab" and "ba"), I find the matching reverse pairs and count the minimum between them. For palindromic words (like "gg", "cc"), I use pairs and reserve one (if available) for the center of the palindrome. Each pair contributes 4 characters (2 + 2), and the center adds 2.

Implementation

class Solution:
    def longestPalindrome(self, words: List[str]) -> int:
@Ifihan
Ifihan / main.md
Created May 24, 2025 22:07
Find Words Containing Character

Question

Approach

I iterate through each word using enumerate to keep track of the index. If the character x is found in the word, I add the index to the result list. Then, I return the list of indices.

Implementation

class Solution:
 def findWordsContaining(self, words: List[str], x: str) -> List[int]:
@Ifihan
Ifihan / main.md
Created May 23, 2025 22:34
Find the Maximum Sum of Node Values
@Ifihan
Ifihan / main.md
Created May 22, 2025 22:02
Zero Array Transformation III

Question

Approach

I approached the problem by realizing I need to ensure each index i in nums is decremented at least nums[i] times. I used a difference array to simulate how many times each index is affected by the remaining queries. Then, I binary searched on the number of queries I could safely remove while still converting nums into a zero array. 486 / 824 testcases passed.

Then the editorial