Skip to content
This repository was archived by the owner on May 16, 2021. It is now read-only.

Commit 485a257

Browse files
committed
14 problems
1 parent 8c20a8a commit 485a257

14 files changed

+563
-0
lines changed

baseball-game_1_AC.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <cstdio>
2+
#include <string>
3+
#include <vector>
4+
using std::sscanf;
5+
using std::string;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int calPoints(vector<string>& ops) {
11+
vector<int> v;
12+
int num;
13+
for (auto &op: ops) {
14+
if (op == "C") {
15+
v.pop_back();
16+
} else if (op == "D") {
17+
v.push_back(v.back() * 2);
18+
} else if (op == "+") {
19+
num = v.size();
20+
v.push_back(v[num - 1] + v[num - 2]);
21+
} else {
22+
sscanf(op.data(), "%d", &num);
23+
v.push_back(num);
24+
}
25+
}
26+
27+
int sum = 0;
28+
for (auto &x: v) {
29+
sum += x;
30+
}
31+
32+
return sum;
33+
}
34+
};

employee-importance_1_AC.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// IIRC, a same problem has already been posted.
2+
/*
3+
// Employee info
4+
class Employee {
5+
public:
6+
// It's the unique ID of each node.
7+
// unique id of this employee
8+
int id;
9+
// the importance value of this employee
10+
int importance;
11+
// the id of direct subordinates
12+
vector<int> subordinates;
13+
};
14+
*/
15+
#include <unordered_map>
16+
using std::unordered_map;
17+
18+
class Solution {
19+
public:
20+
int getImportance(vector<Employee*> employees, int id) {
21+
unordered_map<int, int> dj;
22+
unordered_map<int, int> imp;
23+
24+
for (auto &e: employees) {
25+
if (dj.find(e->id) == dj.end()) {
26+
dj[e->id] = e->id;
27+
}
28+
for (auto &sid: e->subordinates) {
29+
if (sid == id) {
30+
continue;
31+
}
32+
dj[sid] = e->id;
33+
}
34+
}
35+
36+
int sum = 0;
37+
for (auto &e: employees) {
38+
if (findRoot(dj, e->id) == id) {
39+
sum += e->importance;
40+
}
41+
}
42+
43+
return sum;
44+
}
45+
private:
46+
int findRoot(unordered_map<int, int> &dj, int x) {
47+
if (dj.find(x) == dj.end()) {
48+
return x;
49+
}
50+
int r = x;
51+
while (r != dj[r]) {
52+
r = dj[r];
53+
}
54+
int k = x;
55+
while (x != r) {
56+
x = dj[x];
57+
dj[k] = r;
58+
k = x;
59+
}
60+
return r;
61+
}
62+
};

find-k-closest-elements_1_AC.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <algorithm>
2+
#include <queue>
3+
#include <vector>
4+
using std::priority_queue;
5+
using std::reverse;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
11+
vector<int> res;
12+
if (k <= 0) {
13+
return res;
14+
}
15+
16+
priority_queue<int, vector<int>, greater<int>> pq1;
17+
priority_queue<int, vector<int>, less<int>> pq2;
18+
19+
int i;
20+
for (i = 0; i < k; ++i) {
21+
if (arr[i] >= x) {
22+
pq2.push(arr[i]);
23+
} else {
24+
pq1.push(arr[i]);
25+
}
26+
}
27+
28+
int n = arr.size();
29+
for (i = k; i < n; ++i) {
30+
if (arr[i] >= x) {
31+
pq2.push(arr[i]);
32+
} else {
33+
pq1.push(arr[i]);
34+
}
35+
if (pq1.empty()) {
36+
pq2.pop();
37+
} else if (pq2.empty()) {
38+
pq1.pop();
39+
} else if (x - pq1.top() > pq2.top() - x) {
40+
pq1.pop();
41+
} else {
42+
pq2.pop();
43+
}
44+
}
45+
46+
int n1 = pq1.size();
47+
while (!pq1.empty()) {
48+
res.push_back(pq1.top());
49+
pq1.pop();
50+
}
51+
while (!pq2.empty()) {
52+
res.push_back(pq2.top());
53+
pq2.pop();
54+
}
55+
reverse(res.begin() + n1, res.end());
56+
57+
return res;
58+
}
59+
};

image-smoother_1_AC.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
4+
int n = M.size();
5+
int m = M[0].size();
6+
vector<vector<int>> M1(n, vector<int>(m, 0));
7+
8+
static int offset[9][2] = {
9+
{-1, -1}, {-1, 0}, {-1, +1},
10+
{ 0, -1}, { 0, 0}, { 0, +1},
11+
{+1, -1}, {+1, 0}, {+1, +1}
12+
};
13+
14+
int i, j;
15+
int i1, j1;
16+
int k;
17+
int cc;
18+
int sum;
19+
for (i = 0; i < n; ++i) {
20+
for (j = 0; j < m; ++j) {
21+
sum = 0;
22+
cc = 0;
23+
for (k = 0; k < 9; ++k) {
24+
i1 = i + offset[k][0];
25+
j1 = j + offset[k][1];
26+
if (i1 < 0 || i1 > n - 1 || j1 < 0 || j1 > m - 1) {
27+
continue;
28+
}
29+
sum += M[i1][j1];
30+
++cc;
31+
}
32+
M1[i][j] = sum / cc;
33+
}
34+
}
35+
36+
return M1;
37+
}
38+
};

