Skip to content

Commit 012a59e

Browse files
Merge branch 'master' of https://github.com/doocs/leetcode
2 parents 9277faf + 549243a commit 012a59e

File tree

7 files changed

+164
-3
lines changed

7 files changed

+164
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</p>
44

55
<p align="center">
6-
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/lang/Java,CPP,Python,JS,Ruby,Go,.../orange?list=1" alt="language"></a>
6+
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/lang/Java,C++,Python,JS,Ruby,Go,.../orange?list=1" alt="language"></a>
77
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
88
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
99
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>

SOLUTION_TREE.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
│   ├── Solution.java
3737
│   └── Solution.js
3838
├── 0006.ZigZag Conversion
39+
│   ├── README.md
3940
│   ├── Solution.cpp
4041
│   ├── Solution.java
4142
│   ├── Solution.js
@@ -956,6 +957,16 @@
956957
│   └── Solution.py
957958
├── 1022.Smallest Integer Divisible by K
958959
│   └── Solution.py
959-
└── 1023.Binary String With Substrings Representing 1 To N
960-
└── Solution.py
960+
├── 1023.Binary String With Substrings Representing 1 To N
961+
│   └── Solution.py
962+
├── 1024.Video Stitching
963+
│   └── Solution.cpp
964+
├── 1025.Divisor Game
965+
│   └── Solution.cpp
966+
├── 1026.Maximum Difference Between Node and Ancestor
967+
│   └── Solution.cpp
968+
├── 1027.Longest Arithmetic Sequence
969+
│   └── Solution.cpp
970+
└── 1028.Recover a Tree From Preorder Traversal
971+
└── Solution.cpp
961972
```
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int videoStitching(vector<vector<int>>& clips, int T) {
4+
int maxEnding = 0;
5+
int count = 0;
6+
vector<int> maxEnd(T+1, 0); // maxEnd[i] : maximum ending time of all videos that start by time i
7+
for (int i = 0; i < clips.size(); ++i) {
8+
if (clips[i][0] >= T) continue;
9+
maxEnd[clips[i][0]] = max(maxEnd[clips[i][0]], clips[i][1]);
10+
}
11+
for (int i = 1; i < T; ++i) {
12+
maxEnd[i] = max(maxEnd[i], maxEnd[i-1]);
13+
}
14+
for (int i = 0; i < T; ++i) {
15+
if (maxEnding == i) { // select video with maximum ending if one more video is necessary
16+
count ++;
17+
maxEnding = maxEnd[i];
18+
} else if (maxEnding < i) {
19+
return -1;
20+
}
21+
}
22+
return count;
23+
}
24+
};
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
unordered_map<int, bool> mem;
3+
public:
4+
bool divisorGame(int N) {
5+
if (N == 1) return false;
6+
if (mem.count(N)) {
7+
return mem[N];
8+
}
9+
for (int i = 1; i < N; ++i) {
10+
if (N % i == 0) {
11+
if (divisorGame(N-i) == false) {
12+
mem[N] = true;
13+
return true;
14+
}
15+
}
16+
}
17+
mem[N] = false;
18+
return false;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
int helper(TreeNode* node, int& left, int & right) {
12+
left = INT_MAX;
13+
right = INT_MIN;
14+
if (node == nullptr) {
15+
return 0;
16+
}
17+
left = node->val;
18+
right = node->val;
19+
20+
int tmpLeft, tmpRight;
21+
int res = 0;
22+
23+
res = max(res, helper(node->left, tmpLeft, tmpRight));
24+
left = min(left, tmpLeft);
25+
right = max(right, tmpRight);
26+
27+
res = max(res, helper(node->right, tmpLeft, tmpRight));
28+
left = min(left, tmpLeft);
29+
right = max(right, tmpRight);
30+
31+
res = max(res, abs(node->val - left));
32+
res = max(res, abs(node->val - right));
33+
34+
return res;
35+
}
36+
public:
37+
int maxAncestorDiff(TreeNode* root) {
38+
int left = 0, right = 0;
39+
return helper(root, left, right);
40+
}
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
unordered_map<int, int> tested; // map (start index * 1005 + steps) to longest length
3+
public:
4+
int longestArithSeqLength(vector<int>& A) {
5+
int res = 0;
6+
for (int i = 0; i < A.size(); ++i) {
7+
for (int j = 0; j < i; ++j) {
8+
int diff = A[i] - A[j];
9+
if (tested.count(j*10005 + diff)) {
10+
tested[i*10005+diff] = max(tested[i*10005+diff], 1+tested[j*10005 + diff]);
11+
} else {
12+
tested[i*10005+diff] = max(tested[i*10005+diff], 2);
13+
}
14+
res = max(res, tested[i*10005+diff]);
15+
}
16+
}
17+
return res;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* recoverFromPreorder(string S) {
13+
stack<TreeNode*> st;
14+
int depth = 0;
15+
int num = 0;
16+
for (int i = 0; i < S.length(); ++i) {
17+
if (S[i] == '-') {
18+
depth ++;
19+
} else {
20+
num = 10*num + S[i]-'0';
21+
}
22+
if (i+1 >= S.length() || (isdigit(S[i]) && S[i+1] == '-')) {
23+
TreeNode * newNode = new TreeNode(num);
24+
while (st.size() > depth) {
25+
st.pop();
26+
}
27+
if (!st.empty()) {
28+
if (st.top()->left == nullptr) {
29+
st.top()->left = newNode;
30+
} else {
31+
st.top()->right = newNode;
32+
}
33+
}
34+
st.push(newNode);
35+
depth = 0;
36+
num = 0;
37+
}
38+
}
39+
TreeNode* res;
40+
while (!st.empty()) {
41+
res = st.top();
42+
st.pop();
43+
}
44+
return res;
45+
}
46+
};

0 commit comments

Comments
 (0)