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 + + +``` 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..37d649c0 --- /dev/null +++ b/Day-21/q2 : Container With Most Water/Tech-neophyte--c.md @@ -0,0 +1,27 @@ +## Approach: +
1. Initiate two pointers, one from start (left) and one from end (right). +
2. While left& 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; + } +}; +``` 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); + + } +}; +``` diff --git a/Day-22/q1: Minimum Number of Operations to Make Array Empty/Tech-neophyte--c.md b/Day-22/q1: Minimum Number of Operations to Make Array Empty/Tech-neophyte--c.md new file mode 100644 index 00000000..9d128116 --- /dev/null +++ b/Day-22/q1: Minimum Number of Operations to Make Array Empty/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