Skip to content

Commit a65df9e

Browse files
committedMar 19, 2023
🐞 weekly contest
1 parent f120393 commit a65df9e

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
 

Diff for: ‎6322.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool checkValidGrid(vector<vector<int>>& grid) {
10+
struct helper {
11+
int r = 0;
12+
int c = 0;
13+
int step = 0;
14+
bool operator<(const helper& b) { return this->step < b.step; }
15+
};
16+
17+
vector<helper> arr;
18+
int m = grid.size(), n = grid[0].size();
19+
20+
if (grid[0][0] != 0) return false;
21+
22+
for (auto i = 0; i < m; ++i)
23+
for (auto j = 0; j < n; ++j) {
24+
arr.push_back({i, j, grid[i][j]});
25+
}
26+
27+
sort(arr.begin(), arr.end());
28+
29+
vector<pair<int, int>> valid = {{1, 2}, {1, -2}, {-1, -2}, {-1, 2},
30+
{2, 1}, {2, -1}, {-2, -1}, {-2, 1}};
31+
32+
for (int i = 1; i < m * n; ++i) {
33+
bool found = false;
34+
for (auto v : valid) {
35+
if (v.first == (arr[i].r - arr[i - 1].r) &&
36+
v.second == (arr[i].c - arr[i - 1].c)) {
37+
found = true;
38+
break;
39+
}
40+
}
41+
42+
if (found == false) return false;
43+
}
44+
45+
return true;
46+
}
47+
};
48+
49+
int main(int argc, char const* argv[]) {
50+
Solution s;
51+
// vector<vector<int>> v = {{0, 11, 16, 5, 20},
52+
// {17, 4, 19, 10, 15},
53+
// {12, 1, 8, 21, 6},
54+
// {3, 18, 23, 14, 9},
55+
// {24, 13, 2, 7, 22}};
56+
57+
vector<vector<int>> v = {{24, 11, 22, 17, 4},
58+
{21, 16, 5, 12, 9},
59+
{6, 23, 10, 3, 18},
60+
{15, 20, 1, 8, 13},
61+
{0, 7, 14, 19, 2}};
62+
cout << s.checkValidGrid(v) << endl;
63+
return 0;
64+
}

Diff for: ‎6352.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#include <vector>
3+
#include <unordered_map>
4+
#include <set>
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
int beautifulSubsets(vector<int>& nums, int k) {
11+
int sub = nums[0], n = nums.size();
12+
set<int> indexes;
13+
14+
for (int i = 0; i < n - 1; ++i)
15+
for (int j = i + 1; j < n; ++j) {
16+
if (nums[i] - nums[j] == -k || nums[i] - nums[j] == k) {
17+
indexes.insert(i);
18+
indexes.insert(j);
19+
}
20+
}
21+
22+
int ret = ((1 << indexes.size()) - 1 - indexes.size());
23+
return ret;
24+
}
25+
};
26+
27+
int main(int argc, char const* argv[]) {
28+
Solution s;
29+
vector<int> v = {2, 4, 6};
30+
s.beautifulSubsets(v, 2);
31+
return 0;
32+
}

0 commit comments

Comments
 (0)