Skip to content

Commit e25adad

Browse files
committed
426/429
1 parent 5b37a6a commit e25adad

9 files changed

+165
-1
lines changed

132-pattern.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 132 Pattern
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+
bool find132pattern(vector<int>& nums) {
8+
int n = nums.size(), j;
9+
vector<int> l(n), mn(n);
10+
REP(i, n) {
11+
for (j = i-1; j >= 0 && nums[j] <= nums[i]; j = l[j]);
12+
l[i] = j;
13+
mn[i] = min(nums[i], i ? mn[i-1] : INT_MAX);
14+
}
15+
REP(i, n)
16+
if (l[i] > 0 && mn[l[i]-1] < nums[i])
17+
return true;
18+
return false;
19+
}
20+
};

4sum-ii.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 4Sum II
2+
class Solution {
3+
public:
4+
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
5+
unordered_map<int, int> m;
6+
int r = 0;
7+
for (int a: A)
8+
for (int b: B)
9+
m[a+b]++;
10+
for (int c: C)
11+
for (int d: D) {
12+
int t = -c-d;
13+
if (m.count(t))
14+
r += m[t];
15+
}
16+
return r;
17+
}
18+
};

README.md

+9-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 418/421 problems.
5+
Solved 426/429 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|465|[Optimal Account Balancing](https://leetcode.com/problems/optimal-account-balancing)|[optimal-account-balancing.cc](optimal-account-balancing.cc)|
16+
|464|[Can I Win](https://leetcode.com/problems/can-i-win)|[can-i-win.cc](can-i-win.cc)|
17+
|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter)|[island-perimeter.cc](island-perimeter.cc)|
18+
|462|[Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii)|[minimum-moves-to-equal-array-elements-ii.cc](minimum-moves-to-equal-array-elements-ii.cc)|
19+
|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern)|[repeated-substring-pattern.cc](repeated-substring-pattern.cc)|
20+
|456|[132 Pattern](https://leetcode.com/problems/132-pattern)|[132-pattern.cc](132-pattern.cc)|
21+
|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies)|[assign-cookies.cc](assign-cookies.cc)|
22+
|454|[4Sum II](https://leetcode.com/problems/4sum-ii)|[4sum-ii.cc](4sum-ii.cc)|
1523
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements)|[minimum-moves-to-equal-array-elements.cc](minimum-moves-to-equal-array-elements.cc)|
1624
|452|[Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons)|[minimum-number-of-arrows-to-burst-balloons.cc](minimum-number-of-arrows-to-burst-balloons.cc)|
1725
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs)|[number-of-boomerangs.cc](number-of-boomerangs.cc)|

assign-cookies.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Assign Cookies
2+
class Solution {
3+
public:
4+
int findContentChildren(vector<int>& g, vector<int>& s) {
5+
sort(g.begin(), g.end());
6+
sort(s.begin(), s.end());
7+
auto i = s.begin();
8+
int r = 0;
9+
for (int x: g) {
10+
while (i != s.end() && *i < x) ++i;
11+
if (i == s.end()) break;
12+
++i;
13+
r++;
14+
}
15+
return r;
16+
}
17+
};

can-i-win.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Can I Win
2+
class Solution {
3+
unordered_map<int, bool> memo;
4+
int k;
5+
bool f(int s, int m) {
6+
int t = m*301+s;
7+
if (memo.count(t))
8+
return memo[t];
9+
bool& r = memo[t];
10+
for (int i = 0; i < k; i++)
11+
if (! (m & 1 << i) && (s-i-1 <= 0 || ! f(s-i-1, m | 1<<i))) {
12+
r = true;
13+
break;
14+
}
15+
return r;
16+
}
17+
public:
18+
bool canIWin(int maxChoosableInteger, int desiredTotal) {
19+
k = maxChoosableInteger;
20+
return k*(k+1)/2>= desiredTotal && f(desiredTotal, 0);
21+
}
22+
};

island-perimeter.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Island Perimeter
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 islandPerimeter(vector<vector<int>>& a) {
8+
int m = a.size(), n = a[0].size(), s = 0;
9+
REP(i, m)
10+
REP(j, n)
11+
if (a[i][j]) {
12+
if (! i || ! a[i-1][j]) s++;
13+
if (i == m-1 || ! a[i+1][j]) s++;
14+
if (! j || ! a[i][j-1]) s++;
15+
if (j == n-1 || ! a[i][j+1]) s++;
16+
}
17+
return s;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Minimum Moves to Equal Array Elements II
2+
class Solution {
3+
public:
4+
int minMoves2(vector<int>& a) {
5+
if (a.empty()) return 0;
6+
nth_element(a.begin(), a.begin()+a.size()/2, a.end());
7+
int s = 0;
8+
for (int i: a)
9+
s += abs(i-a[a.size()/2]);
10+
return s;
11+
}
12+
};

optimal-account-balancing.cc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Optimal Account Balancing
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 minTransfers(vector<vector<int>>& t) {
8+
unordered_map<int, int> m;
9+
int r = 0, c = 0;
10+
for (auto& x: t) {
11+
m[x[0]] += x[2];
12+
m[x[1]] -= x[2];
13+
}
14+
vector<int> a;
15+
for (auto& x: m)
16+
if (x.second)
17+
a.push_back(x.second);
18+
if (a.empty()) return 0;
19+
vector<int> dp(1<<a.size(), INT_MAX/2), cir;
20+
FOR(i, 1, 1<<a.size()) {
21+
int t = 0, c = 0;
22+
REP(j, a.size())
23+
if (i>>j & 1)
24+
t += a[j], c++;
25+
if (! t) {
26+
dp[i] = c-1;
27+
for (int t: cir)
28+
if ((i & t) == t)
29+
dp[i] = min(dp[i], dp[t]+dp[i-t]);
30+
cir.push_back(i);
31+
}
32+
}
33+
return dp.back();
34+
}
35+
};

repeated-substring-pattern.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Repeated Substring Pattern
2+
class Solution {
3+
public:
4+
bool repeatedSubstringPattern(string str) {
5+
vector<int> pi(str.size());
6+
for (int j = 0, i = 1; i < str.size(); i++) {
7+
while (j && str[i] != str[j]) j = pi[j-1];
8+
if (str[i] == str[j]) j++;
9+
pi[i] = j;
10+
}
11+
return pi.back() && str.size()%(str.size()-pi.back()) == 0;
12+
}
13+
};

0 commit comments

Comments
 (0)