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
Last active October 2, 2025 22:39

Question

Approach

I drink all the initial bottles first, keeping track of empties. Then, as long as I have at least numExchange empties, I perform a single exchange: I trade the empties for one full bottle, drink it, update the empties, and increase numExchange by one since future exchanges are harder. I repeat this process until I can no longer exchange.

Implementation

class Solution:
 def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
@Ifihan
Ifihan / main.md
Created October 1, 2025 22:33
Water Bottles

Question

Approach

I start by drinking all the initial bottles, keeping track of empties. While I have enough empties to exchange for a full one, I perform the exchange, drink those, and update my empty count. I continue until I can no longer exchange.

Implementation

class Solution:
 def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
@Ifihan
Ifihan / main.md
Created September 30, 2025 22:11
Find Triangular Sum of an Array

Question

Approach

I solve the triangular sum problem by recognizing that the process is equivalent to weighting each element in the array by a binomial coefficient. Instead of simulating all reductions, I compute the binomial coefficients for row n-1, multiply each nums[i] by its coefficient, sum them up, and take the result modulo 10.

Implementation

class Solution:
 def triangularSum(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created September 29, 2025 21:53
Minimum Score Triangulation of Polygon

Question

Approach

I treat the polygon as an interval [i, j] of vertices. If I want to triangulate that section, I pick a third vertex k between i and j to form triangle (i, k, j). The cost is values[i] * values[k] * values[j] + dp[i][k] + dp[k][j]. I compute the minimum across all choices of k.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created September 28, 2025 22:53
Largest Perimeter Triangle

Question

Approach

I sort the array in nonincreasing order and then scan triples from largest to smallest. For any three sides a >= b >= c, the triangle inequality reduces to a < b + c. So I check consecutive triples in the sorted list; the first triple that satisfies nums[i] < nums[i+1] + nums[i+2] gives the maximum perimeter a + b + c. If no triple works, return 0.

Implementation

class Solution:
 def largestPerimeter(self, nums: List[int]) -&gt; int:
@Ifihan
Ifihan / main.md
Created September 27, 2025 22:36
Largest Triangle Area

Question

Approach

Brute force way. I iterate over all combinations of three distinct points (since (n \le 50) this (O(n^3)) brute-force is fine). For each triple I compute the triangle area using the cross product formula: for points (A(x_1,y_1), B(x_2,y_2), C(x_3,y_3)) the area is (\tfrac{1}{2}\big| (x_2-x_1)(y_3-y_1) - (x_3-x_1)(y_2-y_1)\big|). I track the maximum area seen and return it.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created September 26, 2025 22:45
Valid Triangle Number

Question

Approach

I solve this by first sorting the array. For each possible largest side nums[k], I use two pointers (i at start, j just before k) to count valid triangles. If nums[i] + nums[j] > nums[k], then all pairs from i to j-1 with j and k are valid, so I add (j - i) to the count and move j left. Otherwise, I move i right.

Implementation

class Solution:
 def triangleNumber(self, nums: List[int]) -&gt; int:
@Ifihan
Ifihan / main.md
Created September 25, 2025 15:37
Triangle

Question

Approach

I solve it with dynamic programming from the bottom up. I keep a 1D array dp representing the minimum path sums from the current row down to the bottom; I initialize dp to the last row of the triangle. Then I iterate upward row by row: for each element triangle[i][j] I update dp[j] = triangle[i][j] + min(dp[j], dp[j+1]). After processing the top row, dp[0] holds the answer.

Implementation

class Solution:
 def minimumTotal(self, triangle: List[List[int]]) -&gt; int:
@Ifihan
Ifihan / main.md
Created September 24, 2025 21:47
Fraction to Recurring Decimal

Question

Approach

  1. Handle sign: If numerator and denominator have different signs, the result should start with -. Work with absolute values afterward.
  2. Integer part: Perform integer division (numerator // denominator) to get the whole number part.
  3. Remainder handling: If the remainder is zero, just return the integer part as the answer.
  4. Fractional part:
  • Start simulating long division: multiply remainder by 10, divide by denominator, append quotient to result.
@Ifihan
Ifihan / main.md
Last active September 24, 2025 21:40
Compare Version Numbers

Question

Approach

I solve this by splitting both version strings into lists of integers, padding the shorter one with zeros, and then comparing element by element.

Implementation

class Solution:
 def compareVersion(self, version1: str, version2: str) -&gt; int: