Skip to content

Commit 472f536

Browse files
author
Luke
committed
.
1 parent 1803d84 commit 472f536

File tree

6 files changed

+220
-3
lines changed

6 files changed

+220
-3
lines changed

divisor-game.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode-cn.com/problems/divisor-game/
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
bool divisorGame(int N) { return !(N & 1); }
13+
};
14+
15+
inline void test() {
16+
Solution solution;
17+
cout << (solution.divisorGame(2) ? "True" : "False") << endl;
18+
}

dungeon-game.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,37 @@
22

33
#pragma once
44

5+
#include <algorithm>
56
#include <iostream>
67
#include <vector>
78

8-
#using namespace std;
9+
using namespace std;
910

1011
class Solution {
1112
public:
12-
int calculateMinimumHP(vector<vector<int>> &dungeon) {}
13+
int calculateMinimumHP(const vector<vector<int>> &dungeon) {
14+
const int M = dungeon.size(), N = dungeon.front().size();
15+
vector<vector<int>> dp(M, vector<int>(N, 0));
16+
dp[M - 1][N - 1] = max(1, 1 - dungeon[M - 1][N - 1]);
17+
for (int i = M - 2; i >= 0; --i) {
18+
dp[i][N - 1] = max(1, dp[i + 1][N - 1] - dungeon[i][N - 1]);
19+
}
20+
for (int j = N - 2; j >= 0; --j) {
21+
dp[M - 1][j] = max(1, dp[M - 1][j + 1] - dungeon[M - 1][j]);
22+
}
23+
for (int i = M - 2; i >= 0; --i) {
24+
for (int j = N - 2; j >= 0; --j) {
25+
dp[i][j] =
26+
max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
27+
}
28+
}
29+
return dp.front().front();
30+
}
1331
};
1432

15-
inline void test() {}
33+
inline void test() {
34+
Solution solution;
35+
cout << solution.calculateMinimumHP(
36+
{{-2, -3, 3}, {-5, -10, 1}, {10, 30, -5}})
37+
<< endl; // 7
38+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
#include <queue>
8+
#include <vector>
9+
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
int res = 0;
15+
int dir[5] = {0, 1, 0, -1, 0};
16+
int M, N;
17+
int dfs(vector<vector<int>> &dp, vector<vector<int>> &matrix, int x, int y,
18+
int len) {
19+
if (dp[x][y] != -1)
20+
return dp[x][y];
21+
// int tmp = len;
22+
for (int i = 0; i < 4; i++) {
23+
int tx = x + dir[i];
24+
int ty = y + dir[i + 1];
25+
if (tx >= 0 && tx < M && ty >= 0 && ty < N) {
26+
if (matrix[tx][ty] > matrix[x][y]) {
27+
dp[x][y] = max(dp[x][y], dfs(dp, matrix, tx, ty, len + 1));
28+
}
29+
}
30+
}
31+
return ++dp[x][y];
32+
}
33+
int longestIncreasingPath(vector<vector<int>> &matrix) {
34+
if (matrix.empty() || matrix.front().size())
35+
return 0;
36+
M = matrix.size(), N = matrix.front().size();
37+
vector<vector<int>> dp(M, vector<int>(N, -1));
38+
for (int i = 0; i < M; i++) {
39+
for (int j = 0; j < N; j++) {
40+
res = max(res, dfs(dp, matrix, i, j, 1));
41+
}
42+
}
43+
return ++res;
44+
}
45+
};
46+
47+
inline void test() {
48+
Solution solution;
49+
cout << solution.longestIncreasingPath({{9, 9, 4}, {6, 6, 8}, {2, 1, 1}})
50+
<< endl; // 4
51+
cout << solution.longestIncreasingPath({{3, 4, 5}, {3, 2, 6}, {2, 2, 1}})
52+
<< endl; // 4
53+
cout << solution.longestIncreasingPath(
54+
{{0, 0, 12, 6, 15, 1, 12, 10, 12, 10, 6},
55+
{6, 19, 6, 13, 5, 18, 17, 19, 7, 11, 13},
56+
{8, 6, 9, 1, 15, 7, 10, 10, 3, 7, 18},
57+
{2, 14, 12, 10, 17, 2, 3, 10, 4, 8, 3},
58+
{8, 2, 19, 3, 19, 10, 17, 18, 12, 10, 8},
59+
{0, 17, 14, 12, 10, 4, 8, 17, 15, 11, 19},
60+
{13, 6, 14, 8, 16, 19, 12, 17, 16, 17, 8},
61+
{7, 4, 6, 8, 3, 9, 19, 12, 4, 13, 0},
62+
{18, 0, 16, 12, 10, 11, 8, 14, 6, 3, 0},
63+
{10, 3, 14, 17, 19, 18, 10, 2, 11, 5, 19},
64+
{6, 2, 2, 1, 8, 1, 11, 7, 7, 18, 1},
65+
{11, 12, 16, 0, 9, 6, 8, 3, 12, 8, 15},
66+
{5, 18, 17, 4, 11, 9, 9, 6, 8, 2, 4},
67+
{3, 12, 7, 2, 9, 17, 14, 10, 14, 5, 0}})
68+
<< endl;
69+
}

