Skip to content

Commit 067c0b9

Browse files
committedJul 6, 2017
482/559
1 parent 4fddd0f commit 067c0b9

34 files changed

+706
-2
lines changed
 

‎README.md

+30-2
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 454/495 problems.
5+
Solved 482/559 problems.
66

77
## Database
88

@@ -12,10 +12,39 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|635|[Design Log Storage System](https://leetcode.com/problems/design-log-storage-system)|[design-log-storage-system.cc](design-log-storage-system.cc)|
16+
|634|[Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array)|[find-the-derangement-of-an-array.cc](find-the-derangement-of-an-array.cc)|
17+
|633|[Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers)|[sum-of-square-numbers.cc](sum-of-square-numbers.cc)|
18+
|631|[Design Excel Sum Formula](https://leetcode.com/problems/design-excel-sum-formula)|[design-excel-sum-formula.cc](design-excel-sum-formula.cc)|
19+
|630|[Course Schedule III](https://leetcode.com/problems/course-schedule-iii)|[course-schedule-iii.cc](course-schedule-iii.cc)|
20+
|629|[K Inverse Pairs Array](https://leetcode.com/problems/k-inverse-pairs-array)|[k-inverse-pairs-array.cc](k-inverse-pairs-array.cc)|
21+
|628|[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers)|[maximum-product-of-three-numbers.cc](maximum-product-of-three-numbers.cc)|
22+
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees)|[merge-two-binary-trees.cc](merge-two-binary-trees.cc)|
23+
|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string)|[add-bold-tag-in-string.cc](add-bold-tag-in-string.cc)|
24+
|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number)|[valid-triangle-number.cc](valid-triangle-number.cc)|
25+
|604|[Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator)|[design-compressed-string-iterator.cc](design-compressed-string-iterator.cc)|
26+
|600|[Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones)|[non-negative-integers-without-consecutive-ones.cc](non-negative-integers-without-consecutive-ones.cc)|
27+
|599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists)|[minimum-index-sum-of-two-lists.cc](minimum-index-sum-of-two-lists.cc)|
28+
|598|[Range Addition II](https://leetcode.com/problems/range-addition-ii)|[range-addition-ii.cc](range-addition-ii.cc)|
29+
|594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence)|[longest-harmonious-subsequence.cc](longest-harmonious-subsequence.cc)|
30+
|593|[Valid Square](https://leetcode.com/problems/valid-square)|[valid-square.cc](valid-square.cc)|
31+
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction)|[fraction-addition-and-subtraction.cc](fraction-addition-and-subtraction.cc)|
32+
|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system)|[design-in-memory-file-system.cc](design-in-memory-file-system.cc)|
33+
|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence)|[erect-the-fence.cc](erect-the-fence.cc)|
34+
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings)|[delete-operation-for-two-strings.cc](delete-operation-for-two-strings.cc)|
35+
|582|[Kill Process](https://leetcode.com/problems/kill-process)|[kill-process.cc](kill-process.cc)|
36+
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray)|[shortest-unsorted-continuous-subarray.cc](shortest-unsorted-continuous-subarray.cc)|
37+
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies)|[distribute-candies.cc](distribute-candies.cc)|
38+
|565|[Array Nesting](https://leetcode.com/problems/array-nesting)|[array-nesting.cc](array-nesting.cc)|
39+
|556|[Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii)|[next-greater-element-iii.cc](next-greater-element-iii.cc)|
40+
|553|[Optimal Division](https://leetcode.com/problems/optimal-division)|[optimal-division.cc](optimal-division.cc)|
41+
|552|[Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii)|[student-attendance-record-ii.cc](student-attendance-record-ii.cc)|
1542
|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii)|[lonely-pixel-ii.cc](lonely-pixel-ii.cc)|
1643
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i)|[lonely-pixel-i.cc](lonely-pixel-i.cc)|
1744
|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail)|[freedom-trail.cc](freedom-trail.cc)|
1845
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum)|[most-frequent-subtree-sum.cc](most-frequent-subtree-sum.cc)|
46+
|507|[Perfect Number](https://leetcode.com/problems/perfect-number)|[perfect-number.cc](perfect-number.cc)|
47+
|504|[Base 7](https://leetcode.com/problems/base-7)|[base-7.cc](base-7.cc)|
1948
|502|[IPO](https://leetcode.com/problems/ipo)|[ipo.cc](ipo.cc)|
2049
|501|[Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree)|[find-mode-in-binary-search-tree.cc](find-mode-in-binary-search-tree.cc)|
2150
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row)|[keyboard-row.cc](keyboard-row.cc)|
@@ -417,7 +446,6 @@ See [database.md](database.md)
417446
|52|[N-Queens II](https://leetcode.com/problems/n-queens-ii)|[n-queens-ii.cc](n-queens-ii.cc)|
418447
|51|[N-Queens](https://leetcode.com/problems/n-queens)|[n-queens.cc](n-queens.cc)|
419448
|50|[Pow(x, n)](https://leetcode.com/problems/powx-n)|[powx-n.cc](powx-n.cc)|
420-
|49|[Group Anagrams](https://leetcode.com/problems/anagrams)|[anagrams.cc](anagrams.cc)|
421449
|48|[Rotate Image](https://leetcode.com/problems/rotate-image)|[rotate-image.cc](rotate-image.cc)|
422450
|47|[Permutations II](https://leetcode.com/problems/permutations-ii)|[permutations-ii.cc](permutations-ii.cc)|
423451
|46|[Permutations](https://leetcode.com/problems/permutations)|[permutations.cc](permutations.cc)|

‎add-bold-tag-in-string.cc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Add Bold Tag in String
2+
class Solution {
3+
public:
4+
string addBoldTag(string s, vector<string>& dict) {
5+
vector<int> a(s.size()+1);
6+
for (auto t: dict) {
7+
vector<int> pi(t.size());
8+
for (int j = 0, i = 1; i < t.size(); i++) {
9+
while (j && t[j] != t[i]) j = pi[j-1];
10+
if (t[j] == t[i]) j++;
11+
pi[i] = j;
12+
}
13+
for (int j = 0, i = 0; i < s.size(); i++) {
14+
while (j && t[j] != s[i]) j = pi[j-1];
15+
if (t[j] == s[i] && ++j == t.size()) {
16+
a[i-t.size()+1]++;
17+
a[i+1]--;
18+
j = pi[j-1];
19+
}
20+
}
21+
}
22+
string r;
23+
int c = 0, cc;
24+
for (int i = 0; i <= s.size(); i++) {
25+
cc = c+a[i];
26+
if (! c && cc) r += "<b>";
27+
else if (c && ! cc) r += "</b>";
28+
if (i < n) r += s[i];
29+
c = cc;
30+
}
31+
return r;
32+
}
33+
};

‎array-nesting.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Array Nesting
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int arrayNesting(vector<int>& nums) {
8+
int n = nums.size(), r = 0;
9+
vector<int> v(n, 0);
10+
REP(i, nums.size())
11+
if (! v[i]) {
12+
int c = 0;
13+
for (int j = i; ; ) {
14+
c++;
15+
v[j] = 1;
16+
j = nums[j];
17+
if (j == i) break;
18+
}
19+
r = max(r, c);
20+
}
21+
return r;
22+
}
23+
};

‎base-7.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Base 7
2+
class Solution {
3+
public:
4+
string convertToBase7(int num) {
5+
if (num < 0) return '-'+convertToBase7(-num);
6+
string r;
7+
do r.push_back(num%7+'0');
8+
while (num /= 7);
9+
reverse(r.begin(), r.end());
10+
return r;
11+
}
12+
};

‎codegolf/design-log-storage-system.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct LogSystem{
2+
map<int,string>a;
3+
void put(int i,string t){a[i]=t;}
4+
vector<int>retrieve(string s,string e,string g){
5+
int i=string("YMDHMS").find(g[0]);g[1]=='i'?i=4:0;
6+
for(s=s.substr(0,4+3*i),e=e.substr(0,4+3*i);i++<5;s+=":00",e+=":99");
7+
vector<int>r;
8+
for(auto&x:a)
9+
if(s<=(g=x.second)&&g<=e)
10+
r.push_back(x.first);
11+
return r;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Solution {
2+
int findDerangement(int n) {
3+
long s=0,i=2;
4+
for(;i<=n;i++)
5+
s=(i*s+1-i%2*2)%1000000007;
6+
return s;
7+
}
8+
};

‎course-schedule-iii.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Course Schedule III My SubmissionsBack To Contest
2+
class Solution {
3+
public:
4+
int scheduleCourse(vector<vector<int>>& as) {
5+
sort(as.begin(), as.end(), [](const vector<int>& x, const vector<int>& y) {
6+
return x[1] < y[1];
7+
});
8+
priority_queue<int> q;
9+
int s = 0;
10+
for (auto &a: as) {
11+
q.push(a[0]);
12+
if ((s += a[0]) > a[1])
13+
s -= q.top(), q.pop();
14+
}
15+
return q.size();
16+
}
17+
};

‎delete-operation-for-two-strings.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Delete Operation for Two Strings
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int minDistance(string a, string b) {
8+
int m = a.size(), n = b.size();
9+
vector<vector<int>> s(2, vector<int>(n+1));
10+
iota(s[0].begin(), s[0].end(), 0);
11+
REP(i, m) {
12+
s[i+1&1][0] = i+1;
13+
REP(j, n)
14+
s[i+1&1][j+1] = a[i] == b[j] ? s[i&1][j] : min(s[i+1&1][j], s[i&1][j+1])+1;
15+
}
16+
return s[m&1][n];
17+
}
18+
};

‎design-compressed-string-iterator.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Design Compressed String Iterator
2+
class StringIterator {
3+
string a;
4+
size_t i = 0, c = 0;
5+
char ch;
6+
public:
7+
StringIterator(string a) {
8+
this->a = a;
9+
}
10+
11+
char next() {
12+
if (c)
13+
return c--, ch;
14+
if (i >= a.size())
15+
return ' ';
16+
ch = a[i++];
17+
while (i < a.size() && isdigit(a[i]))
18+
c = c*10+a[i++]-'0';
19+
c--;
20+
return ch;
21+
}
22+
23+
bool hasNext() {
24+
return c || i < a.size();
25+
}
26+
};

‎design-excel-sum-formula.cc

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Design Excel Sum Formula
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Excel {
6+
vector<vector<long>> a, c, d;
7+
vector<vector<vector<tuple<int,int,int,int>>>> b;
8+
long tick = 0;
9+
10+
pair<int, int> f(string x) {
11+
return {atoi(x.c_str()+1)-1, x[0]-'A'};
12+
}
13+
long get_sum(const vector<tuple<int,int,int,int>>& xs, bool t = true) {
14+
long sum = 0;
15+
t && tick++;
16+
for (auto& r: xs)
17+
FOR(i, std::get<0>(r), std::get<2>(r))
18+
FOR(j, std::get<1>(r), std::get<3>(r))
19+
sum += get_cell(i, j);
20+
return sum;
21+
}
22+
long get_cell(int x, int y) {
23+
if (a[x][y] != LONG_MAX) return a[x][y];
24+
if (d[x][y] == tick) return c[x][y];
25+
d[x][y] = tick;
26+
return c[x][y] = get_sum(b[x][y], false);
27+
}
28+
public:
29+
Excel(int H, char W) {
30+
a = vector<vector<long>>(H, vector<long>(W-'A'+1));
31+
b.resize(H);
32+
REP(i, H) b[i].resize(W);
33+
c = vector<vector<long>>(H, vector<long>(W-'A'+1));
34+
d = vector<vector<long>>(H, vector<long>(W-'A'+1));
35+
}
36+
37+
void set(int r, char c, int v) {
38+
a[r-1][c-'A'] = v;
39+
}
40+
41+
int get(int r, char c) {
42+
tick++;
43+
return get_cell(r-1, c-'A');
44+
}
45+
46+
int sum(int r, char c, vector<string> strs) {
47+
r--;
48+
c -= 'A';
49+
a[r][c] = LONG_MAX;
50+
b[r][c].clear();
51+
for (auto& s: strs) {
52+
string::size_type g;
53+
int x0, y0, x1, y1;
54+
if ((g = s.find(':')) != string::npos) {
55+
tie(x0, y0) = f(s.substr(0, g));
56+
tie(x1, y1) = f(s.substr(g+1));
57+
} else
58+
tie(x0, y0) = tie(x1, y1) = f(s);
59+
b[r][c].emplace_back(x0, y0, x1+1, y1+1);
60+
}
61+
return get_sum(b[r][c]);
62+
}
63+
};

‎design-in-memory-file-system.cc

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Design In-Memory File System
2+
class FileSystem {
3+
struct Entry {
4+
bool file = false;
5+
string content;
6+
unordered_map<string, Entry*> ch;
7+
} *root = new Entry;
8+
public:
9+
vector<string> parts(string path) {
10+
vector<string> r;
11+
size_t i = 1, j = 1;
12+
for (; j <= path.size(); j++)
13+
if (i < j && (j == path.size() || path[j] == '/')) {
14+
r.emplace_back(path.substr(i, j-i));
15+
i = j+1;
16+
}
17+
return r;
18+
}
19+
20+
vector<string> ls(string path) {
21+
auto ps = parts(path);
22+
auto* x = root;
23+
for (auto p: ps)
24+
x = x->ch[p];
25+
if (x->file)
26+
return {ps.back()};
27+
vector<string> r;
28+
for (auto &i: x->ch)
29+
r.push_back(i.first);
30+
sort(r.begin(), r.end());
31+
return r;
32+
}
33+
34+
void mkdir(string path) {
35+
auto* x = root;
36+
for (auto p: parts(path)) {
37+
if (! x->ch.count(p))
38+
x->ch[p] = new Entry;
39+
x = x->ch[p];
40+
}
41+
}
42+
43+
void addContentToFile(string filePath, string content) {
44+
auto* x = root;
45+
for (auto p: parts(filePath)) {
46+
if (! x->ch.count(p))
47+
x->ch[p] = new Entry;
48+
x = x->ch[p];
49+
}
50+
x->file = true;
51+
x->content += content;
52+
}
53+
54+
string readContentFromFile(string filePath) {
55+
auto* x = root;
56+
for (auto p: parts(filePath))
57+
x = x->ch[p];
58+
return x->content;
59+
}
60+
};

‎design-log-storage-system.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Design Log Storage System
2+
class LogSystem {
3+
unordered_map<int, string> a;
4+
public:
5+
void put(int id, string timestamp) {
6+
a[id] = timestamp;
7+
}
8+
9+
vector<int> retrieve(string s, string e, string gra) {
10+
vector<string> gs{"Year", "Month", "Day", "Hour", "Minute", "Second"};
11+
int i = find(gs.begin(), gs.end(), gra)-gs.begin();
12+
s = s.substr(0, 4+3*i);
13+
e = e.substr(0, 4+3*i);
14+
for (; i < 5; i++) s += ":00", e += ":99";
15+
vector<int> r;
16+
for (auto& x: a)
17+
if (s <= x.second && x.second <= e)
18+
r.push_back(x.first);
19+
return r;
20+
}
21+
};

‎distribute-candies.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Distribute Candies
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int distributeCandies(vector<int>& candies) {
7+
auto r = candies.size()/2;
8+
sort(ALL(candies));
9+
candies.erase(unique(ALL(candies)), candies.end());
10+
return min(r, candies.size());
11+
}
12+
};

‎erect-the-fence.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Erect the Fence
2+
class Solution {
3+
static int cross(Point o, Point a, Point b) {
4+
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
5+
}
6+
static bool cmp(Point a, Point b) {
7+
return a.x < b.x || a.x == b.x && a.y < b.y;
8+
}
9+
static bool eq(Point a, Point b) {
10+
return a.x == b.x && a.y == b.y;
11+
}
12+
public:
13+
vector<Point> outerTrees(vector<Point>& A) {
14+
vector<Point> ret;
15+
int n = A.size();
16+
if (n <= 1) return A;
17+
sort(A.begin(), A.end(), cmp);
18+
for (int i = 0; i < n; ret.push_back(A[i++]))
19+
while (ret.size() > 1 && cross(ret[ret.size()-2], ret.back(), A[i]) > 0)
20+
ret.pop_back();
21+
for (int i = n-2, t = ret.size(); i >= 0; ret.push_back(A[i--]))
22+
while (ret.size() > t && cross(ret[ret.size()-2], ret.back(), A[i]) > 0)
23+
ret.pop_back();
24+
ret.pop_back();
25+
sort(ret.begin(), ret.end(), cmp);
26+
ret.erase(unique(ret.begin(), ret.end(), eq), ret.end());
27+
return ret;
28+
}
29+
};

‎find-the-derangement-of-an-array.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Find the Derangement of An Array
2+
class Solution {
3+
public:
4+
int findDerangement(int n) {
5+
long s = 0;
6+
for (int i = 2; i <= n; i++)
7+
s = (i*s+(i%2?-1:1))%1000000007;
8+
return s;
9+
}
10+
};

‎fraction-addition-and-subtraction.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Fraction Addition and Subtraction
2+
class Solution {
3+
public:
4+
string fractionAddition(string s) {
5+
int p = 0, q = 1, p1, q1, t;
6+
for (size_t i = 0, j; i < s.size(); i = j) {
7+
j = s.find_first_of("+-", i+1);
8+
if (j == string::npos) j = s.size();
9+
auto k = s.find('/', i);
10+
int x = stol(s.substr(i, k-i)), y = stol(s.substr(k+1, j));
11+
p1 = p*y+q*x;
12+
q1 = q*y;
13+
t = __gcd(abs(p1), q1);
14+
p = p1/t;
15+
q = q1/t;
16+
}
17+
return to_string(p)+"/"+to_string(q);
18+
}
19+
};

‎freedom-trail.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Freedom Trail
12
#define ALL(x) (x).begin(), (x).end()
23
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
34
#define REP(i, n) FOR(i, 0, n)

‎k-inverse-pairs-array.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// K Inverse Pairs Array
2+
class Solution {
3+
static const int MOD = 1000000007;
4+
public:
5+
int kInversePairs(int n, int k) {
6+
int a[k+1] = {}, b[k+1];
7+
a[0] = 1;
8+
for (int i = 2; i <= n; i++) {
9+
for (int s = 0, j = 0; j <= k; j++) {
10+
(s += a[j]) %= MOD;
11+
if (j >= i) (s += MOD-a[j-i]) %= MOD;
12+
b[j] = s;
13+
}
14+
copy_n(b, k+1, a);
15+
}
16+
return a[k];
17+
}
18+
};

‎kill-process.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Kill Process
2+
class Solution {
3+
public:
4+
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
5+
unordered_map<int, vector<int>> e;
6+
for (size_t i = 0; i < pid.size(); i++)
7+
e[ppid[i]].push_back(pid[i]);
8+
vector<int> q{kill};
9+
for (size_t i = 0; i < q.size(); i++)
10+
q.insert(q.end(), e[q[i]].begin(), e[q[i]].end());
11+
return q;
12+
}
13+
};

‎longest-harmonious-subsequence.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Longest Harmonious Subsequence
2+
class Solution {
3+
public:
4+
int findLHS(vector<int>& nums) {
5+
int s = 0;
6+
unordered_map<int, int> c;
7+
for (int x: nums)
8+
c[x]++;
9+
for (auto &x: c)
10+
if (c.count(x.first+1))
11+
s = max(s, x.second+c[x.first+1]);
12+
return s;
13+
}
14+
};

‎maximum-product-of-three-numbers.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Maximum Product of Three Numbers
2+
class Solution {
3+
public:
4+
int maximumProduct(vector<int>& a) {
5+
size_t n = a.size();
6+
sort(a.begin(), a.end());
7+
return max(max(a[n-3]*a[n-2]*a[n-1],
8+
a[0]*a[n-2]*a[n-1]),
9+
max(a[0]*a[1]*a[n-1],
10+
a[0]*a[1]*a[2]));
11+
}
12+
};

‎merge-two-binary-trees.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Merge Two Binary Trees
2+
class Solution {
3+
void f(TreeNode*& s, TreeNode* a, TreeNode* b) {
4+
if (! a && ! b) return;
5+
s = new TreeNode((a ? a->val : 0) + (b ? b->val : 0));
6+
f(s->left, a ? a->left : 0, b ? b->left : 0);
7+
f(s->right, a ? a->right : 0, b ? b->right : 0);
8+
}
9+
public:
10+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
11+
TreeNode* r = 0;
12+
f(r, t1, t2);
13+
return r;
14+
}
15+
};

‎minimum-index-sum-of-two-lists.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Minimum Index Sum of Two Lists
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
8+
int s = list1.size()+list2.size();
9+
vector<string> r;
10+
unordered_map<string, int> a;
11+
REP(i, list1.size())
12+
a[list1[i]] = i;
13+
REP(i, list2.size())
14+
if (a.count(list2[i]))
15+
s = min(s, int(a[list2[i]]+i));
16+
REP(i, list2.size())
17+
if (a.count(list2[i]) && a[list2[i]]+i == s)
18+
r.push_back(list2[i]);
19+
return r;
20+
}
21+
};

‎next-greater-element-iii.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Next Greater Element III
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int nextGreaterElement(int n) {
7+
vector<int> a;
8+
do a.push_back(n%10);
9+
while (n /= 10);
10+
reverse(ALL(a));
11+
if (! next_permutation(ALL(a)))
12+
return -1;
13+
long r = 0;
14+
for (auto x: a)
15+
r = r*10+x;
16+
return r > INT_MAX ? -1 : r;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Non-negative Integers without Consecutive Ones
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int findIntegers(int num) {
8+
const int N = 32-__builtin_clz(num);
9+
int a[N], b[N] = {}, n = 0, s = 0;
10+
do a[n++] = num%2;
11+
while (num /= 2);
12+
b[0] = 1;
13+
b[1] = 2;
14+
FOR(i, 2, n)
15+
b[i] = b[i-2]+b[i-1];
16+
ROF(i, 0, n) {
17+
if (a[i]) {
18+
s += b[i];
19+
if (i+1 < n && a[i+1]) break;
20+
}
21+
if (! i) s++;
22+
}
23+
return s;
24+
}
25+
};

‎optimal-division.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Optimal Division
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
4+
class Solution {
5+
public:
6+
string optimalDivision(vector<int>& a) {
7+
if (a.size() == 1)
8+
return to_string(a[0]);
9+
if (a.size() == 2)
10+
return to_string(a[0])+'/'+to_string(a[1]);
11+
string r = to_string(a[0])+"/(";
12+
FOR(i, 1, a.size()) {
13+
if (i > 1) r += '/';
14+
r += to_string(a[i]);
15+
};
16+
return r+')';
17+
}
18+
};

‎perfect-number.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Perfect Number
2+
class Solution {
3+
public:
4+
bool checkPerfectNumber(int num) {
5+
int s = 1;
6+
for (int i = 2; i*i <= num; i++)
7+
if (num%i == 0) {
8+
s += i;
9+
if (i*i < num)
10+
s += num/i;
11+
}
12+
return num > 1 && s == num;
13+
}
14+
};

‎range-addition-ii.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Range Addition II
2+
class Solution {
3+
public:
4+
int maxCount(int m, int n, vector<vector<int>>& ops) {
5+
int r = m, c = n;
6+
for (auto &op: ops) {
7+
r = min(r, op[0]);
8+
c = min(c, op[1]);
9+
}
10+
return r*c;
11+
}
12+
};
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Shortest Unsorted Continuous Subarray
2+
class Solution {
3+
public:
4+
int findUnsortedSubarray(vector<int>& a) {
5+
vector<int> b = a;
6+
sort(b.begin(), b.end());
7+
int l = 0, r = a.size();
8+
while (l < r && a[l] == b[l]) l++;
9+
while (l < r && a[r-1] == b[r-1]) r--;
10+
return r-l;
11+
}
12+
};
13+
14+
/// O(n)
15+
16+
class Solution {
17+
public:
18+
int findUnsortedSubarray(vector<int>& a) {
19+
int s = 0, l = 0, r = a.size();
20+
while (l+1 < a.size() && a[l] <= a[l+1]) l++;
21+
while (r > 1 && a[r-2] <= a[r-1]) r--;
22+
if (l+1 >= r) return 0;
23+
int mn = *min_element(&a[l], &a[r]),
24+
mx = *max_element(&a[l], &a[r]);
25+
while (l && mn < a[l-1]) l--;
26+
while (r < a.size() && a[r] < mx) r++;
27+
return r-l;
28+
}
29+
};

‎split-assembled-strings.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Split Assembled Strings
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
string splitLoopedString(vector<string>& strs) {
9+
int n = strs.size();
10+
for (auto &s: strs) {
11+
string t = s;
12+
reverse(ALL(t));
13+
if (t > s) s = t;
14+
}
15+
string ans;
16+
REP(i, n)
17+
REP(e, 2) {
18+
REP(j, strs[i].size()) {
19+
string r = strs[i].substr(j);
20+
FOR(k, i+1, n) r += strs[k];
21+
REP(k, i) r += strs[k];
22+
r += strs[i].substr(0, j);
23+
ans = max(ans, r);
24+
}
25+
reverse(ALL(strs[i]));
26+
}
27+
return ans;
28+
}
29+
};
30+

‎student-attendance-record-ii.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Student Attendance Record II
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
static const int MOD = 1000000007;
7+
public:
8+
int checkRecord(int n) {
9+
int dp[2][3] = {}, dp1[2][3];
10+
dp[0][0] = 1;
11+
REP(_, n) {
12+
memset(dp1, 0, sizeof dp1);
13+
REP(i, 2)
14+
REP(j, 3) {
15+
(dp1[i][0] += dp[i][j]) %= MOD;
16+
if (! i)
17+
(dp1[i+1][0] += dp[i][j]) %= MOD;
18+
if (j < 2)
19+
(dp1[i][j+1] += dp[i][j]) %= MOD;
20+
}
21+
memcpy(dp, dp1, sizeof dp1);
22+
}
23+
int r = 0;
24+
REP(i, 2)
25+
REP(j, 3)
26+
(r += dp[i][j]) %= MOD;
27+
return r;
28+
}
29+
};

‎sum-of-square-numbers.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Sum of Square Numbers
2+
class Solution {
3+
public:
4+
bool judgeSquareSum(int c) {
5+
for (long i = 0; i*i <= c; i++) {
6+
long j = sqrt(c-i*i);
7+
if (i*i+j*j == c) return true;
8+
}
9+
return false;
10+
}
11+
};

‎valid-square.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Valid Square
2+
class Solution {
3+
int d(vector<int>& p, vector<int>& q) {
4+
return (p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1]);
5+
}
6+
public:
7+
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
8+
unordered_set<int> s{d(p1,p2), d(p1,p3), d(p1,p4), d(p2,p3), d(p2,p4), d(p3,p4)};
9+
return ! s.count(0) && s.size() == 2;
10+
}
11+
};

‎valid-triangle-number.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Valid Triangle Number
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
int triangleNumber(vector<int>& a) {
9+
sort(ALL(a));
10+
int n = a.size(), r = 0;
11+
REP(i, n)
12+
if (a[i]) {
13+
int k = i+1;
14+
FOR(j, i+1, n) {
15+
while (k < n && a[i]+a[j] > a[k]) k++;
16+
r += k-j-1;
17+
}
18+
}
19+
return r;
20+
}
21+
};

0 commit comments

Comments
 (0)
Please sign in to comment.