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 8b135b5f20f4428e5c6bac6e1c9dcf10d5e0f682 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 16:58:30 +0530 Subject: [PATCH 3/3] Added solution for Add Binary --- .../lc-solutions/0000-0099/0067-Add-Binary.md | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0067-Add-Binary.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0067-Add-Binary.md b/dsa-solutions/lc-solutions/0000-0099/0067-Add-Binary.md new file mode 100644 index 000000000..5a474d060 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0067-Add-Binary.md @@ -0,0 +1,121 @@ +--- +id: add-binary +title: Add Binary (LeetCode) +difficulty: Easy +sidebar_label: 0067-AddBinary +topics: + - Math + - String +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Merge Two Sorted Lists](https://leetcode.com/problems/add-binary/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/add-binary/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | + +## Problem Description + +Given two binary strings `a` and `b`, return their sum as a binary string. + +### Examples + +#### Example 1: + +- **Input:** `a = "11"`, `b = "1"` +- **Output:** `"100"` +- **Explanation:** The sum of binary strings `11` and `1` is `100`. + +#### Example 2: + +- **Input:** `a = "1010"`, `b = "1011"` +- **Output:** `"10101"` +- **Explanation:** The sum of binary strings `1010` and `1011` is `10101`. + +### Constraints: + +- `1 <= a.length, b.length <= 10^4` +- `a` and `b` consist only of `'0'` or `'1'` characters. +- Each string does not contain leading zeros except for the zero itself. + +### Approach + +To add two binary strings `a` and `b`, we can follow these steps: + +1. Initialize variables `result` and `carry` to store the result and carry, respectively. +2. Start from the last digit of both strings and move towards the beginning. +3. Add the corresponding digits from `a` and `b` along with the carry and update the result accordingly. +4. If the sum exceeds 1, set the carry to 1; otherwise, set it to 0. +5. After processing all digits, if there's still a carry left, append it to the result. +6. Reverse the result string to get the final sum. + +### Solution Code + +#### Python + +``` +class Solution(object): + def addBinary(self, a, b): + return bin(int(a, 2) + int(b, 2))[2:] +``` + +#### C++ + +``` +class Solution { +public: + string addBinary(string a, string b) { + string result; + int carry = 0; + int i = a.length() - 1, j = b.length() - 1; + + while (i >= 0 || j >= 0) { + int digit_a = (i >= 0) ? a[i] - '0' : 0; + int digit_b = (j >= 0) ? b[j] - '0' : 0; + int current_sum = digit_a + digit_b + carry; + result = to_string(current_sum % 2) + result; + carry = current_sum / 2; + i--; + j--; + } + + if (carry) { + result = "1" + result; + } + + return result; + } +}; +``` + +#### Java + +``` +class Solution { + public String addBinary(String a, String b) { + StringBuilder result = new StringBuilder(); + int carry = 0; + int i = a.length() - 1, j = b.length() - 1; + + while (i >= 0 || j >= 0) { + int digit_a = (i >= 0) ? a.charAt(i) - '0' : 0; + int digit_b = (j >= 0) ? b.charAt(j) - '0' : 0; + int current_sum = digit_a + digit_b + carry; + result.insert(0, current_sum % 2); + carry = current_sum / 2; + i--; + j--; + } + + if (carry == 1) { + result.insert(0, '1'); + } + + return result.toString(); + } +} +``` + +### Conclusion + +The "Add Binary" problem can be efficiently solved by adding two binary strings digit by digit and considering the carry. 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