当前位置:网站首页>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
边栏推荐
- Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
- Is the securities account opened by qiniu safe?
- 1672. Total assets of the richest customers
- Bi skills - permission axis
- 从实时应用角度谈通信总线仲裁机制和网络流控
- Other InterSystems%net tools
- Leetcode fizzbuzz C # answer
- 2022年字节跳动日常实习面经(抖音)
- Scala基础教程--17--集合
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
猜你喜欢

Wireshark网络抓包

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Li Kou brush question diary /day2/2022.6.24

Deleting nodes in binary search tree

Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?

Nebula importer data import practice

2022年字节跳动日常实习面经(抖音)

神经网络物联网平台搭建(物联网平台搭建实战教程)

Scala基础教程--17--集合

Angry bird design based on unity
随机推荐
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
技术分享 | 接口测试价值与体系
Caché WebSocket
node_exporter部署
小发猫物联网平台搭建与应用模型
奥迪AUDI EDI INVOIC发票报文详解
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Use canal and rocketmq to listen to MySQL binlog logs
整理混乱的头文件,我用include what you use
C语言打印练习
其他InterSystems %Net工具
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
26. Delete the duplicate item C solution in the ordered array
Have you guys ever used CDC direct Mysql to Clickhouse
From automation to digital twins, what can Tupo do?
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
Using FTP
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
Scala基础教程--19--Actor