Skip to content

Commit 0d02159

Browse files
committed
345/345
1 parent 1751822 commit 0d02159

10 files changed

+237
-1
lines changed

README.md

+10-1
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 336/336 problems.
5+
Solved 345/345 problems.
66

77
## Database
88

@@ -12,6 +12,15 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter/)|[design-hit-counter.cc](design-hit-counter.cc)|
16+
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[bomb-enemy.cc](bomb-enemy.cc)|
17+
|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/)|[sort-transformed-array.cc](sort-transformed-array.cc)|
18+
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[logger-rate-limiter.cc](logger-rate-limiter.cc)|
19+
|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)|[rearrange-string-k-distance-apart.cc](rearrange-string-k-distance-apart.cc)|
20+
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[count-numbers-with-unique-digits.cc](count-numbers-with-unique-digits.cc)|
21+
|356|[Line Reflection](https://leetcode.com/problems/line-reflection/)|[line-reflection.cc](line-reflection.cc)|
22+
|355|[Design Twitter](https://leetcode.com/problems/design-twitter/)|[design-twitter.cc](design-twitter.cc)|
23+
|354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/)|[russian-doll-envelopes.cc](russian-doll-envelopes.cc)|
1524
|353|[Design Snake Game](https://leetcode.com/problems/design-snake-game/)|[design-snake-game.cc](design-snake-game.cc)|
1625
|352|[Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/)|[data-stream-as-disjoint-intervals.cc](data-stream-as-disjoint-intervals.cc)|
1726
|351|[Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/)|[android-unlock-patterns.cc](android-unlock-patterns.cc)|

bomb-enemy.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Bomb Enemy
2+
#define REP(i, n) FOR(i, 0, n)
3+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
4+
5+
class Solution {
6+
public:
7+
int maxKilledEnemies(vector<vector<char>>& grid) {
8+
int m = grid.size(), n = m ? grid[0].size() : 0, r = 0, row, col[n];
9+
REP(i, m)
10+
REP(j, n) {
11+
if (! j || grid[i][j-1] == 'W') {
12+
row = 0;
13+
for (int k = j; k < n && grid[i][k] != 'W'; k++)
14+
row += grid[i][k] == 'E';
15+
}
16+
if (! i || grid[i-1][j] == 'W') {
17+
col[j] = 0;
18+
for (int k = i; k < m && grid[k][j] != 'W'; k++)
19+
col[j] += grid[k][j] == 'E';
20+
}
21+
if (grid[i][j] == '0')
22+
r = max(r, row+col[j]);
23+
}
24+
return r;
25+
}
26+
};

count-numbers-with-unique-digits.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Count Numbers with Unique Digits
2+
#define REP(i, n) FOR(i, 0, n)
3+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
4+
5+
class Solution {
6+
public:
7+
int countNumbersWithUniqueDigits(int n) {
8+
int r = 1, p = 9;
9+
REP(i, min(n, 10)) {
10+
r += p;
11+
p *= 9-i;
12+
}
13+
return r;
14+
}
15+
};

design-hit-counter.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Design Hit Counter
2+
class HitCounter {
3+
queue<int> q;
4+
public:
5+
/** Initialize your data structure here. */
6+
HitCounter() {
7+
8+
}
9+
10+
/** Record a hit.
11+
@param timestamp - The current timestamp (in seconds granularity). */
12+
void hit(int timestamp) {
13+
q.push(timestamp);
14+
}
15+
16+
/** Return the number of hits in the past 5 minutes.
17+
@param timestamp - The current timestamp (in seconds granularity). */
18+
int getHits(int timestamp) {
19+
while (q.size() && timestamp - q.front() >= 300)
20+
q.pop();
21+
return q.size();
22+
}
23+
};

design-twitter.cc

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Design Twitter
2+
class Twitter {
3+
unordered_map<int, unordered_set<int>> follows;
4+
unordered_map<int, vector<pair<int, int>>> tweets;
5+
int tick = 0;
6+
public:
7+
/** Initialize your data structure here. */
8+
Twitter() {
9+
10+
}
11+
12+
/** Compose a new tweet. */
13+
void postTweet(int userId, int tweetId) {
14+
tweets[userId].emplace_back(tick++, tweetId);
15+
}
16+
17+
/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
18+
vector<int> getNewsFeed(int userId) {
19+
typedef vector<pair<int, int>>::reverse_iterator it;
20+
auto cmp = [](const pair<it, it>& x, const pair<it, it>& y) {
21+
return x.first->first < y.first->first;
22+
};
23+
vector<pair<it, it>> h;
24+
for (int x: follows[userId]) {
25+
auto& ts = tweets[x];
26+
if (ts.size())
27+
h.emplace_back(ts.rbegin(), ts.rend());
28+
}
29+
auto& ts = tweets[userId];
30+
if (ts.size())
31+
h.emplace_back(ts.rbegin(), ts.rend());
32+
make_heap(h.begin(), h.end(), cmp);
33+
vector<int> r;
34+
while (r.size() < 10 && h.size()) {
35+
pop_heap(h.begin(), h.end(), cmp);
36+
auto& x = h.back();
37+
r.push_back(x.first->second);
38+
if (++x.first == x.second)
39+
h.pop_back();
40+
else
41+
push_heap(h.begin(), h.end(), cmp);
42+
}
43+
return r;
44+
}
45+
46+
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
47+
void follow(int followerId, int followeeId) {
48+
if (followerId != followeeId)
49+
follows[followerId].insert(followeeId);
50+
}
51+
52+
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
53+
void unfollow(int followerId, int followeeId) {
54+
follows[followerId].erase(followeeId);
55+
}
56+
};

line-reflection.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Line Reflection
2+
class Solution {
3+
public:
4+
bool isReflected(vector<pair<int, int>>& points) {
5+
sort(points.begin(), points.end(), [](const pair<int, int>& x, const pair<int, int>& y) {
6+
return x.second != y.second ? x.second < y.second : x.first < y.first;
7+
});
8+
long r = LONG_MIN;
9+
for (auto i = points.begin(); i != points.end(); ) {
10+
auto j = i;
11+
while (++i != points.end() && j->second == i->second);
12+
for (auto jj = j, ii = i; points.begin() < ii && jj <= --ii; ++jj) {
13+
if (r != LONG_MIN && r != jj->first + ii->first)
14+
return false;
15+
r = jj->first + ii->first;
16+
}
17+
}
18+
return true;
19+
}
20+
};

logger-rate-limiter.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Logger Rate Limiter
2+
class Logger {
3+
unordered_map<string, int> last;
4+
public:
5+
/** Initialize your data structure here. */
6+
Logger() {
7+
8+
}
9+
10+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
11+
If this method returns false, the message will not be printed.
12+
The timestamp is in seconds granularity. */
13+
bool shouldPrintMessage(int timestamp, string message) {
14+
if (! last.count(message) || last[message] <= timestamp-10) {
15+
last[message] = timestamp;
16+
return true;
17+
}
18+
return false;
19+
}
20+
};

rearrange-string-k-distance-apart.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Rearrange String k Distance Apart
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
string rearrangeString(string str, int k) {
8+
int c[26] = {};
9+
priority_queue<pair<int, char>> h;
10+
k = max(k, 1);
11+
vector<pair<int, int>> q(k);
12+
for (auto i: str)
13+
c[i-'a']++;
14+
REP(i, 26)
15+
if (c[i])
16+
h.emplace(c[i], 'a'+i);
17+
REP(i, str.size()) {
18+
if (i >= k && q[i%k].first)
19+
h.push(q[i%k]);
20+
if (h.empty())
21+
return "";
22+
str[i] = h.top().second;
23+
q[i%k] = {h.top().first-1, h.top().second};
24+
h.pop();
25+
}
26+
return str;
27+
}
28+
};

russian-doll-envelopes.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Russian Doll Envelopes
2+
class Solution {
3+
public:
4+
int maxEnvelopes(vector<pair<int, int>>& a) {
5+
sort(a.begin(), a.end(), [](const pair<int, int>& x, const pair<int, int>& y) {
6+
return x.first != y.first ? x.first < y.first : x.second > y.second;
7+
});
8+
vector<int> b;
9+
for (auto& x: a) {
10+
auto t = lower_bound(b.begin(), b.end(), x.second);
11+
if (b.end() == t)
12+
b.push_back(x.second);
13+
else
14+
*t = x.second;
15+
}
16+
return b.size();
17+
}
18+
};

sort-transformed-array.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Sort Transformed Array
2+
class Solution {
3+
public:
4+
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
5+
#define S(x, y) ((x) < (y) ? 1 : (x) > (y) ? 2 : 0)
6+
if (nums.empty())
7+
return nums;
8+
for (auto& x: nums)
9+
x = (x*a+b)*x+c;
10+
int i = 1, dir = S(nums[0], nums[1]);
11+
while (i < nums.size() && (dir |= S(nums[i-1], nums[i])) != 3)
12+
i++;
13+
if (i >= 2 && nums[0] > nums[i-1])
14+
reverse(nums.begin(), nums.begin()+i);
15+
if (i+2 <= nums.size() && nums[i] > nums.back())
16+
reverse(nums.begin()+i, nums.end());
17+
vector<int> r;
18+
merge(nums.begin(), nums.begin()+i, nums.begin()+i, nums.end(), back_inserter(r));
19+
return r;
20+
}
21+
};

0 commit comments

Comments
 (0)