From 3f2cd028a490458254db29a6bc1514157f093eba Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Thu, 18 Jan 2024 02:35:57 +0530 Subject: [PATCH 01/13] Create Question.md --- .../Question.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Day-18/q2: Search in Rotated Sorted Array/Question.md diff --git a/Day-18/q2: Search in Rotated Sorted Array/Question.md b/Day-18/q2: Search in Rotated Sorted Array/Question.md new file mode 100644 index 00000000..401f4018 --- /dev/null +++ b/Day-18/q2: Search in Rotated Sorted Array/Question.md @@ -0,0 +1,32 @@ +## Search in Rotated Sorted Array +There is an integer array `nums` sorted in ascending order (with distinct values). + +Prior to being passed to your function, `nums` is possibly rotated at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed). +For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. + +Given the array `nums` after the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, +or -1 if it is not in `nums`. + +You must write an algorithm with O(log n) runtime complexity. + +### Example 1: + +Input: `nums` = [4,5,6,7,0,1,2], `target` = 0 +
Output: 4 + +### Example 2: +Input: `nums` = [4,5,6,7,0,1,2], `target` = 3 +
Output: -1 + +### Example 3: + +Input: `nums` = [1], `target` = 0 +
Output: -1 + +Constraints: + +`1 <= nums.length <= 5000` +
`-10^4 <= nums[i] <= 10^4` +
All values of `nums` are unique. +`nums` is an ascending array that is possibly rotated. +
`-10^4 <= target <= 10^4` From 34f30bf8b4007d4813f74a71f6abb965f45f9e03 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Thu, 18 Jan 2024 02:39:08 +0530 Subject: [PATCH 02/13] Day18-q2 solution added in python #409 --- .../Tech-neophyte--p.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md diff --git a/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md b/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md new file mode 100644 index 00000000..a44a87b3 --- /dev/null +++ b/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md @@ -0,0 +1,23 @@ +## Python code: +``` +class Solution { + public int search(int[] A, int B) { + int l = 0; + int r = A.length-1; + int m; + while(l<=r){ + m = (l+r)/2; + if(A[m] == B) return m; + if(A[m]>=A[0]){ + if(B>=A[0] && B<=A[m]) r = m-1; + else l = m+1; + }else{ + if(B>=A[m] && B<=A[A.length-1]) l = m+1; + else r = m-1; + } + + } + return -1; + } +} +``` From ab775b2fcbcefc863a32cb6604e300ffc6803e6c Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sat, 20 Jan 2024 13:50:34 +0530 Subject: [PATCH 03/13] Create day-21-q2 --- .../Tech-neophyte--c.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Day-21/q2 : Container With Most Water/Tech-neophyte--c.md diff --git a/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md b/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md new file mode 100644 index 00000000..d74b2b74 --- /dev/null +++ b/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md @@ -0,0 +1,23 @@ +``` +class Solution { +public: + int maxArea(vector& height) { + int left = 0; + int right = height.size() - 1; + int maxArea = 0; + + while (left < right) { + int currentArea = min(height[left], height[right]) * (right - left); + maxArea = max(maxArea, currentArea); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } +}; +``` From c916a60a758d3c75bd50e0d2035cfcf19ebe8e86 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sat, 20 Jan 2024 13:54:23 +0530 Subject: [PATCH 04/13] Update day21-Q2 #421 --- Day-21/q2 : Container With Most Water/Tech-neophyte--c.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md b/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md index d74b2b74..37d649c0 100644 --- a/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md +++ b/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md @@ -1,3 +1,7 @@ +## Approach: +
1. Initiate two pointers, one from start (left) and one from end (right). +
2. While left Date: Sun, 21 Jan 2024 19:22:06 +0530 Subject: [PATCH 05/13] Create Tech-neophyte--c.md --- Day 22/q1/Tech-neophyte--c.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Day 22/q1/Tech-neophyte--c.md diff --git a/Day 22/q1/Tech-neophyte--c.md b/Day 22/q1/Tech-neophyte--c.md new file mode 100644 index 00000000..9d128116 --- /dev/null +++ b/Day 22/q1/Tech-neophyte--c.md @@ -0,0 +1,25 @@ +## Approach: + +
1. Create a hashmap object to count the occurrences of each element in the given array nums. Initialize a variable ans = 0 to keep track of the minimum number of operations required. +
2. Check if frequency is equal to 1. If yes, return -1, as it is not possible to perform the required operations on a single element. +Else increment the answer ans by the ceiling division of c by 3. +
3. After iterating through all counts in the Counter, return the final value of ans as the minimum number of operations required to empty the array. +``` +class Solution { +public: + int minOperations(vector& nums) + { + unordered_map counter; + int ans = 0; + for (int i=0;i Date: Sun, 21 Jan 2024 19:34:18 +0530 Subject: [PATCH 06/13] day 23-q1 cpp added #446 --- day-23/q1/Tech-neophyte--c.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 day-23/q1/Tech-neophyte--c.md diff --git a/day-23/q1/Tech-neophyte--c.md b/day-23/q1/Tech-neophyte--c.md new file mode 100644 index 00000000..332922ed --- /dev/null +++ b/day-23/q1/Tech-neophyte--c.md @@ -0,0 +1,23 @@ +## Cpp code: +``` +class Solution { +public: + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + for(int i=1;i0){ + miniv=min(miniv, matrix[i-1][j-1]); + } + if(j Date: Mon, 22 Jan 2024 00:27:03 +0530 Subject: [PATCH 07/13] day 19-q3 added in python #418 --- .../Tech-neophyte--p.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Day-19/q3-Letter Combinations of a Phone Number/Tech-neophyte--p.md diff --git a/Day-19/q3-Letter Combinations of a Phone Number/Tech-neophyte--p.md b/Day-19/q3-Letter Combinations of a Phone Number/Tech-neophyte--p.md new file mode 100644 index 00000000..21d276eb --- /dev/null +++ b/Day-19/q3-Letter Combinations of a Phone Number/Tech-neophyte--p.md @@ -0,0 +1,34 @@ +## Approach: +
1. Initialize a Mapping: Create a dictionary that maps each digit from 2 to 9 to their corresponding letters on a telephone's buttons. +
2. Base Case: If the input string digits is empty, return an empty list. +
3. Iteratively make Combinations: Start with an empty combination in a list and iteratively build the combinations by processing each digit in the input string. +For each existing combination, append each corresponding letter for the current digit, building new combinations. +
4. Result: Return the generated combinations as the final result. +## Python code: +``` +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + if not digits: + return [] + + phone_map = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' + } + combinations=[""] + for i in digits: + new_comb=[] + for j in combinations: + for l in phone_map[i]: + new_comb.append(j+l) + combinations=new_comb + return combinations + + +``` From 98167d502217d49c59503fe95ab2dcc0d43a52e4 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Mon, 22 Jan 2024 00:42:46 +0530 Subject: [PATCH 08/13] Create Tech--neophyte--pc.md --- Day-21/Q3/Tech--neophyte--pc.md | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Day-21/Q3/Tech--neophyte--pc.md diff --git a/Day-21/Q3/Tech--neophyte--pc.md b/Day-21/Q3/Tech--neophyte--pc.md new file mode 100644 index 00000000..fa621bc8 --- /dev/null +++ b/Day-21/Q3/Tech--neophyte--pc.md @@ -0,0 +1,65 @@ +## Python code: +``` +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + row = [1]*n + + for i in range(m-1): + newRow = [1]*n + + for j in range(n-2, -1, -1): + newRow[j] = newRow[j+1] + row[j] + row = newRow + + return row[0] +``` +## cpp code: Using dp +``` +class Solution { +public: + int uniquePaths(int m, int n) { + + vector> dp(m, vector(n, 0)); + + + for (int i = 0; i < m; ++i) { + dp[i][0] = 1; + } + for (int j = 0; j < n; ++j) { + dp[0][j] = 1; + } + + + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + + return dp[m - 1][n - 1]; + } +}; +``` +## Cpp code: Using combinations: +``` +class Solution { +public: + int nCr(int n, int r) { + if (r > n) return 0; + if (r == 0 || n == r) return 1; + double res = 0; + for (int i = 0; i < r; i++) { + res += log(n-i) - log(i+1); + } + return (int)round(exp(res)); + } + + int uniquePaths(int m, int n) { + int t = (m-1) + (n-1); + + return nCr(t,m-1); + + } +}; +``` From 3f9f163a64c01c4e2ee5c3006fe9ee0cd6657cbf Mon Sep 17 00:00:00 2001 From: Bhumika Gupta <95757762+bh-g@users.noreply.github.com> Date: Tue, 23 Jan 2024 01:56:38 +0530 Subject: [PATCH 09/13] Rename Tech-neophyte--c.md to Tech-neophyte--c.md --- .../Tech-neophyte--c.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Day 22/q1 => Day-22/q1: Minimum Number of Operations to Make Array Empty}/Tech-neophyte--c.md (100%) diff --git a/Day 22/q1/Tech-neophyte--c.md b/Day-22/q1: Minimum Number of Operations to Make Array Empty/Tech-neophyte--c.md similarity index 100% rename from Day 22/q1/Tech-neophyte--c.md rename to Day-22/q1: Minimum Number of Operations to Make Array Empty/Tech-neophyte--c.md From 88ef5a1306175bc9edc802ded8121f711d1446e2 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:01:35 +0530 Subject: [PATCH 10/13] Delete Day-18/q2: Search in Rotated Sorted Array directory --- .../Question.md | 32 ------------------- .../Tech-neophyte--p.md | 23 ------------- 2 files changed, 55 deletions(-) delete mode 100644 Day-18/q2: Search in Rotated Sorted Array/Question.md delete mode 100644 Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md diff --git a/Day-18/q2: Search in Rotated Sorted Array/Question.md b/Day-18/q2: Search in Rotated Sorted Array/Question.md deleted file mode 100644 index 401f4018..00000000 --- a/Day-18/q2: Search in Rotated Sorted Array/Question.md +++ /dev/null @@ -1,32 +0,0 @@ -## Search in Rotated Sorted Array -There is an integer array `nums` sorted in ascending order (with distinct values). - -Prior to being passed to your function, `nums` is possibly rotated at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed). -For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. - -Given the array `nums` after the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, -or -1 if it is not in `nums`. - -You must write an algorithm with O(log n) runtime complexity. - -### Example 1: - -Input: `nums` = [4,5,6,7,0,1,2], `target` = 0 -
Output: 4 - -### Example 2: -Input: `nums` = [4,5,6,7,0,1,2], `target` = 3 -
Output: -1 - -### Example 3: - -Input: `nums` = [1], `target` = 0 -
Output: -1 - -Constraints: - -`1 <= nums.length <= 5000` -
`-10^4 <= nums[i] <= 10^4` -
All values of `nums` are unique. -`nums` is an ascending array that is possibly rotated. -
`-10^4 <= target <= 10^4` diff --git a/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md b/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md deleted file mode 100644 index a44a87b3..00000000 --- a/Day-18/q2: Search in Rotated Sorted Array/Tech-neophyte--p.md +++ /dev/null @@ -1,23 +0,0 @@ -## Python code: -``` -class Solution { - public int search(int[] A, int B) { - int l = 0; - int r = A.length-1; - int m; - while(l<=r){ - m = (l+r)/2; - if(A[m] == B) return m; - if(A[m]>=A[0]){ - if(B>=A[0] && B<=A[m]) r = m-1; - else l = m+1; - }else{ - if(B>=A[m] && B<=A[A.length-1]) l = m+1; - else r = m-1; - } - - } - return -1; - } -} -``` From 095f2ffd2d4595a984769f669b0dc5a824de3d5b Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:07:22 +0530 Subject: [PATCH 11/13] Delete Day-21/Q3/Tech--neophyte--pc.md and move to required folder. --- Day-21/Q3/Tech--neophyte--pc.md | 65 --------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 Day-21/Q3/Tech--neophyte--pc.md diff --git a/Day-21/Q3/Tech--neophyte--pc.md b/Day-21/Q3/Tech--neophyte--pc.md deleted file mode 100644 index fa621bc8..00000000 --- a/Day-21/Q3/Tech--neophyte--pc.md +++ /dev/null @@ -1,65 +0,0 @@ -## Python code: -``` -class Solution: - def uniquePaths(self, m: int, n: int) -> int: - row = [1]*n - - for i in range(m-1): - newRow = [1]*n - - for j in range(n-2, -1, -1): - newRow[j] = newRow[j+1] + row[j] - row = newRow - - return row[0] -``` -## cpp code: Using dp -``` -class Solution { -public: - int uniquePaths(int m, int n) { - - vector> dp(m, vector(n, 0)); - - - for (int i = 0; i < m; ++i) { - dp[i][0] = 1; - } - for (int j = 0; j < n; ++j) { - dp[0][j] = 1; - } - - - for (int i = 1; i < m; ++i) { - for (int j = 1; j < n; ++j) { - dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; - } - } - - - return dp[m - 1][n - 1]; - } -}; -``` -## Cpp code: Using combinations: -``` -class Solution { -public: - int nCr(int n, int r) { - if (r > n) return 0; - if (r == 0 || n == r) return 1; - double res = 0; - for (int i = 0; i < r; i++) { - res += log(n-i) - log(i+1); - } - return (int)round(exp(res)); - } - - int uniquePaths(int m, int n) { - int t = (m-1) + (n-1); - - return nCr(t,m-1); - - } -}; -``` From 94b2e020f447fae6d6c4327f71fdb78f2d7ab055 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:08:34 +0530 Subject: [PATCH 12/13] Move Tech-neophyte--pc.md #436 --- Day-21/q3: Unique Paths/Tech-neophyte--pc.md | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Day-21/q3: Unique Paths/Tech-neophyte--pc.md diff --git a/Day-21/q3: Unique Paths/Tech-neophyte--pc.md b/Day-21/q3: Unique Paths/Tech-neophyte--pc.md new file mode 100644 index 00000000..fa621bc8 --- /dev/null +++ b/Day-21/q3: Unique Paths/Tech-neophyte--pc.md @@ -0,0 +1,65 @@ +## Python code: +``` +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + row = [1]*n + + for i in range(m-1): + newRow = [1]*n + + for j in range(n-2, -1, -1): + newRow[j] = newRow[j+1] + row[j] + row = newRow + + return row[0] +``` +## cpp code: Using dp +``` +class Solution { +public: + int uniquePaths(int m, int n) { + + vector> dp(m, vector(n, 0)); + + + for (int i = 0; i < m; ++i) { + dp[i][0] = 1; + } + for (int j = 0; j < n; ++j) { + dp[0][j] = 1; + } + + + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + + return dp[m - 1][n - 1]; + } +}; +``` +## Cpp code: Using combinations: +``` +class Solution { +public: + int nCr(int n, int r) { + if (r > n) return 0; + if (r == 0 || n == r) return 1; + double res = 0; + for (int i = 0; i < r; i++) { + res += log(n-i) - log(i+1); + } + return (int)round(exp(res)); + } + + int uniquePaths(int m, int n) { + int t = (m-1) + (n-1); + + return nCr(t,m-1); + + } +}; +``` From 47fb3fd38bac0dc839b2d1da9141ebcb5fb480ba Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:14:13 +0530 Subject: [PATCH 13/13] Delete day-23/q1/Tech-neophyte--c.md --- day-23/q1/Tech-neophyte--c.md | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 day-23/q1/Tech-neophyte--c.md diff --git a/day-23/q1/Tech-neophyte--c.md b/day-23/q1/Tech-neophyte--c.md deleted file mode 100644 index 332922ed..00000000 --- a/day-23/q1/Tech-neophyte--c.md +++ /dev/null @@ -1,23 +0,0 @@ -## Cpp code: -``` -class Solution { -public: - int minFallingPathSum(vector>& matrix) { - int n = matrix.size(); - for(int i=1;i0){ - miniv=min(miniv, matrix[i-1][j-1]); - } - if(j