Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8536ee6

Browse files
committed
Add cpp solution for problem 1024-1028
1 parent cf7575a commit 8536ee6

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 24 additions & 0 deletions
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+
};
Lines changed: 20 additions & 0 deletions
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+
};
Lines changed: 41 additions & 0 deletions
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+
};
Lines changed: 19 additions & 0 deletions
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+
};
Lines changed: 46 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /