Skip to content

Commit 594e1c0

Browse files
committed
319/319 notably, reconstruct-itinerary: Hierholzer's algorithm; palindrome-pairs: manacher
1 parent 0d63026 commit 594e1c0

6 files changed

+177
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 314/314 problems.
5+
Solved 319/319 problems.
66

77
## Database
88

@@ -12,6 +12,11 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|[palindrome-pairs.cc](palindrome-pairs.cc)|
16+
|335|[Self Crossing](https://leetcode.com/problems/self-crossing/)|[self-crossing.cc](self-crossing.cc)|
17+
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|[increasing-triplet-subsequence.cc](increasing-triplet-subsequence.cc)|
18+
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)|[largest-bst-subtree.cc](largest-bst-subtree.cc)|
19+
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/)|[reconstruct-itinerary.cc](reconstruct-itinerary.cc)|
1520
|331|[Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/)|[verify-preorder-serialization-of-a-binary-tree.cc](verify-preorder-serialization-of-a-binary-tree.cc)|
1621
|330|[Patching Array](https://leetcode.com/problems/patching-array/)|[patching-array.cc](patching-array.cc)|
1722
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)|[longest-increasing-path-in-a-matrix.cc](longest-increasing-path-in-a-matrix.cc)|

increasing-triplet-subsequence.cc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Increasing Triplet Subsequence
2+
class Solution {
3+
public:
4+
bool increasingTriplet(vector<int>& nums) {
5+
int a = INT_MAX, b = INT_MAX;
6+
for (int x: nums) {
7+
if (x <= a)
8+
a = x;
9+
else if (x <= b)
10+
b = x;
11+
else
12+
return true;
13+
}
14+
return false;
15+
}
16+
};

largest-bst-subtree.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Largest BST Subtree
2+
class Solution {
3+
bool postorder(TreeNode *x, long &mn, long &mx, long &size) {
4+
if (! x) {
5+
mn = LONG_MAX;
6+
mx = LONG_MIN;
7+
size = 0;
8+
return true;
9+
}
10+
long lmn, lmx, ls, rmn, rmx, rs;
11+
bool lvalid = postorder(x->left, lmn, lmx, ls),
12+
rvalid = postorder(x->right, rmn, rmx, rs);
13+
if (lvalid && rvalid && lmx < x->val && x->val < rmn) {
14+
mn = ls ? lmn : x->val;
15+
mx = rs ? rmx : x->val;
16+
size = ls+1+rs;
17+
return true;
18+
}
19+
size = max(ls, rs);
20+
return false;
21+
}
22+
public:
23+
int largestBSTSubtree(TreeNode* root) {
24+
long mn, mx, size;
25+
postorder(root, mn, mx, size);
26+
return size;
27+
}
28+
};

palindrome-pairs.cc

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Palindrome Pairs
2+
#define REP(i, n) for (int i = 0; i < (n); i++)
3+
4+
class Solution {
5+
string a;
6+
vector<int> z;
7+
void manacher(int n) {
8+
z.resize(n);
9+
z[0] = 1;
10+
for (int f, g = 1, i = 1; i < n; i++)
11+
if (i < g && z[2*f-i] != g-i)
12+
z[i] = min(z[2*f-i], g-i);
13+
else {
14+
g = max(g, f = i);
15+
for (; g < n && 2*f-g >= 0 && a[2*f-g] == a[g]; g++);
16+
z[f] = g-f;
17+
}
18+
}
19+
public:
20+
vector<vector<int>> palindromePairs(vector<string>& words) {
21+
unordered_multimap<string, int> ls, rs;
22+
REP(i, words.size()) {
23+
string &w = words[i];
24+
int n = w.size(), nn = 2*n+1;
25+
a = string(nn, '.');
26+
REP(j, n)
27+
a[2*j+1] = w[j];
28+
manacher(nn);
29+
REP(j, nn) {
30+
if (z[j] == j+1)
31+
ls.emplace(w.substr((j+z[j])/2), i);
32+
if (j+z[j] == nn)
33+
rs.emplace(w.substr(0, (j-z[j]+1)/2), i);
34+
}
35+
}
36+
vector<vector<int>> ret;
37+
REP(i, words.size()) {
38+
string w = words[i], ww = w;
39+
reverse(ww.begin(), ww.end());
40+
auto its = ls.equal_range(ww);
41+
for (auto it = its.first; it != its.second; ++it)
42+
if (it->second != i)
43+
ret.push_back(vector<int>{i, it->second});
44+
its = rs.equal_range(ww);
45+
for (auto it = its.first; it != its.second; ++it)
46+
if (w.size() < words[it->second].size())
47+
ret.push_back(vector<int>{it->second, i});
48+
}
49+
return ret;
50+
}
51+
};

reconstruct-itinerary.cc

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Reconstruct Itinerary
2+
/// Hierholzer's algorithm
3+
4+
class Solution {
5+
unordered_map<string, multiset<string>> m;
6+
vector<string> ret;
7+
void hierholzer(string x) {
8+
while (! m[x].empty()) {
9+
string y = *m[x].begin();
10+
m[x].erase(m[x].begin());
11+
hierholzer(y);
12+
}
13+
ret.push_back(x);
14+
}
15+
public:
16+
vector<string> findItinerary(vector<pair<string, string>> tickets) {
17+
for (auto &x: tickets)
18+
m[x.first].insert(x.second);
19+
hierholzer("JFK");
20+
reverse(ret.begin(), ret.end());
21+
return ret;
22+
}
23+
};
24+
25+
/// non-recursive Hierholzer's algorithm
26+
27+
class Solution {
28+
public:
29+
vector<string> findItinerary(vector<pair<string, string>> tickets) {
30+
unordered_map<string, multiset<string>> m;
31+
for (auto &x: tickets)
32+
m[x.first].insert(x.second);
33+
stack<string> st;
34+
vector<string> ret;
35+
st.push("JFK");
36+
while (! st.empty()) {
37+
auto &x = st.top();
38+
if (m[x].empty()) {
39+
ret.push_back(x);
40+
st.pop();
41+
} else {
42+
auto it = m[x].begin();
43+
st.push(*it);
44+
m[x].erase(it);
45+
}
46+
}
47+
reverse(ret.begin(), ret.end());
48+
return ret;
49+
}
50+
};

self-crossing.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Self Crossing
2+
/* https://leetcode.com/discuss/88153/another-python
3+
b b
4+
+----------------+ +----------------+
5+
| | | |
6+
| | | | a
7+
c | | c | |
8+
| | a | | f
9+
+-----------> | | | <----+
10+
d | | | | e
11+
| | |
12+
+-----------------------+
13+
d
14+
*/
15+
class Solution {
16+
public:
17+
bool isSelfCrossing(vector<int>& x) {
18+
int a = 0, b = 0, c = 0, d = 0, e = 0;
19+
for (int f: x) {
20+
if (c && d <= f && e <= c || b && b <= d && d <= b+f && e <= c && c <= a+e)
21+
return true;
22+
a = b, b = c, c = d, d = e, e = f;
23+
}
24+
return false;
25+
}
26+
};

0 commit comments

Comments
 (0)