From 7ebe0fb5c041bc7fff50ba0aa2e686ec003c8822 Mon Sep 17 00:00:00 2001 From: MuraliDharan7 Date: Thu, 13 Jun 2024 12:57:18 +0530 Subject: [PATCH] binary search problems added --- .../01-BS-on-1D-Array.md | 19 +++ .../binary-search-problems/02-BS-Medium.md | 21 +++ .../binary-search-problems/03-BS-Hard.md | 8 + .../binary-search-problems/_category_.json | 8 + ...7-letter-combinations-of-a-phone-number.md | 140 ++++++++++-------- 5 files changed, 131 insertions(+), 65 deletions(-) create mode 100644 dsa-problems/binary-search-problems/01-BS-on-1D-Array.md create mode 100644 dsa-problems/binary-search-problems/02-BS-Medium.md create mode 100644 dsa-problems/binary-search-problems/03-BS-Hard.md create mode 100644 dsa-problems/binary-search-problems/_category_.json diff --git a/dsa-problems/binary-search-problems/01-BS-on-1D-Array.md b/dsa-problems/binary-search-problems/01-BS-on-1D-Array.md new file mode 100644 index 000000000..2d3cd634a --- /dev/null +++ b/dsa-problems/binary-search-problems/01-BS-on-1D-Array.md @@ -0,0 +1,19 @@ + +| Problem | Difficulty Level | Problem Link | +|-----------------------------------------------------------|------------------|------------------------------------------------------------| +| [Binary Search to find X in sorted array](https://www.geeksforgeeks.org/binary-search/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/binary-search-1587115620/1) | +| [Implement Lower Bound](https://www.geeksforgeeks.org/implement-lower-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-lower-bound/1) | +| [Implement Upper Bound](https://www.geeksforgeeks.org/implement-upper-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-upper-bound/1) | +| [Search Insert Position](https://www.geeksforgeeks.org/search-insert-position/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/search-insert-position-of-k-in-a-sorted-array/1) | +| [Floor/Ceil in Sorted Array](https://www.geeksforgeeks.org/floor-and-ceil-from-a-sorted-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1) | +| [Find the first or last occurrence of a given number in a sorted array](https://www.geeksforgeeks.org/find-first-and-last-positions-of-an-element-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/first-and-last-occurrences-of-x/0) | +| [Count occurrences of a number in a sorted array with duplicates](https://www.geeksforgeeks.org/count-number-of-occurrences-or-frequency-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/number-of-occurrence/0) | +| [Search in Rotated Sorted Array I](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-pivoted-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array/0) | +| [Search in Rotated Sorted Array II](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-rotated-array-2/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array-2/1) | +| [Find minimum in Rotated Sorted Array](https://www.geeksforgeeks.org/find-minimum-element-in-a-sorted-and-rotated-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array/0) | +| [Find out how many times has an array been rotated](https://www.geeksforgeeks.org/find-rotation-count-rotated-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/rotation/0) | +| [Single element in a Sorted Array](https://www.geeksforgeeks.org/find-the-element-that-appears-once-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/find-the-element-that-appears-once-in-sorted-array/0) | +| [Find peak element](https://www.geeksforgeeks.org/find-a-peak-in-a-given-array/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/peak-element/1) | + + +This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks. \ No newline at end of file diff --git a/dsa-problems/binary-search-problems/02-BS-Medium.md b/dsa-problems/binary-search-problems/02-BS-Medium.md new file mode 100644 index 000000000..422f0075c --- /dev/null +++ b/dsa-problems/binary-search-problems/02-BS-Medium.md @@ -0,0 +1,21 @@ + + +| Problem | Difficulty Level | Problem Link | +|-----------------------------------------------------------|------------------|------------------------------------------------------------| +| [Find square root of a number in log n](https://www.geeksforgeeks.org/square-root-of-an-integer/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/square-root-of-a-number/0) | +| [Find the Nth root of a number using binary search](https://www.geeksforgeeks.org/n-th-root-number-using-logarithm/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/nth-root-of-m/0) | +| [Koko Eating Bananas](https://www.geeksforgeeks.org/koko-eating-bananas/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/koko-eating-bananas/1) | +| [Minimum days to make M bouquets](https://www.geeksforgeeks.org/minimum-days-to-make-m-bouquets/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/minimum-days-to-make-m-bouquets/1) | +| [Find the smallest Divisor](https://www.geeksforgeeks.org/find-the-smallest-divisor-given-a-threshold/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/smallest-divisor/1) | +| [Capacity to Ship Packages within D Days](https://www.geeksforgeeks.org/capacity-to-ship-packages-within-d-days/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/capacity-to-ship-packages-within-d-days/1) | +| [Kth Missing Positive Number](https://www.geeksforgeeks.org/kth-missing-positive-number/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/kth-missing-positive-number/1) | +| [Aggressive Cows](https://www.geeksforgeeks.org/aggressive-cows-problem/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/aggressive-cows/0) | +| [Book Allocation Problem](https://www.geeksforgeeks.org/allocate-minimum-number-pages/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages/0) | +| [Split array - Largest Sum](https://www.geeksforgeeks.org/split-array-largest-sum/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/split-array-largest-sum/1) | +| [Painter's partition](https://www.geeksforgeeks.org/painters-partition-problem/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/the-painters-partition-problem/0) | +| [Minimize Max Distance to Gas Station](https://www.geeksforgeeks.org/minimize-max-distance-to-gas-station/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/minimize-max-distance-to-gas-station/1) | +| [Median of 2 sorted arrays](https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/median-of-2-sorted-arrays-of-different-sizes/1) | +| [Kth element of 2 sorted arrays](https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/k-th-element-of-two-sorted-arrays/0) | + + +This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks. \ No newline at end of file diff --git a/dsa-problems/binary-search-problems/03-BS-Hard.md b/dsa-problems/binary-search-problems/03-BS-Hard.md new file mode 100644 index 000000000..edc47a06f --- /dev/null +++ b/dsa-problems/binary-search-problems/03-BS-Hard.md @@ -0,0 +1,8 @@ + +| Problem | Difficulty Level | Problem Link | +|-----------------------------------------------------------|------------------|------------------------------------------------------------| +| [Find the row with maximum number of 1's](https://www.geeksforgeeks.org/find-the-row-with-maximum-number-of-1s/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1) | +| [Search in a 2D matrix](https://www.geeksforgeeks.org/search-in-a-row-wise-and-column-wise-sorted-2d-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1587115621/1) | +| [Search in a row and column wise sorted matrix](https://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1728441/1) | +| [Find Peak Element (2D Matrix)](https://www.geeksforgeeks.org/find-a-peak-element-in-a-2d-array/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/find-peak-element-2d/1) | +| [Matrix Median](https://www.geeksforgeeks.org/find-median-row-wise-sorted-matrix/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1) | diff --git a/dsa-problems/binary-search-problems/_category_.json b/dsa-problems/binary-search-problems/_category_.json new file mode 100644 index 000000000..590ef1ef9 --- /dev/null +++ b/dsa-problems/binary-search-problems/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Binary Search Problems", + "position": 3, + "link": { + "type": "generated-index", + "description": "Here You can Solve all of the Problems Which uses Binary Search." + } + } \ No newline at end of file diff --git a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md index 26cf596b7..76a29c9c4 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md +++ b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md @@ -3,21 +3,23 @@ id: letter-combinations-of-a-phone-number title: Letter Combinations of a Phone Number (LeetCode) sidebar_label: 0017 Letter Combinations of a Phone Number tags: - - Back Tracking - - Mapping - - String + - Back Tracking + - Mapping + - String description: The problem requires generating all letter combinations corresponding to given digits (2-9). The solution utilizes backtracking to explore all combinations efficiently, employing a recursive approach in Java. +sidebar_position: 17 --- ## Problem Description -| Problem Statement | Solution Link | LeetCode Profile | -| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | -| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) | +| Problem Statement | Solution Link | LeetCode Profile | +| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- | +| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) | ### Problem Description ## Problem Statement: + Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. ### Examples @@ -32,7 +34,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - **Input:** `digits = ""` - **Output:** `[]` - #### Example 3 - **Input:** `2` @@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter ### Approach 1. **Mapping Digits to Letters:** + - Define a mapping of digits to their corresponding letters, similar to telephone buttons. 2. **Backtracking Function:** + - Define a recursive backtracking function to generate all possible combinations. - The function takes four parameters: - `index`: The current index in the digits string. @@ -59,6 +62,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - After the recursive call, we remove the last character from the combination (backtracking). 3. **Base Case:** + - If the length of the current combination is equal to the length of the input digits string, we add the combination to the result list. 4. **Main Function:** @@ -153,6 +157,7 @@ public class Solution { ``` #### CPP: + ```cpp #include #include @@ -208,40 +213,41 @@ int main() { ``` #### JavaScript + ```js /** * @param {string} digits * @return {string[]} */ -var letterCombinations = function(digits) { - if (digits.length === 0) return []; - - const digitToLetters = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' - }; - - const combinations = []; - - const backtrack = (index, path) => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } - }; - - backtrack(0, ''); - return combinations; +var letterCombinations = function (digits) { + if (digits.length === 0) return []; + + const digitToLetters = { + 2: "abc", + 3: "def", + 4: "ghi", + 5: "jkl", + 6: "mno", + 7: "pqrs", + 8: "tuv", + 9: "wxyz", + }; + + const combinations = []; + + const backtrack = (index, path) => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } + }; + + backtrack(0, ""); + return combinations; }; // Example usage: @@ -249,39 +255,40 @@ console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf" ``` #### TypeScript + ```ts class Solution { - private digitToLetters: { [key: string]: string } = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' + private digitToLetters: { [key: string]: string } = { + "2": "abc", + "3": "def", + "4": "ghi", + "5": "jkl", + "6": "mno", + "7": "pqrs", + "8": "tuv", + "9": "wxyz", + }; + + letterCombinations(digits: string): string[] { + const combinations: string[] = []; + + const backtrack = (index: number, path: string): void => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = this.digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } }; - letterCombinations(digits: string): string[] { - const combinations: string[] = []; - - const backtrack = (index: number, path: string): void => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = this.digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } - }; - - if (digits.length !== 0) { - backtrack(0, ''); - } - - return combinations; + if (digits.length !== 0) { + backtrack(0, ""); } + + return combinations; + } } // Example usage: @@ -294,9 +301,11 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd", Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking: 1. **Define a mapping of digits to letters:** + - Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad. 2. **Define a backtracking function:** + - The function will take the following parameters: - `index`: The current index in the digits string. - `path`: The current combination of letters. @@ -305,6 +314,7 @@ Here's a step-by-step algorithm for generating all possible letter combinations - After the recursive call, remove the last character from the combination (backtracking). 3. **Base Case:** + - If the length of the current combination is equal to the length of the input digits string, add the combination to the result list. 4. **Main Function:** @@ -312,4 +322,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations - Call the backtracking function with the initial index set to 0 and an empty string as the initial combination. - Return the list of combinations. -This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. \ No newline at end of file +This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.