Skip to content
Merged

. #107

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Leetcode/0565-array-nesting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public:
maxi = max(maxi, dp[i]);
return maxi + 1;
}

void dfs(int u, int par, vector<int>& vis) {
vis[u] = 1;
for (int v : adj[u]) {
Expand Down
58 changes: 57 additions & 1 deletion Leetcode/0854-making-a-large-island/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,63 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int dfs(int row , int col , vector<vector<int>>& grid , int id){
int n = grid.size();
if(row < 0 || row >= n || col < 0 || col >= n || grid[row][col] != 1){
return 0;
}
grid[row][col] = id;
int delrow[] = {0 , 1 , 0 , -1};
int delcol[] = {1 , 0 , -1 , 0};
int size = 1;
for(int i = 0 ; i < 4 ; i++){
int nrow = delrow[i] + row;
int ncol = delcol[i] + col;

size += dfs(nrow , ncol , grid , id);
}
return size;
}
int largestIsland(vector<vector<int>>& grid) {
int n = grid.size();
map<int, int> mpp; // id no of island -> size of island
int id = 2;
int maxi = 0;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(grid[i][j] == 1){
int size = dfs(i , j , grid , id);
maxi = max(maxi , size);
mpp[id] = size;
id++;
}
}
}
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < n ; j++){
if(grid[i][j] == 0){ // ask the ngbrs about their max size and add 1 to it
set<int> st;
int delrow[] = {0 , 1 , 0 , -1};
int delcol[] = {1 , 0 , -1 , 0};
for(int x = 0 ; x < 4 ; x++){
int i_ = i + delrow[x];
int j_ = j + delcol[x];
if(i_ >= 0 && i_ < n && j_ >= 0 && j_ < n &&
grid[i_][j_] != 0) st.insert(grid[i_][j_]);
}
int currSize = 1;
for(auto id : st){
currSize += mpp[id];
}
maxi = max(maxi , currSize);
}
}
}
return maxi;
}
};
```

</template>
Expand Down
17 changes: 16 additions & 1 deletion Leetcode/0878-shifting-letters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,22 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
string shiftingLetters(string s, vector<int>& shifts) {
int n = s.size();
vector<long long> suff(n);
suff[n - 1] = shifts[n - 1];
for(int i = n - 2; i >= 0; i--) {
suff[i] = suff[i + 1] + shifts[i];
}
for(int i = 0; i < n; i++) {
int cnt = suff[i] % 26;
s[i] = 'a' + ((s[i] - 'a') + cnt) % 26;
}
return s;
}
};
```

</template>
Expand Down
24 changes: 23 additions & 1 deletion Leetcode/0892-shortest-subarray-with-sum-at-least-k/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int shortestSubarray(vector<int>& nums, int k) {
int n = nums.size();
vector<long long> prefixSum(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
deque<int> dq;
int minLength = INT_MAX;
for (int i = 0; i <= n; ++i) {
while (!dq.empty() && prefixSum[i] - prefixSum[dq.front()] >= k) {
minLength = min(minLength, i - dq.front());
dq.pop_front();
}
while (!dq.empty() && prefixSum[i] <= prefixSum[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
return minLength == INT_MAX ? -1 : minLength;
}
};
```

</template>
Expand Down
16 changes: 15 additions & 1 deletion Leetcode/0900-reordered-power-of-2/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,21 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
bool reorderedPowerOf2(int n) {
auto countDigits = [](int x) {
string s = to_string(x);
sort(s.begin(), s.end());
return s;
};
string target = countDigits(n);
for (int i = 0; i < 31; i++) {
if (countDigits(1 << i) == target) return true;
}
return false;
}
};
```

</template>
Expand Down
26 changes: 25 additions & 1 deletion Leetcode/0901-advantage-shuffle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,31 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
map<int,int> mpp;
for (int x : nums1) {
mpp[x]++;
}
vector<int> res;
for (int i = 0; i < nums2.size(); i++) {
int curr = nums2[i];
auto it = mpp.upper_bound(curr);
if (it == mpp.end()) {
it = mpp.begin();
}
res.push_back(it->first);
it->second--;
if (it->second == 0) {
mpp.erase(it);
}
}
return res;
}
};

```

</template>
Expand Down
26 changes: 25 additions & 1 deletion Leetcode/0905-length-of-longest-fibonacci-subsequence/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,31 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int lenLongestFibSubseq(vector<int>& arr) {
int n = arr.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
int maxLen = 0;
for (int curr = 2; curr < n; curr++) {
int start = 0, end = curr - 1;
while (start < end) {
int pairSum = arr[start] + arr[end];
if (pairSum > arr[curr]) {
end--;
} else if (pairSum < arr[curr]) {
start++;
} else {
dp[end][curr] = dp[start][end] + 1;
maxLen = max(dp[end][curr], maxLen);
end--;
start++;
}
}
}
return maxLen == 0 ? 0 : maxLen + 2;
}
};
```

</template>
Expand Down
18 changes: 17 additions & 1 deletion Leetcode/0920-uncommon-words-from-two-sentences/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,23 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
unordered_map<string, int> count;
stringstream ss1(A), ss2(B);
string word;
while (ss1 >> word) count[word]++;
while (ss2 >> word) count[word]++;
vector<string> ans;
for (auto &it : count) {
if (it.second == 1) {
ans.push_back(it.first);
}
}
return ans;
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,41 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
TreeNode* solve(int prestart, int poststart, int preend, vector<int>& preorder, vector<int>& postorder,
unordered_map<int,int>& mpp){
if(prestart > preend) return NULL;
TreeNode* root = new TreeNode(preorder[prestart]);
if(prestart == preend) return root;
int nextNode = preorder[prestart + 1];
int j = mpp[nextNode];
int num = j - poststart + 1;
root -> left = solve(prestart + 1, poststart, prestart + num, preorder, postorder, mpp);
root -> right = solve(prestart + num + 1, j + 1, preend, preorder, postorder, mpp);
return root;
}
public:
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
int n = preorder.size();
unordered_map<int, int> mpp;
for(int i = 0; i < n; i++){
mpp[postorder[i]] = i;
}
return solve(0, 0, n - 1, preorder, postorder, mpp);
}
};
```

</template>
Expand Down
3 changes: 0 additions & 3 deletions Leetcode/0936-rle-iterator/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ private:
int node;
Pair(long long c, int n) : count(c), node(n) {}
};

deque<Pair> dq;

public:
RLEIterator(vector<int>& encoding) {
int n = encoding.size();
Expand All @@ -119,7 +117,6 @@ public:
dq.emplace_back(count, node);
}
}

int next(int n) {
long long req = n;
int last = -1;
Expand Down