leetcode

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

commit99a39c006ef65108fb12ff7e989350acf4a91e69
parent4d7621ffe152a5af0283fa78ddeaf64dd26f1f04
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateSat, 13 Jan 2024 21:27:59 +0000

2 Random Problems

Diffstat:
AProblems/0449.cpp|++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AProblems/2412.cpp|++++++++++++++++++++++
MREADME.md|++

3 files changed, 80 insertions(+), 0 deletions(-)


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

@@ -0,0 +1,56 @@

static const auto _ = []() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
return NULL;
}();
class Codec {
typedef tuple<TreeNode **, int, int> record;
public:
// Encodes a tree to a single string.
string serialize(const TreeNode *root) const {
static int buff[10001];
int idx = 0;
if (!root) return "";
stack<const TreeNode *> st;
for (st.push(root); !st.empty();) {
auto root = st.top();
st.pop();
while (root) {
buff[idx++] = root->val;
if (root->right) st.push(root->right);
root = root->left;
}
}
return string(reinterpret_cast<const char *>(buff), idx * 4);
}
// Decodes your encoded data to tree.
TreeNode *deserialize(const string &sdata) const {
auto data = reinterpret_cast<const int *>(sdata.data());
TreeNode dummy, *node;
stack<record> st;
for (st.push({&dummy.left, 0, size(sdata) / 4}); !st.empty();) {
auto [place, start, end] = st.top();
st.pop();
while (start != end) {
node = *place = new TreeNode(data[start]);
const auto mid = upper_bound(data + start, data + end, data[start]) - data;
st.push({&node->right, mid, end});
place = &node->left;
start = start + 1;
end = mid;
}
}
return dummy.left;
}
};

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

@@ -0,0 +1,22 @@

static const auto _ = []() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return NULL;
}();
class Solution {
public:
int longestContinuousSubstring(const string &s) const {
int res = 0, crnt = 1;
for (int i = 1; i < size(s); i++) {
if (s[i] - s[i - 1] == 1)
crnt++;
else {
res = max(res, crnt);
crnt = 1;
}
}
return max(res, crnt);
}
};

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

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

| 0445 | Medium | [Add Two Numbers II](Problems/0445.cpp) |
| 0446 | Hard | [Arithmetic Slices II - Subsequence](Problems/0446.cpp) |
| 0448 | Easy | [Find All Numbers Disappeared in an Array](Problems/0448.cpp) |
| 0449 | Medium | [Serialize and Deserialize BST](Problems/0449.cpp) |
| 0450 | Medium | [Delete Node in a BST](Problems/0450.cpp) |
| 0451 | Medium | [Sort Characters By Frequency](Problems/0451.cpp) |
| 0452 | Medium | [Minimum Number of Arrows to Burst Balloons](Problems/0452.cpp) |

@@ -981,6 +982,7 @@ for solving problems.

| 2396 | Medium | [Strictly Palindromic Number](Problems/2396.cpp) |
| 2405 | Medium | [Optimal Partition of String](Problems/2405.cpp) |
| 2410 | Medium | [Maximum Matching of Players With Trainers](Problems/2410.cpp) |
| 2414 | Medium | [Length of the Longest Alphabetical Continuous Substring](Problems/2414.cpp) |
| 2415 | Medium | [Reverse Odd Levels of Binary Tree](Problems/2415.cpp) |
| 2421 | Medium | [Number of Good Paths](Problems/2421.cpp) |
| 2423 | Easy | [Remove Letter To Equalize Frequency](Problems/2423.cpp) |