leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE |

commit6dc5e51a5507b8c08bc139a88d0ae643012fa713
parentf34df98334698d4b4473bdc3de5e3abd31288b74
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateTue, 24 Jan 2023 13:01:54 +0100

Remove orphaned, fix formatting

Diffstat:
AProblems/0022.cpp|+++++++++++++++++++++++++++++++
MProblems/0149.cpp|+++++++++++++-------------
MProblems/0217.cpp|-
MProblems/0977.cpp|+++++++-------
DProblems/144.cpp|------------------
MProblems/1480.cpp|+++++-----
DProblems/24.cpp|--------------
MREADME.md|+

8 files changed, 57 insertions(+), 58 deletions(-)


diff --git a/Problems/0022.cpp b/Problems/0022.cpp

@@ -0,0 +1,31 @@

class Solution {
vector<string> res;
stack<char> st;
string crnt = "";
void dfs(int n) {
static const char o = '(', c = ')';
if (!n) {
if (st.empty()) res.push_back(crnt);
return;
}
st.push(o), crnt += o;
dfs(n - 1);
st.pop(), crnt.pop_back();
if (st.empty()) return;
crnt += c, st.pop();
dfs(n - 1);
crnt.pop_back();
st.push(c);
}
public:
vector<string> generateParenthesis(int n) {
dfs(n * 2);
return res;
}
};

diff --git a/Problems/0149.cpp b/Problems/0149.cpp

@@ -1,18 +1,18 @@

class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int n = points.size();
if (n == 1) return 1;
int maxPoints(vector<vector<int>> &points) {
int n = points.size();
if (n == 1) return 1;
int res = 2;
for (int i = 0; i < n; i++) {
unordered_map<double, int> um;
for (int j = 0; j < n; j++) {
if (j == i) continue;
um[atan2(points[j][1] - points[i][1], points[j][0] - points[i][0])]++;
}
for (auto [_, count] : um) res = max(res, count + 1);
}
return res;
int res = 2;
for (int i = 0; i < n; i++) {
unordered_map<double, int> um;
for (int j = 0; j < n; j++) {
if (j == i) continue;
um[atan2(points[j][1] - points[i][1], points[j][0] - points[i][0])]++;
}
for (auto [_, count] : um) res = max(res, count + 1);
}
return res;
}
};

diff --git a/Problems/0217.cpp b/Problems/0217.cpp

@@ -18,4 +18,3 @@ public:

return nums.size() > unordered_set<int>(nums.begin(), nums.end()).size();
}
};

diff --git a/Problems/0977.cpp b/Problems/0977.cpp

@@ -25,11 +25,11 @@ public:

// avoids reversal of the array
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size(), i=0, j =nums.size()-1;
vector<int> res(n);
for_each(nums.begin(), nums.end(), [](int &a) { a*=a; });
while(i<=j) res[--n] = nums[i]>nums[j] ? nums[i++] : nums[j--];
return res;
}
vector<int> sortedSquares(vector<int> &nums) {
int n = nums.size(), i = 0, j = nums.size() - 1;
vector<int> res(n);
for_each(nums.begin(), nums.end(), [](int &a) { a *= a; });
while (i <= j) res[--n] = nums[i] > nums[j] ? nums[i++] : nums[j--];
return res;
}
};

diff --git a/Problems/144.cpp b/Problems/144.cpp

@@ -1,18 +0,0 @@

class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
if (!root) return {};
vector<int> res;
stack<TreeNode *> st;
st.push(root);
while (!st.empty()) {
TreeNode *root = st.top();
st.pop();
res.push_back(root->val);
if (root->right) st.push(root->right);
if (root->left) st.push(root->left);
}
return res;
}
};

diff --git a/Problems/1480.cpp b/Problems/1480.cpp

@@ -11,9 +11,9 @@ public:

// using lambda function
class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int acc = 0;
for_each(nums.begin(), nums.end(), [&acc](int &a) { a = acc+=a; });
return nums;
}
vector<int> runningSum(vector<int> &nums) {
int acc = 0;
for_each(nums.begin(), nums.end(), [&acc](int &a) { a = acc += a; });
return nums;
}
};

diff --git a/Problems/24.cpp b/Problems/24.cpp

@@ -1,14 +0,0 @@

class Solution {
public:
ListNode *swapPairs(ListNode *head) {
ListNode *d = new ListNode(-1, head);
for (ListNode *p = d; p->next && p->next->next;) {
ListNode *t = p->next;
p->next = p->next->next;
t->next = p->next->next;
p->next->next = t;
p = t;
}
return d->next;
}
};

diff --git a/README.md b/README.md

@@ -34,6 +34,7 @@ for solving problems.

| 0019 | Medium | [Remove Nth Node From the End of List](Problems/0019.cpp) |
| 0020 | Easy | [Valid Parentheses](Problems/0020.cpp) |
| 0021 | Easy | [Merge Two Sorted Lists](Problems/0021.cpp) |
| 0022 | Medium | [Generate Parentheses](Problems/0022.cpp) |
| 0023 | Hard | [Merge k Sorted Lists](Problems/0023.cpp) |
| 0024 | Medium | [Swap Nodes in Pairs](Problems/0024.cpp) |
| 0025 | Hard | [Reverse Nodes in k-Group](Problems/0025.cpp) |