Skip to content

Commit 207fdbe

Browse files
committed
368/368 喵呜呜呜呜,求NYC住处。何年是归日,雨泪下孤舟。
1 parent 08dbf43 commit 207fdbe

12 files changed

+296
-1
lines changed

README.md

+12-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 357/357 problems.
5+
Solved 368/368 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/)|[mini-parser.cc](mini-parser.cc)|
16+
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|[shuffle-an-array.cc](shuffle-an-array.cc)|
17+
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/)|[ransom-note.cc](ransom-note.cc)|
18+
|382|[Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/)|[linked-list-random-node.cc](linked-list-random-node.cc)|
19+
|381|[Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/)|[insert-delete-getrandom-o1-duplicates-allowed.cc](insert-delete-getrandom-o1-duplicates-allowed.cc)|
20+
|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/)|[insert-delete-getrandom-o1.cc](insert-delete-getrandom-o1.cc)|
21+
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[design-phone-directory.cc](design-phone-directory.cc)|
22+
|378|[Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)|[kth-smallest-element-in-a-sorted-matrix.cc](kth-smallest-element-in-a-sorted-matrix.cc)|
23+
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)|[combination-sum-iv.cc](combination-sum-iv.cc)|
24+
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)|[wiggle-subsequence.cc](wiggle-subsequence.cc)|
25+
|375|[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)|[guess-number-higher-or-lower-ii.cc](guess-number-higher-or-lower-ii.cc)|
1526
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[guess-number-higher-or-lower.cc](guess-number-higher-or-lower.cc)|
1627
|373|[Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/)|[find-k-pairs-with-smallest-sums.cc](find-k-pairs-with-smallest-sums.cc)|
1728
|372|[Super Pow](https://leetcode.com/problems/super-pow/)|[super-pow.cc](super-pow.cc)|

combination-sum-iv.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Combination Sum IV
2+
class Solution {
3+
public:
4+
int combinationSum4(vector<int>& nums, int target) {
5+
vector<int> a(target+1, 0);
6+
a[0] = 1;
7+
for (int i = 0; i < target; i++)
8+
for (int x: nums)
9+
if (i+x <= target)
10+
a[i+x] += a[i];
11+
return a[target];
12+
}
13+
};

design-phone-directory.cc

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Design Phone Directory
2+
class PhoneDirectory {
3+
vector<bool> used;
4+
vector<int> a;
5+
int top = 0;
6+
public:
7+
/** Initialize your data structure here
8+
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
9+
PhoneDirectory(int maxNumbers) : a(maxNumbers), used(maxNumbers, false) {
10+
iota(a.begin(), a.end(), 0);
11+
}
12+
13+
/** Provide a number which is not assigned to anyone.
14+
@return - Return an available number. Return -1 if none is available. */
15+
int get() {
16+
if (top < a.size()) {
17+
used[a[top]] = true;
18+
return a[top++];
19+
}
20+
return -1;
21+
}
22+
23+
/** Check if a number is available or not. */
24+
bool check(int number) {
25+
return 0 <= number && number < a.size() && ! used[number];
26+
}
27+
28+
/** Recycle or release a number. */
29+
void release(int number) {
30+
if (0 <= number && number < a.size() && used[number]) {
31+
used[a[--top] = number] = false;
32+
}
33+
}
34+
};
35+
36+
/**
37+
* Your PhoneDirectory object will be instantiated and called as such:
38+
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
39+
* int param_1 = obj.get();
40+
* bool param_2 = obj.check(number);
41+
* obj.release(number);
42+
*/

guess-number-higher-or-lower-ii.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Guess Number Higher or Lower II
2+
// optimization of 2D dynamic programming: http://artofproblemsolving.com/community/c296841h1273742
3+
class Solution {
4+
public:
5+
int getMoneyAmount(int n) {
6+
vector<vector<int>> a(n+1, vector<int>(n+1, 0));
7+
for (int j = 2; j <= n; j++) {
8+
deque<pair<int, int>> q;
9+
for (int k = j-1, i = j; --i; ) {
10+
while (a[i][k-1] > a[k+1][j]) k--;
11+
while (q.size() && k < q.front().second) q.pop_front();
12+
long t = a[i+1][j]+i;
13+
while (q.size() && q.back().first >= t) q.pop_back();
14+
q.emplace_back(t, i);
15+
a[i][j] = min(q.front().first, a[i][k]+k+1);
16+
}
17+
}
18+
return a[1][n];
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Insert Delete GetRandom O(1) - Duplicates allowed
2+
class RandomizedCollection {
3+
public:
4+
unordered_multimap<int, int> m;
5+
vector<pair<int, decltype(m.begin())>> a;
6+
public:
7+
/** Initialize your data structure here. */
8+
RandomizedCollection() {
9+
10+
}
11+
12+
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
13+
bool insert(int val) {
14+
bool r = ! m.count(val);
15+
a.emplace_back(val, m.emplace(val, a.size()));
16+
return r;
17+
}
18+
19+
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
20+
bool remove(int val) {
21+
auto it = m.find(val);
22+
if (it == m.end()) return false;
23+
(a[it->second] = a.back()).second->second = it->second;
24+
m.erase(it);
25+
a.pop_back();
26+
return true;
27+
}
28+
29+
/** Get a random element from the collection. */
30+
int getRandom() {
31+
return a[rand()%a.size()].first;
32+
}
33+
};
34+
35+
/**
36+
* Your RandomizedCollection object will be instantiated and called as such:
37+
* RandomizedCollection obj = new RandomizedCollection();
38+
* bool param_1 = obj.insert(val);
39+
* bool param_2 = obj.remove(val);
40+
* int param_3 = obj.getRandom();
41+
*/

insert-delete-getrandom-o1.cc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Insert Delete GetRandom O(1)
2+
class RandomizedSet {
3+
unordered_map<int, int> m;
4+
vector<int> a;
5+
public:
6+
/** Initialize your data structure here. */
7+
RandomizedSet() {
8+
}
9+
10+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
11+
bool insert(int val) {
12+
if (! m.emplace(val, a.size()).second) return false;
13+
a.push_back(val);
14+
return true;
15+
}
16+
17+
/** Removes a value from the set. Returns true if the set contained the specified element. */
18+
bool remove(int val) {
19+
auto it = m.find(val);
20+
if (it == m.end()) return false;
21+
m[a[it->second] = a.back()] = it->second;
22+
m.erase(it);
23+
a.pop_back();
24+
return true;
25+
}
26+
27+
/** Get a random element from the set. */
28+
int getRandom() {
29+
return a[rand()%a.size()];
30+
}
31+
};
32+
33+
/**
34+
* Your RandomizedSet object will be instantiated and called as such:
35+
* RandomizedSet obj = new RandomizedSet();
36+
* bool param_1 = obj.insert(val);
37+
* bool param_2 = obj.remove(val);
38+
* int param_3 = obj.getRandom();
39+
*/
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Kth Smallest Element in a Sorted Matrix
2+
class Solution {
3+
public:
4+
int kthSmallest(vector<vector<int>>& matrix, int k) {
5+
int l = matrix[0][0], h = matrix.back().back();
6+
while (l < h) {
7+
int x = l+(h-l)/2, c = 0, j = matrix[0].size();
8+
for (auto& a: matrix) {
9+
while (j && x < a[j-1])
10+
j--;
11+
c += j;
12+
}
13+
if (c < k)
14+
l = x+1;
15+
else
16+
h = x;
17+
}
18+
return l;
19+
}
20+
};

linked-list-random-node.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Linked List Random Node
2+
class Solution {
3+
int size;
4+
ListNode* head;
5+
public:
6+
/** @param head The linked list's head.
7+
Note that the head is guaranteed to be not null, so it contains at least one node. */
8+
Solution(ListNode* head) {
9+
this->head = head;
10+
size = 0;
11+
for (; head; head = head->next)
12+
size++;
13+
}
14+
15+
/** Returns a random node's value. */
16+
int getRandom() {
17+
ListNode* x = head;
18+
int t = rand() % size;
19+
while (t--)
20+
x = x->next;
21+
return x->val;
22+
}
23+
};
24+
25+
/**
26+
* Your Solution object will be instantiated and called as such:
27+
* Solution obj = new Solution(head);
28+
* int param_1 = obj.getRandom();
29+
*/

mini-parser.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Mini Parser
2+
class Solution {
3+
public:
4+
NestedInteger deserialize(string s) {
5+
if (s[0] != '[')
6+
return NestedInteger(stoi(s));
7+
NestedInteger r;
8+
stack<NestedInteger> st;
9+
size_t l;
10+
for (size_t i = 0; i < s.size(); i++)
11+
if (s[i] == '[') {
12+
st.emplace();
13+
l = i+1;
14+
} else if (s[i] == ',' || s[i] == ']') {
15+
if (l < i)
16+
st.top().add(NestedInteger(stoi(s.substr(l, i-l))));
17+
l = i+1;
18+
if (s[i] == ']' && st.size() > 1) {
19+
auto x = st.top();
20+
st.pop();
21+
st.top().add(x);
22+
}
23+
}
24+
return st.top();
25+
}
26+
};

ransom-note.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Ransom Note
2+
class Solution {
3+
public:
4+
bool canConstruct(string ransomNote, string magazine) {
5+
long c[256] = {};
6+
for (unsigned char i: magazine)
7+
c[i]++;
8+
for (unsigned char i: ransomNote)
9+
c[i]--;
10+
for (long i: c)
11+
if (i < 0)
12+
return false;
13+
return true;
14+
}
15+
};

shuffle-an-array.cc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Shuffle an Array
2+
class Solution {
3+
vector<int> a;
4+
public:
5+
Solution(vector<int> nums) : a(nums) {}
6+
7+
/** Resets the array to its original configuration and return it. */
8+
vector<int> reset() { return a; }
9+
10+
/** Returns a random shuffling of the array. */
11+
vector<int> shuffle() {
12+
auto r = a;
13+
for (size_t i = 1; i < r.size(); i++)
14+
swap(r[rand()%(i+1)], r[i]);
15+
return r;
16+
}
17+
};
18+
19+
/**
20+
* Your Solution object will be instantiated and called as such:
21+
* Solution obj = new Solution(nums);
22+
* vector<int> param_1 = obj.reset();
23+
* vector<int> param_2 = obj.shuffle();
24+
*/

wiggle-subsequence.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Wiggle Subsequence
2+
class Solution {
3+
public:
4+
int wiggleMaxLength(vector<int>& a) {
5+
if (a.empty()) return 0;
6+
int r = 1, x = a[0], s = 0;
7+
for (int i = 0; i+1 < a.size(); i++) {
8+
if (a[i] < a[i+1] && s <= 0)
9+
r++, s = 1;
10+
if (a[i] > a[i+1] && s >= 0)
11+
r++, s = -1;
12+
}
13+
return r;
14+
}
15+
};

0 commit comments

Comments
 (0)