Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oct 2024 hacktoberfest #102

Merged
merged 5 commits into from
Oct 27, 2024
Merged
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
77 changes: 77 additions & 0 deletions LeetCode/Algorithms/Hard/NumberOfAtoms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Solution {
public:
string countOfAtoms(string formula) {
stack<map<int, int>> m;

int i = 0;
string count_str = "";
int count = 0;
while(i < formula.size()) {
string count_str = "";
if(formula[i] == '(') {
stack.push({});
} else if(formula[i] == ')') {
curr_map = stack.top();
stack.pop();

count_str = "";
while((i + 1) < formula.size() && isdigit(formula[i + 1])) {
count += formula[i + 1];
i += 1;
}

if(count_str.size() == 0) {
count = 1;
} else {
count = stoi(count_str);
}

for(auto &ele: cur_map) {
cur_map[ele] *= count;

prev_map[ele] += cur_map[ele];
}

prev_map = stack.top();


prev_map = stack.top();
} else {
char element = formula[i];
string count_str = "";
if((i + 1) < len(formula) && islower(formula[i + 1])) {
element = formula.substr(i, 2);
}

while(i + 1 < len(formula)) {
count_str += formula[i + 1];
i+=1;
}

int count = 0;
if(count_str.size() == 0) {
count = 1;
} else {
count = stoi(count_str);
}

map<int, int> curr_map = stack.top();
curr_map[element] += count;
}
}

map<int, int> count_map = stk.top();
string result = "";
for(ele in count_map) {

if(count_map[ele] == 1) {
count = 0;
} else {
cunt = count_map[ele];
}
// count = count_map[ele];
result = ele + to_string(count);
}
return result;
}
};
36 changes: 36 additions & 0 deletions LeetCode/Algorithms/Hard/WordBreak2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:

vector<string> result;
vector<string> wordBreak(string s, vector<string>& wordDict) {
vector<string> curr;
dfs(0, s, wordDict, curr);

return result;
}

void dfs(int index, string s, vector<string> &wordDict, vector<string> &curr) {
if(index = s.size()) {
string sub = "";
for(int i=0; i<curr.size(); i++) {
sub += " " + curr;
break;
}
result.push_back(curr);
}

for(int j=index; j<s.size(); j++) {
string w = s.substr(index, j + 1 - index);

for(auto &ele: wordDict) {
if(ele == w) {
curr.push_back(w);
backtrack(j + 1);
curr.pop();
}
}
}
}


};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
long long countSubarrays(vector<int>& nums, int k) {
int maxCount = 0;
int n = nums.size();
int maxNum = *max_element(nums.begin(), nums.end());

int left = 0;
int result = 0;
for(int right = 0; right < n; right++) {
if(nums[right] == maxNum) {
maxCount += 1;
}

while(maxCount > k || (left <= right && maxCount == k && nums[left] != maxNum)) {
if(nums[left] == maxNum) {
maxCount--;
}
left += 1;
}

if(maxCount == k) {
result += l + 1;
}
}

return result;


}
};


// nums = [1,3,2,3,3], k = 2
// l r


59 changes: 59 additions & 0 deletions LeetCode/Algorithms/Medium/CousinsInBinaryTreeII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
public:
TreeNode* replaceValueInTree(TreeNode* root) {
vector<int> level_sum;
queue<TreeNode *> q;

while(!q.empty()) {
int current_sum = 0;

for(int i=0; i<q.size(); i++) {
TreeNode *node = q.front();
q.pop();

current_sum += node->val;

if(node->left) {
q.push(node->left);
}
if(node->right) {
q.push(node->right);
}
}

level_sum.push_back(current_sum);
}

queue<TreeNode *> qnode;

qnode.push(root);
root->val = 0;
while(!qnode.empty()) {
for(int i=0; i<qnode.size(); i++) {
TreeNode *temp = qnode.front();
qnode.pop();

int child_sum = 0;
if(temp->left) {
child_sum += temp->left->val;
}

if(temp->right) {
child_sum += temp->right->val;
}
if(temp->left) {
temp->left->val = level_sum[level + 1] - current_sum;
qnode.push(temp->left);
}
if(temp->right) {
temp->right = level_sum[level + 1] - current_sum;
qnode.push(temp->right);
}
}
level++;
}

return root;
}

};
38 changes: 38 additions & 0 deletions LeetCode/Algorithms/Medium/FillingBookcaseShelves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
vector<int> cache;

return helper(0, cache, books, shelfWidth);
}

int helper(int i, vector<int> cache, vector<vector<int>> &books, int shelfWidth) {
if(i == books.size()) {
return 0;
}

if(cache.find(i) != cache.end()) {
return cache[i];
}

cur_width = shelfWidth;

max_height = 0;
cache[i] = INT_MAX;

for(int j=i; j<books.size(); j++) {
int width = books[j][0];
int height = books[j][1];

if(cur_width < width) {
break;
}

cur_width -= width;
max_height = max(max_height, height);
res = min(res, helper(j + 1) + max_height);
}

return cache[i];
}
};
2 changes: 1 addition & 1 deletion LeetCode/Algorithms/Medium/FindBottomLeftTreeValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ class Solution
// It'll allow us to return the left-most node when the queue return empty == True

// Time Complexity - O(n) - BFS
// Space Complexity - O(n) - Used queue for traversal
// Space Complexity - O(n) - Used queue for traversal
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
deque<int> min_q; // monotonic increasing
deque<int> max_q; // monotonic decreasing
int l = 0;
int result = 0;

for(int r = 0; r<nums.size(); r++) {
while(min_q.empty() && nums[r] < min_q[min_q.size() -1]) {
min_q.pop_back();
}

while(max_q.empty() && nums[r] > max_q[max_q.size() - 1]) {
max_q.pop_back();
}

max_q.push_back(nums[r]);
max_q.push_back(nums[r]);

while(max_q[0] - min_q[0] > limit) {
if(nums[l] == max_q[0]) {
max_q.pop_front();
}
if(nums[l] == min_q[0]) {
min_q.pop_front();
}
l++;
}
result = max(res, r - l + 1);
}

return result;
}
};


// Min data structure will be monotonically increasing
// Max data structure will be monotonically decreasing