split-array-largest-sum.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://leetcode-cn.com/problems/split-array-largest-sum/
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
#include <numeric>
8+
#include <vector>
9+
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
int splitArray(const vector<int> &nums, int m) {
15+
int64_t l = *max_element(nums.begin(), nums.end()),
16+
r = accumulate(nums.begin(), nums.end(), 0i64);
17+
while (l < r) {
18+
int64_t s = 0, c = 1;
19+
const int64_t mid = (l + r) >> 1;
20+
for (auto &&num : nums) {
21+
s += num;
22+
if (s > mid) {
23+
++c;
24+
s = num;
25+
}
26+
}
27+
if (c > m) {
28+
l = mid + 1;
29+
} else {
30+
r = mid;
31+
}
32+
}
33+
return l;
34+
}
35+
};
36+
37+
inline void test() {
38+
Solution solution;
39+
cout << solution.splitArray({7, 2, 5, 10, 8}, 2) << endl; // 18
40+
}

two-sum.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode-cn.com/problems/two-sum/
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
#include <unordered_map>
8+
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
vector<int> twoSum(const vector<int> &nums, int target) {
14+
unordered_map<int, int> mp;
15+
for (int i = 0; i < nums.size(); ++i) {
16+
if (auto iter = mp.find(nums[i]); iter != mp.end()) {
17+
return {iter->second, i};
18+
}
19+
mp[target - nums[i]] = i;
20+
}
21+
22+
return {};
23+
}
24+
};
25+
26+
inline ostream &operator<<(ostream &os, const vector<int> &nums) {
27+
for (auto &&num : nums) {
28+
os << num << " ";
29+
}
30+
return os;
31+
}
32+
33+
inline void test() {
34+
Solution solution;
35+
cout << solution.twoSum({2, 7, 11, 15}, 9) << endl;
36+
}

zui-xiao-tiao-yue-ci-shu.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
int minJump(const vector<int> &jump) {
14+
const auto M = jump.size();
15+
vector<int> dp(M, 0);
16+
for (int i = M - 1; i >= 0; --i) {
17+
dp[i] = i + jump[i] >= M ? 1 : (dp[i + jump[i]] + 1);
18+
for (int j = i + 1; j < M && j < i + jump[i] && dp[j] > dp[i];
19+
++j) {
20+
dp[j] = min(dp[j], dp[i] + 1);
21+
}
22+
}
23+
24+
return dp[0];
25+
}
26+
};
27+
28+
inline void test() {
29+
Solution solution;
30+
cout << solution.minJump({2, 5, 1, 1, 1, 1}) << endl;
31+
}

0 commit comments

Comments
 (0)