diff --git a/leetcode/problems/c/removing-stars-from-a-string.c b/leetcode/problems/c/removing-stars-from-a-string.c new file mode 100644 index 0000000..47e695f --- /dev/null +++ b/leetcode/problems/c/removing-stars-from-a-string.c @@ -0,0 +1,16 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(1) + +char* removeStars(char* s) { + int i = 0, j = 0; + while (s[i] != '\0') { + if (s[i] == '*') + j--; + else + s[j++] = s[i]; + i++; + } + s[j] = '\0'; + return s; +} \ No newline at end of file diff --git a/leetcode/problems/cpp/asteroid-collision.cpp b/leetcode/problems/cpp/asteroid-collision.cpp new file mode 100644 index 0000000..f803bd8 --- /dev/null +++ b/leetcode/problems/cpp/asteroid-collision.cpp @@ -0,0 +1,41 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "iostream" +#include "vector" +#include "stack" +#include "bits/std_abs.h" +using namespace std; + + +class Solution { +public: + vector asteroidCollision(vector& asteroids) { + stack st; + + for (auto asteroid : asteroids) { + int flag = 1; + + while (!st.empty() && (st.top() > 0 && asteroid < 0)) { + if (abs(st.top()) < abs(asteroid)) { + st.pop(); + continue; + } else if (abs(st.top()) == abs(asteroid)) + st.pop(); + flag = 0; + break; + } + + if (flag) st.push(asteroid); + } + + vector remainingAsteroids(st.size()); + for (int i = remainingAsteroids.size() - 1; i >= 0; i--) { + remainingAsteroids[i] = st.top(); + st.pop(); + } + + return remainingAsteroids; + } +}; \ No newline at end of file diff --git a/leetcode/problems/cpp/leaf-similar-trees.cpp b/leetcode/problems/cpp/leaf-similar-trees.cpp new file mode 100644 index 0000000..f5491dc --- /dev/null +++ b/leetcode/problems/cpp/leaf-similar-trees.cpp @@ -0,0 +1,28 @@ +#include "stack" +using namespace std; +// 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 { + void dfs(TreeNode* root, stack& level) { + if (!root) return; + if (root->left == nullptr && root->right == nullptr) + level.push(root->val); + dfs(root->left, level); + dfs(root->right, level); + } +public: + bool leafSimilar(TreeNode* root1, TreeNode* root2) { + stack leaves1, leaves2; + dfs(root1, leaves1); + dfs(root2, leaves2); + return leaves1 == leaves2; + } +}; \ No newline at end of file diff --git a/leetcode/problems/cpp/maximum-twin-sum-of-a-linked-list.cpp b/leetcode/problems/cpp/maximum-twin-sum-of-a-linked-list.cpp new file mode 100644 index 0000000..36932c1 --- /dev/null +++ b/leetcode/problems/cpp/maximum-twin-sum-of-a-linked-list.cpp @@ -0,0 +1,78 @@ +// by @codeAbinash + +#include "iostream" +#include "vector" +#include "algorithm" +#include "stack" +using namespace std; + +// Definition for singly-linked list. +struct ListNode { + int val; + ListNode* next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode* next) : val(x), next(next) {} +}; + +// Using Stack +// Time : O(n) +// Space : O(n) + +class Solution { +public: + int pairSum(ListNode* head) { + ListNode* temp = head; + stacks; + while (temp) { + s.push(temp->val); + temp = temp->next; + } + int maxSum = 0; + while (s.size()) { + maxSum = max(maxSum, s.top() + head->val); + head = head->next; + s.pop(); + } + return maxSum; + } +}; + + +// Using Two Pointer +// Time : O(n) +// Space : O(1) + +class Solution { +public: + int pairSum(ListNode* head) { + ListNode* temp = head; + int maxSum = 0; + ListNode* slow = head, * fast = head; + // Go to the middle of the linked list + + while (fast && fast->next) { + slow = slow->next; + fast = fast->next->next; + } + + // Reverse the second half of the linked list + ListNode* prev = NULL, * curr = slow, * next = NULL; + + while (curr) { + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + + // Now, prev is the head of the reversed linked list + while (prev) { + maxSum = max(maxSum, prev->val + head->val); + prev = prev->next; + head = head->next; + } + + return maxSum; + } +}; \ No newline at end of file diff --git a/leetcode/problems/cpp/number-of-recent-calls.cpp b/leetcode/problems/cpp/number-of-recent-calls.cpp new file mode 100644 index 0000000..3f8a72d --- /dev/null +++ b/leetcode/problems/cpp/number-of-recent-calls.cpp @@ -0,0 +1,19 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "queue" +using namespace std; + +class RecentCounter { +private: queue q; +public: + RecentCounter() {} + + int ping(int t) { + q.push(t); + while (q.front() < t - 3000) + q.pop(); + return q.size(); + } +}; \ No newline at end of file diff --git a/leetcode/problems/cpp/removing-stars-from-a-string.cpp b/leetcode/problems/cpp/removing-stars-from-a-string.cpp index 2f7ada9..6e038b7 100644 --- a/leetcode/problems/cpp/removing-stars-from-a-string.cpp +++ b/leetcode/problems/cpp/removing-stars-from-a-string.cpp @@ -1,17 +1,66 @@ -// by @codeAbinash +// // by @codeAbinash +#include "vector" +#include "string" +#include "stack" +#include "algorithm" +using namespace std; + // Time Complexity : O(n) // Space Complexity : O(n) +class Solution { +public: + string removeStars(string s) { + string f; + for (auto& ch : s) { + if (ch == '*') + f.pop_back(); + else + f.push_back(ch); + } + return f; + } +}; + +// Time : O(n) +// Space : O(n) +class Solution { +public: + string removeStars(string s) { + stack stk; + + for (auto ch : s) { + if (ch == '*') + stk.pop(); + else + stk.push(ch); + } + string result; + + while (!stk.empty()) { + result.push_back(stk.top()); + stk.pop(); + } + + reverse(result.begin(), result.end()); + return result; + } +}; + +// Better way, using two pointers +// Time : O(n) +// Space : O(1) class Solution { - public: - string removeStars(string s) { - string f; - for (auto &ch : s) { - if (ch == '*') - f.pop_back(); - else - f.push_back(ch); - } - return f; - } +public: + string removeStars(string s) { + int i = 0, j = 0; + int len = s.length(); + + for (int i = 0; i < len; i++) { + if (s[i] == '*') j--; + else s[j++] = s[i]; + } + + return s.substr(0, j); + } }; \ No newline at end of file