当前位置:网站首页>Maximum ascending subarray sum of leetcode simple problem
Maximum ascending subarray sum of leetcode simple problem
2022-06-29 05:53:00 【·Starry Sea】
subject
I'll give you an array of positive integers nums , return nums In a Ascending The maximum possible elements of the subarray and .
A subarray is a sequence of consecutive numbers in an array .
Known subarray [numsl, numsl+1, …, numsr-1, numsr] , If for all i(l <= i < r),numsi < numsi+1 All set up , This subarray is called Ascending Subarray . Be careful , The size is 1 The subarray of is also treated as Ascending Subarray .
Example 1:
Input :nums = [10,20,30,5,10,50]
Output :65
explain :[5,10,50] Is the element and the largest ascending subarray , The largest sum of elements is 65 .
Example 2:
Input :nums = [10,20,30,40,50]
Output :150
explain :[10,20,30,40,50] Is the element and the largest ascending subarray , The largest sum of elements is 150 .
Example 3:
Input :nums = [12,17,15,13,10,11,12]
Output :33
explain :[10,11,12] Is the element and the largest ascending subarray , The largest sum of elements is 33 .
Example 4:
Input :nums = [100,10,1]
Output :100
Tips :
1 <= nums.length <= 100
1 <= nums[i] <= 100
source : Power button (LeetCode)
Their thinking
There may be multiple ascending subarrays in an array , Traverse the array to check whether the elements are in ascending order and calculate the sum , If the element does not conform to ascending order , It means that the current ascending sub array has been traversed , Start traversing the next ascending subarray .
class Solution:
def maxAscendingSum(self, nums: List[int]) -> int:
s=nums[0]
MAX=nums[0]
for i in range(1,len(nums)):
if nums[i]>nums[i-1]:
s+=nums[i]
else:
if MAX<s:
MAX=s
s=nums[i]
return max(s,MAX)

边栏推荐
- Week 12 - task 2- shoulder to shoulder cadres
- In 2022, I haven't found a job yet. I have been unemployed for more than one year. What is the "old tester" for eight years?
- Devops development, operation and maintenance Basics: using Jenkins to automatically build projects and notify by email
- Week 10 - task 0- execution process instance resolution of constructors and destructors
- Research on heuristic intelligent task scheduling
- Implementation of queue
- Tcapulusdb Jun · industry news collection (VI)
- Pytest (7) -yield and termination function
- Tcapulusdb Jun · industry news collection (V)
- STI, one controller
猜你喜欢

2022 recommended trend toy blind box industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuously up

PCI Verilog IP

Quickly write MVVM code using source generators

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

Analysis report on the investment market situation of the development planning prospect of the recommended chip industry research industry in 2022 (the attachment is a link to the network disk, and th

Summary of redis basic knowledge points

Rich material libraries make modeling easy and efficient for developers

使用VS创建静态链接库.lib并使用

Jenkins operation Chapter 5 trigger, configuration webhook, Jenkins parameterized construction

Ctrip launched the "3+2" office mode. Are you sour?
随机推荐
2022-02 Microsoft vulnerability notification
Use of sed in shell script
How to hand over complex legacy systems?
How to choose congestion model and anemia model
The easiest GUI to compile: dgui
Week 10 - task 1- fill in the blank: line class inherits point class
證券開戶安全麼,有沒有什麼危險呢
Kubernetes backup disaster recovery service product experience tutorial
How to use regex in file find
Easy to get started naturallanguageprocessing series topic 7 text classification based on fasttext
Tcapulusdb Jun · industry news collection (III)
64 commonly used terms for data analysis, really all!
Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep
Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code
Parsing rshub document auto generation API
Research on heuristic intelligent task scheduling
5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
Pytest (7) -yield and termination function
51 lines of code, self-made TX to MySQL software!
CCTV revealed that xumengtao won the black Technology: there was a virtual coach???