Skip to content

Commit 4913750

Browse files
committed
update leetcode
1 parent f60b9f9 commit 4913750

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

leetcode/ImageSmoother.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
4+
typedef long long ll;
5+
typedef pair<int, int> pii;
6+
7+
int dir_x[] = {0, 0, 1, 1, 1, -1, -1, -1};
8+
int dir_y[] = {1, -1, -1, 0, 1, -1, 0, 1};
9+
class Solution {
10+
public:
11+
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
12+
vector<vector<int>> res = M;
13+
int row = M.size(), col = M[0].size();
14+
for (int i = 0; i < row; ++i) {
15+
for (int j = 0; j < col; ++j) {
16+
int sum = M[i][j];
17+
int cnt = 1;
18+
for (int d = 0; d < 8; ++d) {
19+
int nx = i + dir_x[d];
20+
int ny = j + dir_y[d];
21+
if (nx >= 0 && nx < row && ny >= 0 && ny < col) {
22+
sum += M[nx][ny];
23+
++cnt;
24+
}
25+
}
26+
res[i][j] = sum / cnt;
27+
}
28+
}
29+
return res;
30+
}
31+
};
32+
33+
int main() {
34+
Solution solution;
35+
return 0;
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
4+
typedef long long ll;
5+
typedef pair<int, int> pii;
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* struct TreeNode {
10+
* int val;
11+
* TreeNode *left;
12+
* TreeNode *right;
13+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
14+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
16+
* };
17+
*/
18+
typedef pair<TreeNode*, ll> pti;
19+
class Solution {
20+
public:
21+
int widthOfBinaryTree(TreeNode* root) {
22+
if (NULL == root) return 0;
23+
queue<pti> q;
24+
q.push({root, 0LL});
25+
int res = 1;
26+
while (!q.empty()) {
27+
int sz = q.size();
28+
int start_idx = -1;
29+
ll mn = 1LL<<48, mx = -1;
30+
FOR(i, sz) {
31+
pti tp = q.front();
32+
q.pop();
33+
TreeNode* cur = tp.first;
34+
ll idx = tp.second;
35+
mx = max(idx, mx);
36+
mn = min(idx, mn);
37+
if (cur->left != NULL) {
38+
if (start_idx < 0) {
39+
start_idx = idx * 2;
40+
}
41+
q.push({cur->left, idx * 2 - start_idx});
42+
}
43+
if (cur ->right != NULL) {
44+
if (start_idx < 0) {
45+
start_idx = idx * 2 + 1;
46+
}
47+
q.push({cur->right, idx * 2 + 1 - start_idx});
48+
}
49+
}
50+
res = max(res, (int)(mx - mn + 1));
51+
}
52+
return res;
53+
}
54+
};
55+
56+
int main() {
57+
Solution solution;
58+
return 0;
59+
}

leetcode/NonDecreasingArray.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool checkPossibility(vector<int>& nums) {
4+
int sz = nums.size();
5+
int mod_cnt = 0;
6+
for (int i = 0; i < sz - 1; ++i) {
7+
if (nums[i] > nums[i + 1]) {
8+
if (mod_cnt >= 1) {
9+
return false;
10+
}
11+
if (i - 1 < 0 || nums[i + 1] >= nums[i - 1]) {
12+
++mod_cnt;
13+
nums[i] = nums[i + 1];
14+
} else if (i + 2 >= sz || nums[i + 2] >= nums[i]) {
15+
++mod_cnt;
16+
nums[i + 1] = nums[i];
17+
} else {
18+
return false;
19+
}
20+
}
21+
}
22+
return true;
23+
}
24+
};

0 commit comments

Comments
 (0)