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 April 9, 2025 17:25
Minimum Operations to Make Array Values Equal to K

Question

Approach

I first checked whether it is even possible to reach k — if there's any number in nums less than k, it's impossible, so I returned -1. Then, starting from the maximum element in nums, I processed all unique values in descending order. At each step, if the current value is greater than k and not all values above it are the same, it’s not a valid h, so I skip it. When I find a valid h, I count it as an operation and continue until we reach k.

Implementation

class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created April 8, 2025 23:24
Minimum Number of Operations to Make Elements in Array Distinct

Question

Approach

I started from index 0 and checked for duplicates using a frequency counter. If duplicates existed, I removed the first 3 elements (or all if fewer than 3) and counted one operation. I repeated this until all remaining elements were distinct, or the array was empty.

Implementation

class Solution:
 def minimumOperations(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created April 7, 2025 21:53
Partition Equal Subset Sum

Question

Approach

I first calculated the total sum of the array. If the sum is odd, it's impossible to split it equally, so I returned False. Otherwise, I set the target as total_sum // 2. I then used a DP set to track all reachable subset sums. For each number in the array, I updated the set with new possible sums by adding it to existing ones. If the target is reached, I returned True. If not, I returned False.

Implementation

class Solution:
    def canPartition(self, nums: List[int]) -> bool:
@Ifihan
Ifihan / main.md
Created April 6, 2025 22:28
Largest Divisible Subset

Question

Approach

First, I sorted the input list nums to ensure that for any two elements nums[i] and nums[j], where i > j, nums[i] % nums[j] is easy to check. I then used a dp array where dp[i] stores the size of the largest divisible subset ending at index i. I also tracked the prev index to reconstruct the subset at the end. I returned the largest subset by backtracking from the maximum value in dp.

Implementation

class Solution:
    def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
@Ifihan
Ifihan / main.md
Created April 5, 2025 22:51
Sum of All Subset XOR Totals

Question

Approach

I used a recursive DFS to explore all possible subsets of the array. At each index, I either included the element in the XOR or skipped it. Once I reached the end of the array, I returned the XOR total of that subset. Summing all these gave me the final result.

Implementation

class Solution:
 def subsetXORSum(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created April 4, 2025 22:49
Lowest Common Ancestor of Deepest Leaves

Question

Approach

I solved this using a post-order DFS. For every node, I calculated the max depth of its children and tracked the lowest common ancestor of the deepest nodes. When both sides had the same depth, that node was the LCA. The recursion naturally bubbled up the right LCA to return.

Implementation

# Definition for a binary tree node.
# class TreeNode:
@Ifihan
Ifihan / main.md
Created April 3, 2025 22:33
Maximum Value of an Ordered Triplet II

Question

Approach

To solve this, I tracked the maximum value seen before index j to simulate the best nums[i] where i < j. Then, for each possible k > j, I computed the value of the expression and updated the result if it was higher. I returned the maximum such value, or 0 if all were negative.

Implementation

class Solution:
 def maximumTripletValue(self, nums: List[int]) -&gt; int:
@Ifihan
Ifihan / main.md
Created April 2, 2025 22:32
Maximum Value of an Ordered Triplet I

Question

Approach

I iterated over all possible middle indices j and, for each, found the best i < j that gives the maximum nums[i]. Then, I scanned for all k > j and computed the triplet value. I kept track of the maximum of these values, and if none were positive, I returned 0.

Implementation

class Solution:
 def maximumTripletValue(self, nums: List[int]) -&gt; int:
@Ifihan
Ifihan / main.md
Created April 1, 2025 22:43
Solving Questions With Brainpower

Question

Approach

To solve this problem, I used dynamic programming, working from the end of the list backwards. For each question, I had two choices:

  • Skip it and take the max points from the next question.
  • Solve it, earn points[i], and skip the next brainpower[i] questions.

I stored the best decision at each index and returned the result from index 0.

@Ifihan
Ifihan / main.md
Created March 31, 2025 22:11
Put Marbles in Bags