Leetcode 154: Find minimum in rotated sorted array

Below is my solution for the leetcode 154: Find minimum in rotated sorted array problem Intuition Fact is, that you cannot solve this question in O(log n) time. The reason is because of duplicates. Consider a situation like: $$ [2,2,2,2,1,2,2] $$ where mid is at 3 and the minium number here is clearly $$1$$. But our binary search algorithm will not be able to figure out in which direction it should go, since starting, ending and middle values are all same. In this case the best we can do is increment mid which makes the worst running time: O(n) ...

July 16, 2025 · 2 min · abdul

Leetcode 315: Count of Smaller Numbers After Self

Here’s my solution to the Leetcode 315: Count of Smaller Numbers After Self problem using standard merge sort. I just change one line to count while merge procedure. Solution Let’s build the solution step by step. Input: nums = $$[5,2,6,1]$$ First, turn the numbers into [number, index] tuple. So it looks like: array = $$ [ [ 5, 0 ], [ 2, 1 ], [ 6, 2 ], [ 1, 3 ] ] $$ ...

July 16, 2025 · 9 min · abdul

Leetcode 306: Additive number

Here’s my explanation for the leetcode 306: Additive Number problem Additive Number Problem Problem Recap An additive number is a string of digits where the sequence of numbers formed by splitting the string satisfies the condition that each number (after the first two) is the sum of the two preceding numbers. Examples: "112358" is additive because the sequence is 1, 1, 2, 3, 5, 8, and: 1 + 1 = 2 1 + 2 = 3 2 + 3 = 5 3 + 5 = 8 "199100199" is additive because the sequence is 1, 99, 100, 199, and: ...

July 16, 2025 · 5 min · abdul

All possible solutions to longest increasing subsequence problem: leetcode 300

Here are all possible solutions I could come up with for the longest increasing subsequence problem. leetcode 300 Approach 1: Generate all possible increasing subsequences We will keep track of a subsequence in an array named cur_subsequence or cur_sub. For each element (a[i]) we have the following 2 options: Add the element to the end of current subsequence. Note: The current element can only be included if either the current subsequence is empty or the last element of the current subsequence is smaller than the current element. This is important to maintain the increasing subsequence property. ...

July 16, 2025 · 9 min · abdul

Leetcode 70: Climbing Stairs

Problem link: leetcode 70: climbing stairs Intuition You need to climb n stairs, taking 1 or 2 stairs at a time. If n = 2, you can climb like: $$1+1 = 2\times (1) + 0 \times(2)$$ $$2 = 0 \times (1) + 1\times(2) $$ if n = 3, you can climb like: $$1+1+1 = 3\times(1) + 0\times(2)$$ $$1+2 = 1\times(1) + 1\times(2)$$ $$2+1 = 1\times(1) + 1\times(2) $$ Basically you first need to decide how many steps of size 1 will you take and how many of size 2 do you need: ...

July 16, 2025 · 3 min · abdul