Skip to content

Commit

Permalink
Merge pull request #436 from Tech-neophyte/main
Browse files Browse the repository at this point in the history
day 18-q2 py, day21-q2, day 22-q1, 23-q1, 19-q3, 21-q3
  • Loading branch information
bh-g authored Jan 23, 2024
2 parents f1ba4a2 + 47fb3fd commit 6f7cc55
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Approach:
<br /> 1. Initialize a Mapping: Create a dictionary that maps each digit from 2 to 9 to their corresponding letters on a telephone's buttons.
<br /> 2. Base Case: If the input string digits is empty, return an empty list.
<br /> 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.
<br /> 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
```
27 changes: 27 additions & 0 deletions Day-21/q2 : Container With Most Water/Tech-neophyte--c.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Approach:
<br />1. Initiate two pointers, one from start (left) and one from end (right).
<br />2. While left<right find the max area possible by inc left and dec right wherever necessary.
## cpp code:
```
class Solution {
public:
int maxArea(vector<int>& 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;
}
};
```
65 changes: 65 additions & 0 deletions Day-21/q3: Unique Paths/Tech-neophyte--pc.md
Original file line number Diff line number Diff line change
@@ -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<vector<int>> dp(m, vector<int>(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);
}
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Approach:

<br /> 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.
<br /> 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.
<br />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<int>& nums)
{
unordered_map<int, int> counter;
int ans = 0;
for (int i=0;i<nums.size();i++)
counter[nums[i]]++;
for (auto it : counter)
{
if (it.second == 1)
return -1;
ans = ans + ceil( (double)(it.second) / 3) ;
}
return ans;
}
};
```

0 comments on commit 6f7cc55

Please sign in to comment.