From ea6d57626504a799e9b38b010a17697a077e2da2 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 13:50:14 +0530 Subject: [PATCH 1/3] Added solution for Search Insert Position --- .../0000-0099/0035-search-insert-position.md | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md b/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md new file mode 100644 index 000000000..063617c1d --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md @@ -0,0 +1,136 @@ +--- +id: search-insert-position +title: Search Insert Position +difficulty: Easy +sidebar_label: 0038-SearchInsertPosition +tags: + - Array + - Binary Search +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Merge Two Sorted Lists](https://leetcode.com/problems/search-insert-position/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-insert-position/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | + +## Problem Description + +Given a sorted array `nums` of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. + +You must write an algorithm with O(log n) runtime complexity. + +### Examples + +#### Example 1: + +- **Input:** + - `nums = [1,3,5,6]` + - `target = 5` +- **Output:** `2` + +#### Example 2: + +- **Input:** + - `nums = [1,3,5,6]` + - `target = 2` +- **Output:** `1` + +#### Example 3: + +- **Input:** + - `nums = [1,3,5,6]` + - `target = 7` +- **Output:** `4` + +### Constraints + +- `1 <= `nums.length` <= 10^4` +- `-10^4 <= `nums[i]` <= 10^4` +- `nums` contains distinct values sorted in ascending order. +- `-10^4 <= `target` <= 10^4` + +### Approach + +To solve the problem with O(log n) runtime complexity, we can use binary search to find the insertion position of the target value. + +1. **Binary Search:** + - Start with low = 0 and high = length of `nums` - 1. + - While `low <= high`, compute mid as (low + high) / 2. + - If the target value is equal to the value at index mid, return mid. + - If the target value is less than the value at index mid, set high = mid - 1. + - If the target value is greater than the value at index mid, set low = mid + 1. + - After the loop, if the target value is not found, return low (or high + 1). + +### Solution Code + +#### Python + +``` +class Solution(object): + def searchInsert(self, nums, target): + left, right = 0, len(nums) - 1 + + while left <= right: + mid = left + (right - left) // 2 + if nums[mid] == target: + return mid + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + + return left + +``` + +#### Java + +``` +class Solution { + public int searchInsert(int[] nums, int target) { + int low = 0, high = nums.length - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return low; + } +} +``` + +#### C++ + +``` +class Solution { +public: + int searchInsert(vector& nums, int target) { + int low = 0, high = nums.size() - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return low; + } +}; +``` + +### Conclusion + +The above solution efficiently finds the insertion position of a target value in a sorted array using binary search. It achieves a runtime complexity of O(log n), providing an optimal solution to the problem. From 27c14c64e4e7b35196300803c8e28c957c044257 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 13:57:32 +0530 Subject: [PATCH 2/3] S --- .../0000-0099/0035-search-insert-position.md | 136 ------------------ 1 file changed, 136 deletions(-) delete mode 100644 dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md b/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md deleted file mode 100644 index 063617c1d..000000000 --- a/dsa-solutions/lc-solutions/0000-0099/0035-search-insert-position.md +++ /dev/null @@ -1,136 +0,0 @@ ---- -id: search-insert-position -title: Search Insert Position -difficulty: Easy -sidebar_label: 0038-SearchInsertPosition -tags: - - Array - - Binary Search ---- - -## Problem Description - -| Problem Statement | Solution Link | LeetCode Profile | -| :---------------- | :------------ | :--------------- | -| [Merge Two Sorted Lists](https://leetcode.com/problems/search-insert-position/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-insert-position/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | - -## Problem Description - -Given a sorted array `nums` of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. - -You must write an algorithm with O(log n) runtime complexity. - -### Examples - -#### Example 1: - -- **Input:** - - `nums = [1,3,5,6]` - - `target = 5` -- **Output:** `2` - -#### Example 2: - -- **Input:** - - `nums = [1,3,5,6]` - - `target = 2` -- **Output:** `1` - -#### Example 3: - -- **Input:** - - `nums = [1,3,5,6]` - - `target = 7` -- **Output:** `4` - -### Constraints - -- `1 <= `nums.length` <= 10^4` -- `-10^4 <= `nums[i]` <= 10^4` -- `nums` contains distinct values sorted in ascending order. -- `-10^4 <= `target` <= 10^4` - -### Approach - -To solve the problem with O(log n) runtime complexity, we can use binary search to find the insertion position of the target value. - -1. **Binary Search:** - - Start with low = 0 and high = length of `nums` - 1. - - While `low <= high`, compute mid as (low + high) / 2. - - If the target value is equal to the value at index mid, return mid. - - If the target value is less than the value at index mid, set high = mid - 1. - - If the target value is greater than the value at index mid, set low = mid + 1. - - After the loop, if the target value is not found, return low (or high + 1). - -### Solution Code - -#### Python - -``` -class Solution(object): - def searchInsert(self, nums, target): - left, right = 0, len(nums) - 1 - - while left <= right: - mid = left + (right - left) // 2 - if nums[mid] == target: - return mid - elif nums[mid] < target: - left = mid + 1 - else: - right = mid - 1 - - return left - -``` - -#### Java - -``` -class Solution { - public int searchInsert(int[] nums, int target) { - int low = 0, high = nums.length - 1; - - while (low <= high) { - int mid = low + (high - low) / 2; - if (nums[mid] == target) { - return mid; - } else if (nums[mid] < target) { - low = mid + 1; - } else { - high = mid - 1; - } - } - - return low; - } -} -``` - -#### C++ - -``` -class Solution { -public: - int searchInsert(vector& nums, int target) { - int low = 0, high = nums.size() - 1; - - while (low <= high) { - int mid = low + (high - low) / 2; - if (nums[mid] == target) { - return mid; - } else if (nums[mid] < target) { - low = mid + 1; - } else { - high = mid - 1; - } - } - - return low; - } -}; -``` - -### Conclusion - -The above solution efficiently finds the insertion position of a target value in a sorted array using binary search. It achieves a runtime complexity of O(log n), providing an optimal solution to the problem. From a8fde42c30fdaed141c1c6346b14e174af76b89a Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 17:20:43 +0530 Subject: [PATCH 3/3] Added solution for Climbing Stairs --- .../0000-0099/0070-Climbing-Stairs.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0070-Climbing-Stairs.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0070-Climbing-Stairs.md b/dsa-solutions/lc-solutions/0000-0099/0070-Climbing-Stairs.md new file mode 100644 index 000000000..db0fe68c3 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0070-Climbing-Stairs.md @@ -0,0 +1,113 @@ +--- +id: climbing-stairs +title: Climbing Stairs (LeetCode) +difficulty: Easy +sidebar_label: 0070-ClimbingStairs +topics: + - Dynamic Programming +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Merge Two Sorted Lists](https://leetcode.com/problems/climbing-stairs/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/climbing-stairs/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | + +## Problem Description + +You are climbing a staircase. It takes `n` steps to reach the top. + +Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? + +### Examples + +#### Example 1: + +- **Input:** `n = 2` +- **Output:** `2` +- **Explanation:** There are two ways to climb to the top: + 1. 1 step + 1 step + 2. 2 steps + +#### Example 2: + +- **Input:** `n = 3` +- **Output:** `3` +- **Explanation:** There are three ways to climb to the top: + 1. 1 step + 1 step + 1 step + 2. 1 step + 2 steps + 3. 2 steps + 1 step + +### Constraints: + +- `1 <= n <= 45` + +### Approach + +To find the number of distinct ways to climb to the top of the staircase, we can use dynamic programming. + +1. Initialize an array `dp` of size `n+1` to store the number of distinct ways to reach each step. +2. Set `dp[0] = 1` and `dp[1] = 1` since there's only one way to reach the first step and the second step. +3. Iterate from `2` to `n`, updating `dp[i]` as the sum of `dp[i-1]` and `dp[i-2]`. +4. Finally, return `dp[n]`, which represents the number of distinct ways to reach the `n`-th step. + +### Solution Code + +#### Python + +``` +class Solution(object): + def climbStairs(self, n): + if n <= 2: + return n + dp = [0] * (n + 1) + dp[1] = 1 + dp[2] = 2 + + for i in range(3, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + return dp[n] +``` + +#### C++ + +``` +class Solution { +public: + int climbStairs(int n) { + if (n <= 2) { + return n; + } + vector dp(n + 1); + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; ++i) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +}; +``` + +#### Java + +``` +class Solution { + public int climbStairs(int n) { + if (n <= 2) { + return n; + } + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; ++i) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +} +``` + +### Conclusion + +The "Climbing Stairs" problem can be efficiently solved using dynamic programming, where the number of distinct ways to reach each step is calculated iteratively. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem. \ No newline at end of file