当前位置:网站首页>Leetcode hot topic 100 topic 21-25 solution
Leetcode hot topic 100 topic 21-25 solution
2022-06-11 06:51:00 【A Xuan is going to graduate~】

T24 53. Maximum number of words and ( secondary )
Topic presentation :
Power button
https://leetcode-cn.com/problems/maximum-subarray/
Ideas 1
Use a two-dimensional array log[i][j], Record No. i The number to the first j The number and . Traversing the upper triangle of a two-dimensional array , Find the maximum fields and .
![\left\{\begin{matrix} log[i][i] = nums[i]& &(1) \\ log[i][j] = nums[i][j-1]+nums[j]& i!=j & (2)\end{matrix}\right.](http://img.inotgo.com/imagesLocal/202203/02/202203020525261158_1.gif)
When submitting a topic , Show Memory limit exceeded .
Considering the formula of the above formula (2), When traversing the current i when , Before i-1 Row access data is not used , So change the two-dimensional array to one-dimensional array log[i]. Only record the current from i The sum of numbers as the starting point . When submitting a topic , Show Time limit exceeded .
After reading the solution , reflection : If the current sum is negative , Then there is no need to traverse . So you can just jump out of the loop . Submit the title , Still show Time limit exceeded .( reason : If it's all positive , Then we have to traverse n The square of . But the answer has been found in the first iteration )
So I used the answer in the comment area , One pass traversal . If sum<0, Explain that the previous numbers are useless , You can change the next number to start traversing . Until it reaches the end .
The code is as follows :
class Solution {
public int maxSubArray(int[] nums) {
int len = nums.length;
int max = nums[0];
int sum = 0;
for(int i = 0;i<len;i++){
sum+=nums[i];
max = Math.max(sum,max);
if(sum<0)
sum = 0;
}
return max;
}
}T25 55. Jumping game ( secondary )
Topic presentation : Power button
https://leetcode-cn.com/problems/jump-game/
Ideas 1(dfs)
Use dfs, But you don't need to visit the location you have already visited , So set a visit The array is used to record whether the current position is accessed . If the current location has been accessed , No longer continue to access from the current location .
Code display :
class Solution {
public boolean canJump(int[] nums) {
int len = nums.length;
int i;
int[] visit = new int[len];
for(i = 0;i<=nums[0];i++){
if(visit[i]!=1){
visit[i] = 1;
if(dfs(nums,i,len,visit)==true)
return true;
}
}
return false;
}
public boolean dfs(int[] nums,int cur,int len,int[] visit){
if(cur==len-1)
return true;
else if(cur<len-1){
for(int i=1;i<=nums[cur];i++){
if(visit[cur+i]!=1){
visit[cur+i] = 1;
if(dfs(nums,cur+i,len,visit)==true){
return true;
}
}
}
}
return false;
}
}Ideas 2(bfs)
Using queues to achieve breadth traversal , Also set a visit Array , Used to record whether the current location has been accessed , If visited , You don't need to visit again .
Code implementation :
class Solution {
public boolean canJump(int[] nums) {
int len = nums.length;
int cur = 0;
Queue<Integer> q = new LinkedList<Integer>();
int[] visit = new int[len];
q.add(0);
int num = nums[0];
if(len==1)
return true;
while(q.isEmpty()==false){
num = q.peek();
for(int i = 1;i<=nums[num];i++){
if(i+num<=len-1 && visit[i+num]!=1){
if(i+num==len-1)
return true;
q.add(i+num);
visit[i+num] = 1;
}
}
q.remove();
}
return false;
}
}Ideas 3
Record the maximum location that can be accessed , If the maximum accessible location is greater than or equal to the last location , You can access the last location . So record from the first position , Keep cycling to the maximum position , During the cycle , If the maximum location is updated , Update the value of the maximum position . This method , The time complexity is O(n).
Code display :
class Solution {
public boolean canJump(int[] nums) {
int len = nums.length;
int cur = 0;// Record the current access location
int max = nums[0];// Record the maximum location currently accessible
while(cur<=max){ // The key step !!!
if(max>=len-1)
return true;
int num = nums[cur];
max = Math.max(max,num+cur);// If the maximum location currently accessible changes , Update the maximum location
cur++;
}
return false;
}
}边栏推荐
- 617. merge binary tree
- Differences between FindIndex and indexof
- saltstack部署lnmp
- Appclips & tips (continuous update)
- 【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
- 通过 Ingress 进行灰度发布
- 品牌定位个性六种形态及结论的重大意义
- Biweekly investment and financial report: capital rush yuan universe game
- latex 各种箭头/带文字标号的箭头/可变长箭头
- 洛谷P1091合唱队形(最长上升子序列)
猜你喜欢

VTK vtkplane and vtkcutter use

.NET C#基础(6):命名空间 - 有名字的作用域

Do you use typescript or anyscript

100. same tree

洛谷P1091合唱队形(最长上升子序列)

Redux learning (II) -- encapsulating the connect function

网狐游戏服务器房间配置向导服务定制功能页实现

latex 各种箭头/带文字标号的箭头/可变长箭头

Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically

100. 相同的树
随机推荐
Notice on organizing the application for the first edition of Ningbo key software in 2022
ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
Deep Attentive Tracking via Reciprocative Learning
How to arrange the dataframe from small to large according to the absolute value of a column?
538. convert binary search tree to cumulative tree
JVM from getting started to giving up 2: garbage collection
Differences between FindIndex and indexof
Flutter 约束容器组件
节流和防抖
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen
JS implementation of graphic merging and sorting process [source code attached]
关于parseInt()
Scripy web crawler series tutorials (I) | construction of scripy crawler framework development environment
How to make time planning
河南高考VS天津高考(2008年-2021年)
About daily report plan
不引入第三个变量,交换两个值
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
instanceof到底是怎样判断引用数据类型的!
021 mongodb database from getting started to giving up