-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
611d3d8
commit c63306c
Showing
6 changed files
with
243 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int> asteroidCollision(vector<int>& asteroids) { | ||
stack<int> 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<int> remainingAsteroids(st.size()); | ||
for (int i = remainingAsteroids.size() - 1; i >= 0; i--) { | ||
remainingAsteroids[i] = st.top(); | ||
st.pop(); | ||
} | ||
|
||
return remainingAsteroids; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int>& 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<int> leaves1, leaves2; | ||
dfs(root1, leaves1); | ||
dfs(root2, leaves2); | ||
return leaves1 == leaves2; | ||
} | ||
}; |
78 changes: 78 additions & 0 deletions
78
leetcode/problems/cpp/maximum-twin-sum-of-a-linked-list.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
stack<int>s; | ||
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "queue" | ||
using namespace std; | ||
|
||
class RecentCounter { | ||
private: queue<int> q; | ||
public: | ||
RecentCounter() {} | ||
|
||
int ping(int t) { | ||
q.push(t); | ||
while (q.front() < t - 3000) | ||
q.pop(); | ||
return q.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<char> 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); | ||
} | ||
}; |