Skip to content

Commit 3633cfd

Browse files
author
Liang Wang
committed
update leetcode
1 parent 598bfe9 commit 3633cfd

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

leetcode/BeautifulArrangement.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
private:
6+
int cnt;
7+
void solve(int step, vector<bool>& chosen) {
8+
if (step <= 1) {
9+
++this->cnt;
10+
return;
11+
}
12+
for (int i = 1; i < chosen.size(); ++i) {
13+
if (!chosen[i] && (i % step == 0 || step % i == 0)) {
14+
chosen[i] = true;
15+
solve(step - 1, chosen);
16+
chosen[i] = false;
17+
}
18+
}
19+
}
20+
public:
21+
int countArrangement(int N) {
22+
this->cnt = 0;
23+
vector<bool> chosen(N + 1, false);
24+
solve(N, chosen);
25+
return this->cnt;
26+
}
27+
};
28+
29+
int main() {
30+
Solution sol;
31+
for (int i = 2; i <= 15; ++i) {
32+
cout << sol.countArrangement(i) << endl;
33+
}
34+
return 0;
35+
}

leetcode/MineSweeper.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef pair<int, int> pii;
5+
const int dir_x[] = {0, 0, 1, 1, 1, -1, -1, -1};
6+
const int dir_y[] = {-1, 1, -1, 0, 1, -1, 0, 1};
7+
int get_cnt(const vector<vector<char>>& board, const int x, const int y) {
8+
int ret = 0;
9+
for (int i = 0; i < 8; ++i) {
10+
int nx = x + dir_x[i];
11+
int ny = y + dir_y[i];
12+
if (nx < 0 || nx >= (int)board.size() || ny < 0 || ny >= (int)board[0].size()) {
13+
continue;
14+
}
15+
ret += (board[nx][ny] == 'X' || board[nx][ny] == 'M');
16+
}
17+
return ret;
18+
}
19+
20+
class Solution {
21+
public:
22+
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
23+
int x = click[0];
24+
int y = click[1];
25+
if (board[x][y] == 'M') {
26+
board[x][y] = 'X';
27+
return board;
28+
}
29+
queue<pii> q;
30+
int cnt = get_cnt(board, x, y);
31+
board[x][y] = cnt > 0 ? '0' + cnt : 'B';
32+
if (cnt == 0) {
33+
q.push({x, y});
34+
}
35+
while (!q.empty()) {
36+
pii tp = q.front();
37+
q.pop();
38+
for (int i = 0; i < 8; ++i) {
39+
int nx = tp.first + dir_x[i];
40+
int ny = tp.second + dir_y[i];
41+
if (nx < 0 || nx >= (int)board.size() || ny < 0 || ny >= (int)board[0].size()) {
42+
continue;
43+
}
44+
if (board[nx][ny] == 'E') {
45+
int c = get_cnt(board, nx, ny);
46+
board[nx][ny] = c > 0 ? '0' + c : 'B';
47+
if (c == 0) {
48+
q.push({nx, ny});
49+
}
50+
}
51+
}
52+
}
53+
return board;
54+
}
55+
};
56+
57+
int main() {
58+
Solution sol;
59+
return 0;
60+
}

leetcode/RandomPickWithWeight.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
class Solution {
3+
private:
4+
vector<double> p;
5+
public:
6+
Solution(vector<int> w) {
7+
this->p.resize(w.size());
8+
int sm = accumulate(w.begin(), w.end(), 0);
9+
for (int i = 0; i < (int)w.size(); ++i) {
10+
if (i > 0) {
11+
this->p[i] = this->p[i - 1] + 1.0 * w[i] / sm;
12+
} else {
13+
this->p[i] = 1.0 * w[i] / sm;
14+
}
15+
}
16+
}
17+
18+
int pickIndex() {
19+
double rnd = 1.0 * rand() / RAND_MAX;
20+
int idx = lower_bound(this->p.begin(), this->p.end(), rnd) - this->p.begin();
21+
return idx;
22+
}
23+
};
24+
25+
/**
26+
* Your Solution object will be instantiated and called as such:
27+
* Solution obj = new Solution(w);
28+
* int param_1 = obj.pickIndex();
29+
*/

0 commit comments

Comments
 (0)