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 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

@Ifihan
Ifihan / main.md
Created May 21, 2025 09:18
Set Matrix Zeroes

Question

Approach

I first checked if the first row or first column contains any zero since they’ll be used as markers. Then, I used the rest of the matrix to mark rows and columns that should be zeroed by updating the first row and column accordingly. After marking, I iterated again and updated the matrix cells to zero based on these markers. Finally, I handled the first row and first column separately, zeroing them out if needed.

Implementation

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
@Ifihan
Ifihan / main.md
Created May 20, 2025 22:21
Zero Array Transformation I

Question

Approach

To solve this, I used a difference array to track how many times each index gets decremented. For each query, I marked the start of the decrement range and subtracted at the end+1 to simulate range updates. Then I applied the accumulated decrements and ensured I never subtracted more than the current value in nums. If all values become zero after processing, I return True; otherwise, I return False.

Then I moved to the editorial

image
@Ifihan
Ifihan / main.md
Created May 19, 2025 18:31
Type of Triangle

Question

Approach

I started by sorting the sides so I could easily apply the triangle inequality check, which states that the sum of the two smaller sides must be greater than the largest side. If this condition fails, it's not a valid triangle. Otherwise, I checked the type: if all sides are equal, it's equilateral; if exactly two sides are equal, it's isosceles; and if all sides are different, it's scalene.

Implementation

class Solution:
 def triangleType(self, nums: List[int]) -> str: