当前位置:网站首页>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;
}
};边栏推荐
- cmake
- 总线的基本概念
- flink1.13 sql基础语法(二)join操作
- Appearance of LabVIEW error dialog box
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
- Supplement the JS of a video website to decrypt the video
- RSA加密应用常见缺陷的原理与实践
- VB. Net calls ffmpeg to simply process video (class Library-6)
- 2022G2电站锅炉司炉特种作业证考试题库及答案
- flink1.13 sql基础语法(一)DDL、DML
猜你喜欢

Trie number dictionary tree

Void convolution, deformable convolution, deformable ROI pooling

2022年T电梯修理操作证考试题库及模拟考试

June 2022 summary

【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre

Analysis of classical pointer and array written test questions in C language

企业级日志分析系统ELK(如果事与愿违那一定另有安排)

What are the reasons for the frequent high CPU of ECS?

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

Evolution of system architecture: differences and connections between SOA and microservice architecture
随机推荐
Zhongke Panyun - data analysis and forensics packet flag
2022G2电站锅炉司炉特种作业证考试题库及答案
[matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
谷歌 Chrome 浏览器将支持选取文字翻译功能
[QT] timer
ETCD数据库源码分析——初始化总览
模拟小根堆
NTFS security permissions
云原生架构实战案例及优化解决方案
【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
如何使用postman实现简单的接口关联【增删改查】
Simulink and Arduino serial port communication
National vocational college skills competition (secondary vocational group) network security competition questions - Analysis
总线的基本概念
Flink1.13 basic SQL syntax (II) join operation
cmake
JS string splicing
Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
光模塊字母含義及參數簡稱大全
What is MQ?