描述

Given an array of positive integers nums, return the maximum possible sum of an ascending subarray in nums.

A subarray is defined as a contiguous sequence of numbers in an array.

A subarray [nums[l], nums[l+1], …, nums[r-1], nums[r]] is ascending if for all i where l <= i < r, numsi < nums[i+1]. Note that a subarray of size 1 is ascending.

Example 1:

Input: nums = [10,20,30,5,10,50]
Output: 65
Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.

Example 2:

Input: nums = [10,20,30,40,50]
Output: 150
Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.

Example 3:

Input: nums = [12,17,15,13,10,11,12]
Output: 33
Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.

Example 4:

Input: nums = [100,10,1]
Output: 100

Note:

1 <= nums.length <= 100
1 <= nums[i] <= 100

解析

根据题意,就是找出升序子列表的最大和。如果 nums 长度为 1 ,直接返回 nums[0] 。否则使用 total 表示当前子列表的和, max_result 表示最终需要返回的最大和。遍历 nums ,如果当前的元素小于等于前一个元素,那么直接将 total 设置为当前元素,表示从这里开始往后找新的升序子列表。如果当前元素大于前一个元素,那么当前的升序列表还可以找下去,将该元素加入 total ,然后找出 total 和 max_result 的最大值来更新 max_result ,遍历结束即可得到最终的结果 max_result 。

解答

class Solution(object):def maxAscendingSum(self, nums):""":type nums: List[int]:rtype: int"""if len(nums) == 1:return nums[0]total = nums[0]max_result = nums[0]for i in range(1, len(nums)):e = nums[i]if e>nums[i-1]:total += emax_result = max(max_result, total)else:total = ereturn max_result

运行结果

Runtime: 16 ms, faster than 93.91% of Python online submissions for Maximum Ascending Subarray Sum.
Memory Usage: 13.6 MB, less than 9.68% of Python online submissions for Maximum Ascending Subarray Sum.

原题链接:https://leetcode.com/problems/maximum-ascending-subarray-sum/

您的支持是我最大的动力

leetcode 1800. Maximum Ascending Subarray Sum(python)相关推荐

  1. LeetCode —— 257. 二叉树的所有路径(Python)

    给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: -------- 解题思路: (1)用变量string记录从根结点到当前结点经过的结点路径. (2) ...

  2. LeetCode 325. Maximum Size Subarray Sum Equals k

    这一题开始以为是sliding window,后来发现,因为有负数,sum不具有单调性,没有办法用slidng window找出optimal solution. 如果考虑brute force的做法 ...

  3. leetcode 1313. Decompress Run-Length Encoded List(python)

    描述 We are given a list nums of integers representing a list compressed with run-length encoding. Con ...

  4. LeetCode —— 897. 递增顺序查找树(Python)

    给你一个树,请你 按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点. 示例 : 来源:力扣(LeetCode) 链接:https://leetcode- ...

  5. Two Sum(python)两数相加

    题目:Two Sum Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, ...

  6. Leetcode —— 208. 实现 Trie (前缀树)(Python)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie();trie.insert(" ...

  7. leetcode 1641. Count Sorted Vowel Strings(python)

    描述 Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i ...

  8. 【Leetcode】 刷题之路1(python)

    leetcode 刷题之路1(python) 看到有大佬总结了一些相关题目,想着先刷一类. 1.两数之和 15.三数之和 16.最接近的三数之和 11.盛最多的水 18.四数之和 454.四数相加II ...

  9. LeetCode刷题——哈希表(python语言)

    LeetCode刷题--哈希表(python语言) 一.哈希表 1.1 哈希表的概念 哈希表,也叫散列表.其实可以很像python的字典,也就是键(key)值(Hash(key))对,最简单也最常用的 ...

  10. [LeetCode]题解(python):150-Evaluate Reverse Polish Notation

    题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...

最新文章

  1. Mysql高级调优篇——第五章:Sql调优在面试中深度剖析
  2. 转强大Vim的配额置
  3. 剑指offer 面试16题
  4. 【Android应用开发】 Android 崩溃日志 本地存储 与 远程保存
  5. java 依赖算法_java – Maven 2 – 从传递依赖版本定义依赖版本
  6. 用Python实现希尔排序
  7. Hadoop之MapReduce程序应用一读取专利引用数据集并对它进行倒排
  8. MongoDB 字符串值长度条件查询
  9. 3月任务--target
  10. 一亩地等于多少平方米?
  11. 使用Golang实现的快速排序
  12. java 静态方法 异常_java – 如何模拟一个void静态方法来抛出与Powermock的异常?...
  13. 多载波瑞利信道matlab,瑞利信道仿真matlab.doc
  14. alook浏览器_Alook浏览器
  15. 什么是搜索引擎?有什么作用?
  16. STM32cubeHAL ADC+TIM+DMA (二)
  17. Redis-简单介绍及基本数据类型【随笔一】
  18. 江苏省计算机应用基础统考,年江苏省普通高“专转本”统一考试计算机应用基础.doc...
  19. 【ClickHouse SQL 极简教程】ClickHouse SQL之数据定义语言 DDL
  20. 如何在VSCode中运行js代码

热门文章

  1. 通过池塘配置ip实验
  2. Wagtail 教程 4:自动生成文章右侧TOC导航
  3. BMVC reading list
  4. 在Mac电脑上:更改移动硬盘的格式
  5. html-box-sizing
  6. 多元函数偏导数连续、存在与可微的关系
  7. 读者问题答疑:4 个超级简单的 Linux 基础考试题!
  8. 华为存储iscsi配置_使用华为存储配置ISCSI存储方法和iSCSI建立连接提示目标错误...
  9. ESP8266 读取MPU-6050数据OLED显示
  10. sata接口 图解 定义_【sata硬盘三个接口】sata硬盘接口图解_sata硬盘接口电路