Skip to content

Commit 5ca9d89

Browse files
committed
454/495
1 parent 7c0a75f commit 5ca9d89

20 files changed

+442
-3
lines changed

README.md

Lines changed: 19 additions & 1 deletion
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 436/442 problems.
5+
Solved 454/495 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii)|[lonely-pixel-ii.cc](lonely-pixel-ii.cc)|
16+
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i)|[lonely-pixel-i.cc](lonely-pixel-i.cc)|
17+
|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail)|[freedom-trail.cc](freedom-trail.cc)|
18+
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum)|[most-frequent-subtree-sum.cc](most-frequent-subtree-sum.cc)|
19+
|502|[IPO](https://leetcode.com/problems/ipo)|[ipo.cc](ipo.cc)|
20+
|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)|
21+
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row)|[keyboard-row.cc](keyboard-row.cc)|
22+
|495|[Teemo Attacking](https://leetcode.com/problems/teemo-attacking)|[teemo-attacking.cc](teemo-attacking.cc)|
23+
|494|[Target Sum](https://leetcode.com/problems/target-sum)|[target-sum.cc](target-sum.cc)|
24+
|491|[Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences)|[increasing-subsequences.cc](increasing-subsequences.cc)|
25+
|490|[The Maze](https://leetcode.com/problems/the-maze)|[the-maze.cc](the-maze.cc)|
26+
|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones)|[max-consecutive-ones.cc](max-consecutive-ones.cc)|
27+
|483|[Smallest Good Base](https://leetcode.com/problems/smallest-good-base)|[smallest-good-base.cc](smallest-good-base.cc)|
28+
|482|[License Key Formatting](https://leetcode.com/problems/license-key-formatting)|[license-key-formatting.cc](license-key-formatting.cc)|
29+
|481|[Magical String](https://leetcode.com/problems/magical-string)|[magical-string.cc](magical-string.cc)|
30+
|480|[Sliding Window Median](https://leetcode.com/problems/sliding-window-median)|[sliding-window-median.cc](sliding-window-median.cc)|
1531
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance)|[total-hamming-distance.cc](total-hamming-distance.cc)|
32+
|476|[Number Complement](https://leetcode.com/problems/number-complement)|[number-complement.cc](number-complement.cc)|
1633
|475|[Heaters](https://leetcode.com/problems/heaters)|[heaters.cc](heaters.cc)|
1734
|474|[Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes)|[ones-and-zeroes.cc](ones-and-zeroes.cc)|
1835
|473|[Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square)|[matchsticks-to-square.cc](matchsticks-to-square.cc)|
1936
|472|[Concatenated Words](https://leetcode.com/problems/concatenated-words)|[concatenated-words.cc](concatenated-words.cc)|
37+
|469|[Convex Polygon](https://leetcode.com/problems/convex-polygon)|[convex-polygon.cc](convex-polygon.cc)|
2038
|468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address)|[validate-ip-address.cc](validate-ip-address.cc)|
2139
|467|[Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string)|[unique-substrings-in-wraparound-string.cc](unique-substrings-in-wraparound-string.cc)|
2240
|466|[Count The Repetitions](https://leetcode.com/problems/count-the-repetitions)|[count-the-repetitions.cc](count-the-repetitions.cc)|

convex-polygon.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Convex Polygon
2+
class Solution {
3+
public:
4+
bool isConvex(vector<vector<int>>& points) {
5+
if (points.size() < 3) return false;
6+
int f = -1;
7+
for (int i = 0; i < points.size(); i++) {
8+
auto pre = i == 0 ? points.back() : points[i - 1], post = i == points.size() - 1 ? points[0] : points[i + 1];
9+
int t = (points[i][0]-pre[0])*(post[1]-points[i][1]) - (post[0]-points[i][0])*(points[i][1]-pre[1]);
10+
if (f == -1) {
11+
if (t) f = t > 0;
12+
} else if (f != t > 0)
13+
return false;
14+
}
15+
return true;
16+
}
17+
};

find-mode-in-binary-search-tree.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Find Mode in Binary Search Tree
2+
class Solution {
3+
vector<int> a;
4+
void f(TreeNode* x) {
5+
if (! x) return;
6+
a.push_back(x->val);
7+
f(x->left);
8+
f(x->right);
9+
}
10+
public:
11+
vector<int> findMode(TreeNode* root) {
12+
a.clear();
13+
f(root);
14+
sort(a.begin(), a.end());
15+
int c = 0;
16+
vector<int> r;
17+
for (int i = 0, j = 0; i < a.size(); i = j) {
18+
for (; j < a.size() && a[i] == a[j]; j++);
19+
c = max(c, j-i);
20+
}
21+
for (int i = 0, j = 0; i < a.size(); i = j) {
22+
for (; j < a.size() && a[i] == a[j]; j++);
23+
if (j-i == c)
24+
r.push_back(a[i]);
25+
}
26+
return r;
27+
}
28+
};

freedom-trail.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define ALL(x) (x).begin(), (x).end()
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 findRotateSteps(string ring, string key) {
8+
int n = ring.size();
9+
vector<int> s(n, INT_MAX/2), ss(n);
10+
s[0] = 0;
11+
for (char c: key) {
12+
fill(ALL(ss), INT_MAX/2);
13+
REP(i, n)
14+
if (ring[i] == c)
15+
REP(j, n)
16+
ss[i] = min(ss[i], s[j]+min(abs(i-j), n-abs(i-j)));
17+
s.swap(ss);
18+
}
19+
return key.size()+*min_element(ALL(s));
20+
}
21+
};

increasing-subsequences.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Increasing Subsequences
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+
vector<vector<int>> findSubsequences(vector<int>& nums) {
9+
int n = nums.size();
10+
vector<vector<int>> r;
11+
REP(i, 1<<n) {
12+
int last = -1000;
13+
vector<int> a;
14+
REP(j, n)
15+
if (i>>j&1) {
16+
if (last > nums[j])
17+
goto out;
18+
a.push_back(last = nums[j]);
19+
}
20+
if (a.size() > 1)
21+
r.push_back(a);
22+
out:;
23+
}
24+
sort(ALL(r));
25+
r.erase(unique(ALL(r)), r.end());
26+
return r;
27+
}
28+
};

ipo.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// IPO
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 findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
8+
vector<pair<int, int>> a;
9+
REP(i, Profits.size())
10+
a.emplace_back(Capital[i], Profits[i]);
11+
sort(a.begin(), a.end());
12+
multiset<int> b;
13+
for (int i = 0; k--; ) {
14+
for (; i < a.size() && a[i].first <= W; i++)
15+
b.insert(a[i].second);
16+
if (b.empty()) break;
17+
W += *b.rbegin();
18+
b.erase(prev(b.end()));
19+
}
20+
return W;
21+
}
22+
};

keyboard-row.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Keyboard Row
2+
class Solution {
3+
public:
4+
vector<string> findWords(vector<string>& words) {
5+
vector<string> r;
6+
string a = "qwertyuiop",
7+
b = "asdfghjkl",
8+
c = "zxcvbnm";
9+
int d[127];
10+
for (auto i: a) d[i] = d[i-32] = 0;
11+
for (auto i: b) d[i] = d[i-32] = 1;
12+
for (auto i: c) d[i] = d[i-32] = 2;
13+
for (auto word: words) {
14+
int e[3] = {};
15+
for (auto c: word)
16+
e[d[c]]++;
17+
if (*max_element(e, e+3) == word.size())
18+
r.push_back(word);
19+
}
20+
return r;
21+
}
22+
};

license-key-formatting.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// License Key Formatting
2+
class Solution {
3+
public:
4+
string licenseKeyFormatting(string S, int K) {
5+
S.erase(remove(S.begin(), S.end(), '-'), S.end());
6+
transform(S.begin(), S.end(), S.begin(), ::toupper);
7+
string r;
8+
int t = S.size()%K;
9+
if (! t) t = K;
10+
for (int i = 0; i < S.size(); i += t, t = K) {
11+
if (i) r += '-';
12+
r += S.substr(i, t);
13+
}
14+
return r;
15+
}
16+
};

lonely-pixel-i.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
2+
#define REP(i, n) FOR(i, 0, n)
3+
4+
class Solution {
5+
public:
6+
int findLonelyPixel(vector<vector<char>>& a) {
7+
if (a.empty()) return 0;
8+
int s = 0, m = a.size(), n = a[0].size();
9+
vector<int> r(m), c(n);
10+
REP(i, m)
11+
REP(j, n) {
12+
r[i] += a[i][j] == 'B';
13+
c[j] += a[i][j] == 'B';
14+
}
15+
REP(i, m)
16+
REP(j, n)
17+
if (a[i][j] == 'B' && r[i] == 1 && c[j] == 1)
18+
s++;
19+
return s;
20+
}
21+
};

lonely-pixel-ii.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
2+
#define REP(i, n) FOR(i, 0, n)
3+
4+
class Solution {
5+
public:
6+
int findBlackPixel(vector<vector<char>>& a, int k) {
7+
if (a.empty()) return 0;
8+
int s = 0, m = a.size(), n = a[0].size();
9+
vector<int> r(m), c(n);
10+
REP(i, m)
11+
REP(j, n) {
12+
r[i] += a[i][j] == 'B';
13+
c[j] += a[i][j] == 'B';
14+
}
15+
REP(j, n)
16+
REP(i, m)
17+
if (a[i][j] == 'B' && r[i] == k && c[j] == k) {
18+
REP(ii, m)
19+
if (a[ii][j] == 'B' && a[ii] != a[i]) goto out;
20+
s += k;
21+
out:
22+
break;
23+
}
24+
return s;
25+
}
26+
};

magical-string.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Magical String
2+
class Solution {
3+
public:
4+
int magicalString(int n) {
5+
int i = 2, j = 3;
6+
char t = '1';
7+
string s = "122";
8+
for (; s.size() < n; i++, t ^= 3) {
9+
s += t;
10+
if (s[i] == '2') s += t;
11+
}
12+
return count(s.begin(), s.begin()+n, '1');
13+
}
14+
};

max-consecutive-ones.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Max Consecutive Ones
2+
class Solution {
3+
public:
4+
int findMaxConsecutiveOnes(vector<int>& nums) {
5+
int r = 0;
6+
for (int i = 0; i < nums.size(); )
7+
if (nums[i] != 1)
8+
i++;
9+
else {
10+
int j = i;
11+
while (++j < nums.size() && nums[j] == 1);
12+
r = max(r, j-i);
13+
}
14+
return r;
15+
}
16+
};

most-frequent-subtree-sum.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Most Frequent Subtree Sum
2+
class Solution {
3+
unordered_map<int, int> c;
4+
int f(TreeNode* x) {
5+
int s = 0;
6+
if (x->left) s += f(x->left);
7+
if (x->right) s += f(x->right);
8+
++c[s += x->val];
9+
return s;
10+
}
11+
public:
12+
vector<int> findFrequentTreeSum(TreeNode* root) {
13+
vector<int> r;
14+
f(root);
15+
int y = 0;
16+
for (auto& i: c)
17+
if (i.second > y)
18+
y = i.second;
19+
for (auto& i: c)
20+
if (i.second == y)
21+
r.push_back(i.first);
22+
return r;
23+
}
24+
};

number-complement.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Number Complement
2+
class Solution {
3+
public:
4+
int findComplement(int num) {
5+
return (1u << 32-__builtin_clz(num))-1-num;
6+
}
7+
};

reverse-integer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
class Solution {
33
public:
44
int reverse(int x) {
5-
int y = 0;
5+
long y = 0;
66
do {
77
y = y*10+x%10;
88
x /= 10;
99
} while (x);
10-
return y;
10+
return INT_MIN <= y && y <= INT_MAX ? y : 0;
1111
}
1212
};

sliding-window-median.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Sliding Window Median
2+
class Solution {
3+
public:
4+
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
5+
vector<double> r;
6+
multiset<int> a(nums.begin(), nums.begin()+k);
7+
auto mid = next(a.begin(), (k-1)/2);
8+
for (int i = k; ; i++) {
9+
r.push_back((double(*mid)+*next(mid, 1-k%2))/2);
10+
if (i == nums.size()) break;
11+
a.insert(nums[i]);
12+
if (nums[i] < *mid) --mid;
13+
if (nums[i-k] <= *mid) ++mid;
14+
a.erase(a.lower_bound(nums[i-k]));
15+
}
16+
return r;
17+
}
18+
};
19+
///
20+
class Solution {
21+
vector<int> fenwick;
22+
void add(int x, int n, int v) {
23+
for (; x < n; x |= x+1)
24+
fenwick[x] += v;
25+
}
26+
int select(int k, int n) {
27+
int r = -1;
28+
for (int x = 1<<31-__builtin_clz(n); x; x >>= 1)
29+
if (r+x < n && fenwick[r+x] <= k)
30+
k -= fenwick[r += x];
31+
return r+1;
32+
}
33+
public:
34+
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
35+
vector<double> r;
36+
vector<int> a = nums;
37+
int n = nums.size();
38+
fenwick.assign(n, 0);
39+
sort(a.begin(), a.end());
40+
for (int i = 0; i < k; i++)
41+
add(lower_bound(a.begin(), a.end(), nums[i])-a.begin(), n, 1);
42+
for (int i = k; i <= nums.size(); i++) {
43+
r.push_back(0.5*(double(a[select((k-1)/2, n)])+a[select(k/2, n)]));
44+
add(lower_bound(a.begin(), a.end(), nums[i-k])-a.begin(), n, -1);
45+
if (i < nums.size())
46+
add(lower_bound(a.begin(), a.end(), nums[i])-a.begin(), n, 1);
47+
}
48+
return r;
49+
}
50+
};

smallest-good-base.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Smallest Good Base
2+
class Solution {
3+
public:
4+
string smallestGoodBase(string n2) {
5+
unsigned long n = stol(n2);
6+
for (int m = 63-__builtin_clz(n); m > 1; m--) {
7+
unsigned long x = pow(n, 1.0/m), s = 1, c = 1;
8+
if (x > 1) {
9+
for (int i = m; i--; )
10+
s += c *= x;
11+
if (s == n)
12+
return to_string(x);
13+
}
14+
}
15+
return to_string(n-1);
16+
}
17+
};

0 commit comments

Comments
 (0)