当前位置:网站首页>LC weekly 300
LC weekly 300
2022-07-04 05:27:00 【anieoo】
class Solution {
public:
string decodeMessage(string key, string message) {
unordered_map<char,char> map;
map.clear();
char index = 'a';
for(auto &x : key) {
if(x == ' ') continue;
if(map.count(x)) continue;
map[x] = index;
index++;
}
string res;
for(auto &x : message) {
if(x == ' ') res += x;
else {
char c = map[x];
res += c;
}
}
return res;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int dx[4] = {0, 1, 0, -1},dy[4] = {1, 0, -1, 0};
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> grid(m, vector<int>(n, -1));
vector<vector<bool>> sta(m, vector<bool>(n, false));
for(int i = 0,x = 0,y = 0,d = 0;i < m * n;i++) {
if(head == nullptr) break;
grid[x][y] = head->val;
sta[x][y] = true;
head = head->next;
int a = x + dx[d],b = y + dy[d];
if(a < 0 || a == m || b < 0 || b == n || sta[a][b]){
d = (d + 1) % 4;
a = x + dx[d],b = y + dy[d];
}
x = a,y = b;
}
return grid;
}
};
solution:
First initialize the matrix to -1, Then do it according to the method of spiral matrix .
6109. Number of people who know the secret
solution:
State means :dp[i] It means the first one i God knows the number of Secrets
State calculation :dp[i] = dp[i - forget] ~ dp[i - delay] The number of people adds up
Return value answer :dp[n] Push forward forget The number of people who know the secret accumulates
const int mod = 1e9 + 7;
class Solution {
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
//dp[i] It means the first one i God knows the number of Secrets
// The answer is No n Enumerate back forget The number of people in days
vector<long> dp(n + 1);
long ans = 0,num = 0; //ans Save return value ,num Save the first i God knows the number of Secrets
// initialization
dp[1] = 1;
for(int i = 2;i <= n;i++) {
int in_f = i - forget;
int in_d = i - delay; //dp[i] Only by dp[i - forget] ~ dp[i - delay] decision
num = 0;
while(in_d >= 0 && in_d > in_f) { //in_d > in_f Because I can't share that day
num += dp[in_d] % mod;
in_d--;
}
dp[i] = num;
}
int t = forget;
while (n >= 0 && t >= 1) {
ans = (ans + dp[n]) % mod;
n--;
t--;
}
return (int) ans;
}
};
6110. The number of incremental paths in the grid graph
solution:
Used in the competition BFS, Only pass 34/36, Time limit exceeded .
const int N = 1e9 + 7;
class Solution {
public:
int dx[4] = {1, 0, -1, 0},dy[4] = {0, 1, 0, -1};
typedef pair<int,int> PII;
vector<vector<int>> g;
vector<vector<int>> sta;
int ans = 0;
int countPaths(vector<vector<int>>& grid) {
int m = grid.size(),n = grid[0].size();
g = grid;
sta = vector<vector<int>> (m, vector<int> (n, -1));
for(int i = 0;i < m;i++)
for(int j = 0;j < n;j++) {
bfs(i, j);
}
return (ans + m * n) % N;
}
void bfs(int i,int j) {
if(sta[i][j] != -1) ans += sta[i][j];
int res = 0;
queue<PII> que;
que.push({i,j});
while(!que.empty()) {
auto t = que.front();
que.pop();
for(int i = 0;i < 4;i++) {
int a = t.first + dx[i],b = t.second + dy[i];
if(a < 0 || a == g.size() || b < 0 || b == g[0].size() || g[a][b] <= g[t.first][t.second]) continue;
if(sta[a][b] != -1) {
res += sta[a][b];
continue;
}
que.push({a,b});
res++;
}
}
sta[i][j] = res + 1;
ans += res % N;
}
};
dfs+ Memory search + Pruning
const int mod = 1e9 + 7;
class Solution {
public:
int f[1010][1010];
int dx[4] = {1, 0, -1, 0},dy[4] = {0, 1, 0, -1};
vector<vector<int>> g;
int countPaths(vector<vector<int>>& grid) {
memset(f, -1, sizeof(f)); // Initialization status
g = grid;
int m = grid.size(),n = grid[0].size();
long ans = 0;
for(int i = 0;i < m;i++)
for(int j = 0;j < n;j++) {
ans = (ans + dfs(i, j)) % mod;
}
return (int)ans % mod;
}
int dfs(int x,int y) {
if(f[x][y] != -1) return f[x][y]; // prune
int res = 1;
for(int i = 0;i < 4;i++) {
int a = x + dx[i],b = y + dy[i];
if(a < 0 || a == g.size() || b < 0 || b == g[0].size() || g[a][b] <= g[x][y]) continue;
res += dfs(a, b) % mod;
}
f[x][y] = res;
return f[x][y] % mod;
}
};
边栏推荐
- RSA加密应用常见缺陷的原理与实践
- JS string splicing enhancement
- VB.net 简单的处理图片,黑白(类库——7)
- 光模块字母含义及参数简称大全
- [matlab] matlab simulation modulation system - VSB system
- 2022 question bank and answers for safety management personnel of hazardous chemical business units
- [QT] timer
- 简易零钱通
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- Void convolution, deformable convolution, deformable ROI pooling
猜你喜欢
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
2022 R2 mobile pressure vessel filling retraining question bank and answers
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
模拟小根堆
谷歌 Chrome 浏览器将支持选取文字翻译功能
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
Trie数-字典树
Automated testing selenium foundation -- webdriverapi
Ping port artifact psping
随机推荐
2022危险化学品经营单位安全管理人员上岗证题库及答案
LabVIEW错误对话框的出现
Li Kou's 300th weekly match
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
[matlab] matlab simulation modulation system SSB system
Void convolution, deformable convolution, deformable ROI pooling
Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
Yyds dry goods inventory TCP & UDP
Principle and practice of common defects in RSA encryption application
空洞卷积、可变形卷积、可变形ROI Pooling
VB.net GIF(制作、拆解——优化代码,类库——5)
Automated testing selenium foundation -- webdriverapi
Evolution of system architecture: differences and connections between SOA and microservice architecture
基于单片机的太阳能杀虫系统
Roles of rollup components
[matlab] general function of communication signal modulation inverse Fourier transform
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
Flink1.13 basic SQL syntax (II) join operation
Get the ID of the record just inserted from laravel