当前位置:网站首页>[the 300th weekly match of leetcode]
[the 300th weekly match of leetcode]
2022-07-06 18:20:00 【ღCauchyོꦿ࿐】
List of articles
Decrypt the message
subject
Ideas
- simulation , Just mark it .
Code
class Solution {
public:
string decodeMessage(string key, string message) {
string res = "";
int n = key.size();
bool st[26] = {
0};
map<char, int> mp;
int now = 0;
for (int i = 0; i < n; i++) {
if(key[i] == ' ') continue;
if(!st[key[i] - 'a']) {
st[key[i] - 'a'] = true;
mp[key[i]] = now;
now ++;
}
}
for (int i = 0; i < message.size(); i++) {
if(message[i] == ' ') res += ' ';
else {
res += mp[message[i]] + 'a';
}
}
return res;
}
};
Spiral matrix IV
subject
Ideas
- Set the current direction n o w now now,(0、1、2、3) Corresponding direction array w a l k walk walk.
- Set up, down, left and right (lx、rx、ly、ry) Four borders , Adjust when turning the direction .
Code
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> res(m, vector<int>(n, -1));
ListNode *p = head;
if(p == nullptr) return res;
vector<int> v;
while(p) {
v.push_back(p->val); p = p->next; }
int x = 0, y = -1;
int walk[4][2] = {
0, 1, 1, 0, 0, -1, -1, 0};
int now = 0;
int lx = 0, rx = m - 1, ly = 0, ry = n - 1;
for (int i = 0; i < v.size(); i++) {
int dx = x + walk[now][0], dy = y + walk[now][1];
if(dx > rx || dx < lx || dy > ry || dy < ly) {
if(now == 0) lx += 1;
if(now == 1) ry -= 1;
if(now == 2) rx -= 1;
if(now == 3) ly += 1;
now = (now + 1) % 4;
dx = x + walk[now][0], dy = y + walk[now][1];
}
res[dx][dy] = v[i];
x = dx, y = dy;
}
return res;
}
};
Number of people who know the secret
subject
Ideas
I thought at first , Just take a dynamic array , Simulated deletion 、 The process of adding people . But time , The space cost is bound to be great .
This needs to be optimized :
- It is found that for the i i i God : In the current collection Everyone who can share secrets ( i > = x + d e l a y i>=x+delay i>=x+delay), New people added , Their state ( Time is the beginning i i i God 、delay、forget) All are consistent .( This is equivalent to that we don't need to add everyone one by one , Instead, you can directly replace it with a variable equivalent )
- Find that time is monotonically increasing , Parameters d e l a y 、 f o r g e t delay、forget delay、forget Is constant , Then the latter must be later than the former .
Here I use m a p < i n t , i n t > map<int, int> map<int,int> For storage : The first i i i God , The number of j j j.
Code
class Solution {
public:
constexpr static int mod = 1e9 + 7;
int peopleAwareOfSecret(int n, int delay, int forget) {
map<int, int> mp;
int res = 0;
mp[1] = 1;
for (int i = 2; i <= n; i++) {
int cnt = 0;
for (auto &x: mp) {
int a = x.first, b = x.second;
if(i >= a + forget) {
mp.erase(a); continue; }
if (i >= a + delay) cnt = (cnt + b) % mod;
}
mp.insert({
i, cnt});
}
for (auto &x: mp) res = (res + x.second) % mod;
return res % mod;
}
};
The number of incremental paths in the grid graph
subject
Ideas
- Memory search
Consider the example illustration
3->4:1
1->3、1->3->4: 2
Adjacent can arrive , State shift d p [ i ] [ j ] = d p [ x ] [ y ] + 1 dp[i][j] = dp[x][y] + 1 dp[i][j]=dp[x][y]+1.
Code
class Solution {
public:
constexpr static int mod = 1e9 + 7;
int n, m;
int walk[4][2] = {
0, 1, 1, 0, 0, -1, -1, 0};
int dfs(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& dp) {
int s = 0;
for (int i = 0; i < 4; i++) {
int dx = x + walk[i][0];
int dy = y + walk[i][1];
if(dx >= n || dx < 0 || dy >= m || dy < 0) continue;
if(grid[x][y] >= grid[dx][dy]) continue;
if(dp[dx][dy]) {
s += dp[dx][dy] + 1; continue; }
s += dfs(dx, dy, grid, dp) + 1;
s %= mod;
}
dp[x][y] = (s + dp[x][y]) % mod;
return dp[x][y];
}
int countPaths(vector<vector<int>>& grid) {
n = grid.size(), m = grid[0].size();
vector<vector<int>> dp(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!dp[i][j]) {
dfs(i, j, grid, dp);
}
}
}
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
res = (res + dp[i][j] + 1) % mod;
return res;
}
};
边栏推荐
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
- TCP packet sticking problem
- MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
- STM32+HC05串口蓝牙设计简易的蓝牙音箱
- Windows connects redis installed on Linux
- Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
- 阿里云国际版ECS云服务器无法登录宝塔面板控制台
- 推荐好用的后台管理脚手架,人人开源
猜你喜欢
Coco2017 dataset usage (brief introduction)
std::true_type和std::false_type
Four processes of program operation
阿里云国际版ECS云服务器无法登录宝塔面板控制台
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
1700C - Helping the Nature
【Swoole系列2.1】先把Swoole跑起来
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
随机推荐
Tree-LSTM的一些理解以及DGL代码实现
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
Windows连接Linux上安装的Redis
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
1700C - Helping the Nature
MSF horizontal MSF port forwarding + routing table +socks5+proxychains
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Coco2017 dataset usage (brief introduction)
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
高精度运算
具体说明 Flume介绍、安装和配置
Wchars, coding, standards and portability - wchars, encodings, standards and portability
模板于泛型编程之declval
Codeforces Round #803 (Div. 2)
用友OA漏洞学习——NCFindWeb 目录遍历漏洞
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
Five data structures of redis
std::true_type和std::false_type
Alibaba cloud international ECS cannot log in to the pagoda panel console