当前位置:网站首页>The 300th weekly match of leetcode (20220703)
The 300th weekly match of leetcode (20220703)
2022-07-04 19:21:00 【[email protected]】
List of articles
6108. Decrypt the message
https://leetcode.cn/problems/decode-the-message/
Ideas : Simple simulation questions , First, according to the string key Create a comparison table , Then replace according to the comparison table message The characters in the , about key Each character in , It is only added to the comparison table when it is first visited , You can set the initial value to -1 Indicates that it has not been updated
class Solution {
public String decodeMessage(String key, String message) {
int[] table=new int[26];
Arrays.fill(table, -1);
int index=0;
for(int i=0;i<key.length();i++) {
if(key.charAt(i)==' ') {
// Space skipping
continue;
}
int ascii=key.charAt(i)-'a';// The current character ch Of ascii Code value
if(table[ascii]!=-1) {
//ch Has joined in table It's in skip
continue;
}
table[ascii]=index;// Update cross reference table
index++;
}
char[] ch=message.toCharArray();
for(int i=0;i<ch.length;i++) {
if(ch[i]==' ') {
continue;
}
ch[i]=(char) (table[ch[i]-'a']+'a');// Update according to the comparison table
}
return new String(ch);
}
}
6111. Spiral matrix IV
https://leetcode.cn/problems/spiral-matrix-iv/
Ideas : The key point is how to traverse a matrix counterclockwise , Specific ideas can be referred to 59. Spiral matrix II
Put 1-n^2 Replace the data in the linked list with the data in the linked list
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public int[][] spiralMatrix(int m, int n, ListNode head) {
int[][] matrix=new int[m][n];
int top=0,bottom=m-1,left=0,right=n-1;
ListNode cur=head;
while(true)
{
for(int i=left;i<=right;i++) {
// From left to right
if(cur!=null) {
matrix[top][i]=cur.val;
cur=cur.next;
}else {
matrix[top][i]=-1;
}
}
top++;
if(top>bottom)
break;
for(int i=top;i<=bottom;i++) {
// From top to bottom
if(cur!=null) {
matrix[i][right]=cur.val;
cur=cur.next;
}else {
matrix[i][right]=-1;
}
}
right--;
if(right<left)
break;
for(int i=right;i>=left;i--) {
// From right to left
if(cur!=null) {
matrix[bottom][i]=cur.val;
cur=cur.next;
}else {
matrix[bottom][i]=-1;
}
}
bottom--;
if(bottom<top)
break;
for(int i=bottom;i>=top;i--) {
// From bottom to top
if(cur!=null) {
matrix[i][left]=cur.val;
cur=cur.next;
}else {
matrix[i][left]=-1;
}
}
left++;
if(left>right)
break;
}
return matrix;
}
}
6109. Number of people who know the secret
https://leetcode.cn/problems/number-of-people-aware-of-a-secret/
Ideas :
Write it down as dp[i]
It means the first one i Tianxin knows the number of Secrets , Be careful , It's the new number , Not the total number , When we count the number of new people we know every day , Last statistical interval [n-forget+1,n]
The sum of the number of people who newly know the secret in the interval , That is, the total number of people who know the secret , Because other people who know the secret have forgotten
class Solution {
public int peopleAwareOfSecret(int n, int delay, int forget) {
int[] dp=new int[n+1];
dp[1]=1;
int mod=(int)(1e9+7);
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
// The first i Tianxin knows that people can be divided into sections [i-forget+1,i-delay] God knows who will tell
if(i>=j+delay&&i<j+forget){
// The first j Tianxin knows that people can tell the first i Man of heaven You can tell && I haven't forgotten
dp[i]+=dp[j];
dp[i]%=mod;
}
}
}
long ans=0;
for(int i=n-forget+1;i<=n;i++){
//[i-forget+1,n] New people in the day n Days have not forgotten
ans+=dp[i];
ans%=mod;
}
return (int)ans;
}
}
6110. The number of incremental paths in the grid graph
https://leetcode.cn/problems/number-of-increasing-paths-in-a-grid/
Ideas : Dynamic programming is also used here , But in general dp It solves problems from the bottom up , Here is top-down problem solving , Recursive solution , Top down dp A memo is usually added , Prevent double counting . Concrete ,dp[i][j]
It is expressed as a cell (i,j) The number of paths that meet the increment condition at the end , When calculating in cells (i,j) When the number of paths at the end that meet the increment condition , If you find a cell (i,j) Up, down, left and right of a grid (x,y) The value is less than grid[i][j]
Value , explain (i,j) The position can be transferred from this position , So add cells (x,y) The number of paths at the end dp[x][y]
class Solution {
int m,n;
int[][] grid;
int[][] memo;
int[][] dirs={
{
0,-1},{
0,1},{
-1,0},{
1,0}};
int mod=(int)(1e9+7);
public int countPaths(int[][] grid) {
int ans=0;
this.grid=grid;
this.m=grid.length;
this.n=grid[0].length;
memo=new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
ans+=dp(i,j);
ans%=mod;
}
}
return ans;
}
public int dp(int i,int j){
if(memo[i][j]!=0){
// By location (i,j) The number of paths at the end has been calculated
return memo[i][j];
}
long ans=1L;// The initial value is 1 because 1 Lattice (i,j) It is also a qualified path
for(int[] dir:dirs){
int x=i+dir[0],y=j+dir[1];
if(x>=0&&x<m&&y>=0&&y<n&&grid[x][y]<grid[i][j]){
ans+=dp(x,y);//(x,y)->(i,j)
ans%=mod;
}
}
memo[i][j]=(int)ans;
return memo[i][j];
}
}
//O(mn)
//O(mn)
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041738341584.html
边栏推荐
- node_exporter部署
- Pb extended DLL development (super chapter) (VII)
- 删除字符串中出现次数最少的字符【JS,Map排序,正则】
- 2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
- Unity editor extends C to traverse all pictures in folders and subdirectories
- 启牛开的证券账户安全吗?
- 基于unity的愤怒的小鸟设计
- DeFi生态NFT流动性挖矿系统开发搭建
- Nebula Importer 数据导入实践
- 876. Intermediate node of linked list
猜你喜欢
LM10丨余弦波动顺势网格策略
Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
Scala基础教程--15--递归
Li Kou brush question diary /day3/2022.6.25
神经网络物联网是什么意思通俗的解释
读写关闭的channel是啥后果?
node_exporter部署
My colleagues quietly told me that flying Book notification can still play like this
基于unity的愤怒的小鸟设计
2022 ByteDance daily practice experience (Tiktok)
随机推荐
2022养生展,健康展,北京大健康展,健康产业展11月举办
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
读写关闭的channel是啥后果?
php伪原创api对接方法
完善的js事件委托
LeetCode 赎金信 C#解答
神经网络物联网是什么意思通俗的解释
使用FTP
Li Chi's work and life summary in June 2022
IBM WebSphere MQ检索邮件
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
876. Intermediate node of linked list
2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
The 15th youth informatics competition in Shushan District in 2019
整理混乱的头文件,我用include what you use
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
6.26cf simulation match B: solution to array reduction problem
Detailed explanation of issues related to SSL certificate renewal
ThreadLocal原理与使用