judge-route-circle_1_AC.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <string>
2+
#include <unordered_map>
3+
using std::string;
4+
using std::unordered_map;
5+
6+
class Solution {
7+
public:
8+
bool judgeCircle(string moves) {
9+
unordered_map<char, int> um;
10+
for (auto &c: moves) {
11+
++um[c];
12+
}
13+
return (um['L'] == um['R'] && um['U'] == um['D']);
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// I'm getting bored of this.
2+
#include <algorithm>
3+
#include <vector>
4+
using std::max;
5+
using std::vector;
6+
7+
class Solution {
8+
public:
9+
int findLengthOfLCIS(vector<int>& nums) {
10+
int n = nums.size();
11+
int i, j;
12+
int res = 0;
13+
i = 0;
14+
while (i < n) {
15+
j = i + 1;
16+
while (j < n && nums[j] > nums[j - 1]) {
17+
++j;
18+
}
19+
res = max(res, j - i);
20+
i = j;
21+
}
22+
return res;
23+
}
24+
};

longest-univalue-path_1_AC.cpp

+41
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+
#include <algorithm>
11+
#include <unordered_map>
12+
using std::max;
13+
using std::unordered_map;
14+
15+
class Solution {
16+
public:
17+
int longestUnivaluePath(TreeNode* root) {
18+
if (root == NULL) {
19+
return 0;
20+
}
21+
if (um.find(root) != um.end()) {
22+
return um[root];
23+
}
24+
25+
int r1 = traverse(root->left, root->val) + traverse(root->right, root->val);
26+
int r2 = longestUnivaluePath(root->left);
27+
int r3 = longestUnivaluePath(root->right);
28+
um[root] = max(r1, max(r2, r3));
29+
30+
return um[root];
31+
}
32+
private:
33+
unordered_map<TreeNode *, int> um;
34+
35+
int traverse(TreeNode *root, int val) {
36+
if (root == NULL || root->val != val) {
37+
return 0;
38+
}
39+
return 1 + max(traverse(root->left, val), traverse(root->right, val));
40+
}
41+
};

maximum-width-of-binary-tree_1_AC.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
#include <algorithm>
11+
#include <cstdint>
12+
#include <unordered_map>
13+
using std::max;
14+
using std::min;
15+
using std::unordered_map;
16+
17+
class Solution {
18+
public:
19+
int widthOfBinaryTree(TreeNode* root) {
20+
if (root == NULL) {
21+
return 0;
22+
}
23+
24+
traverse(root, 0, 0);
25+
26+
int64_t res = 0;
27+
int dep;
28+
for (auto &p: um_max) {
29+
dep = p.first;
30+
res = max(res, um_max[dep] - um_min[dep]);
31+
}
32+
um_max.clear();
33+
um_min.clear();
34+
35+
return res + 1;
36+
}
37+
private:
38+
unordered_map<int, int64_t> um_max, um_min;
39+
40+
void traverse(TreeNode *root, int dep, int64_t path) {
41+
if (root == NULL) {
42+
return;
43+
}
44+
45+
int64_t cur;
46+
if (um_max.find(dep) == um_max.end()) {
47+
um_max[dep] = path;
48+
} else {
49+
cur = um_max[dep];
50+
um_max[dep] = max(cur, path);
51+
}
52+
53+
if (um_min.find(dep) == um_min.end()) {
54+
um_min[dep] = path;
55+
} else {
56+
cur = um_min[dep];
57+
um_min[dep] = min(cur, path);
58+
}
59+
60+
traverse(root->left, dep + 1, path << 1);
61+
traverse(root->right, dep + 1, (path << 1) | 1);
62+
}
63+
};

non-decreasing-array_1_AC.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Messy.
2+
#include <climits>
3+
4+
class Solution {
5+
public:
6+
bool checkPossibility(vector<int>& nums) {
7+
int n = nums.size();
8+
int cc = 0;
9+
int maxval = INT_MIN, old_maxval = INT_MIN;
10+
11+
int i;
12+
for (i = 0; i < n; ++i) {
13+
if (nums[i] >= maxval) {
14+
old_maxval = maxval;
15+
maxval = nums[i];
16+
} else {
17+
if (cc > 0) {
18+
return false;
19+
}
20+
++cc;
21+
if (nums[i] >= old_maxval) {
22+
maxval = nums[i];
23+
}
24+
}
25+
}
26+
27+
return true;
28+
}
29+
};

0 commit comments

Comments
 (0)