# LeetCode周赛306，用原题，你对得起我们吗，日内瓦，退钱！

## 矩阵中的局部最大值

• maxLocal[i][j] 等于 grid 中以 i + 1 行和 j + 1 列为中心的 3 x 3 矩阵中的 最大值

### 题解

class Solution {
public:
vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> ret(n-2, vector<int>(n-2, 0));

for (int i = 1; i < n-1; i++) {
for (int j = 1; j < n-1; j++) {
int cur = grid[i][j];
for (int _i = i-1; _i <= i+1; _i++) {
for (int _j = j-1; _j <= j+1; _j++) {
cur = max(cur, grid[_i][_j]);
}
}
ret[i-1][j-1] = cur;
}
}

return ret;
}
};

## 边积分最高的节点

### 题解

class Solution {
public:
int edgeScore(vector<int>& edges) {
int n = edges.size();
vector<long long> sc(n, 0);
long long maxi = 0, ret = 0;
for (int i = 0; i < n; i++) {
int v = edges[i];
sc[v] += i;
if (sc[v] > maxi || sc[v] == maxi && v < ret) {
maxi = sc[v];
ret = v;
}
}

return ret;
}
};

## 根据模式串构造最小数字

• num 包含数字 '1''9' ，其中每个数字 至多 使用一次。
• 如果 pattern[i] == 'I' ，那么 num[i] < num[i + 1]
• 如果 pattern[i] == 'D' ，那么 num[i] > num[i + 1]

### 题解

class Solution {
public:
string smallestNumber(string pat) {
int n = pat.length();

string base = "";
for (int i = 0; i <= n; i++) base.push_back('0' + i+1);

do {
bool match = true;
for (int i = 0; i < n; i++) {
// 遍历所有位置，如果存在某个位置的大小关系不成立，退出循环
if (pat[i] == 'I' && base[i] > base[i+1] || pat[i] == 'D' && base[i] < base[i+1]) {
match = false;
break;
}
if (!match) break;
}
if (match) return base;
}while (next_permutation(base.begin(), base.end()));

return "";
}
};

## 统计特殊整数

### 题解

class Solution {
public:
int countSpecialNumbers(int n) {
int dp[10][2][1024];
memset(dp, 0xff, sizeof dp);

int nums[10]{0};
for (int i = 0, x = n; i < 10; i++, x/=10) nums[i] = x % 10;

function<int(int, bool, int)> dfs = [&](int p, bool flag, int status) -> int {
if (p < 0) return status != 0;
int &cur = dp[p][flag][status];
if (cur >= 0) return cur;
cur = 0;
for (int i = 0; i <= (flag ? nums[p]: 9); i++) {
if (i == 0 && status == 0) cur += dfs(p-1, flag && (i == nums[p]), 0);
else if (!(status & (1 << i))) cur += dfs(p-1, flag && (i == nums[p]), status | (1 << i));
}
return cur;
};

return dfs(9, 1, 0);
}
};