- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我曾经在网上看到这个编码问题..
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
...并从 leetcode 用户那里得到了一个解决方案...
def maxSubArray(self, nums: List[int]) -> int:
sumVal = 0
ret = 0
for i in nums:
sumVal = max(0, sumVal) + i
ret = max(ret, sumVal)
return max(nums) if ret == 0 else ret
虽然经过一些“调试”后我也不知道它是如何工作的。
你能解释一下吗?
最佳答案
正如问题所述,它的所有要求都是使用连续元素在子数组中找到可能的最大值,即相邻元素。
这里的做法是一个一个遍历数组,将元素加到总和上,检查是否超过当前最大值,如果超过,则更新最大值。查看每行的内联注释。
def maxSubArray(self, nums: List[int]) -> int: #type hint to return an int value
sumVal = 0 #keeps the total sum
ret = 0 #return value
for i in nums: #iterates through every single value in the list
sumVal = max(0, sumVal) + i #gets the total sum for the upto the given i value
ret = max(ret, sumVal) #ret variable is updated with whichever is the max of `ret` or `sumVal`.
return max(nums) if ret == 0 else ret #retunrs max value of the array if ret = 0; this simply means array is of single element.Else return the value held in `ret`
虽然这段代码并不完整。它不会尝试检查连续值,而只是检查可能的总和。
运行这个,
def maxSubArray(nums):
sumVal = 0
ret = 0
for i in nums:
sumVal = max(0, sumVal) + i
ret = max(ret, sumVal)
return max(nums) if ret == 0 else ret
print(maxSubArray([8,1,-3,4,-1,2,1,-5,4]))
给出 12
。参见 here了解更多信息
关于python - 谁能解释这个 "Maxium Subarray"问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61013256/
我在使用 mongoose 包的 Node js 中有以下代码。 1)如果我使用填充方法,如何删除响应中基础数组中的字段总计?因为该数组是由对另一个集合的引用形成的。 2) 如何使用“campaign
我正在尝试创建一个函数,该函数将从较大数组中检查 3x3 数组元素 block 中的特定元素。 9...84.6. ..4..75.8 .3....... 3....1... .7.5.6.4
我正在开展一个项目,该项目需要找到包含在较大二维整数数组中的一些较小二维整数数组。 更具体地说,我将获得一个文本文件以供输入。文本文件将包含 N、M 和 K 值,以及填充“大”MxN 网格的整数。然后
最近我发现了这段 C 代码,用于查找数组中等于和的整数。 int subArraySum(int arr[], int n, int sum) { /* Initialize curr_sum as
我有一个关于将数组第一个元素的地址传递给(递归)函数的问题: selectionSort( &b[1], size-1); 据我所知,本地址被传递给函数时,函数参数必须是一个指针。 selection
我正在寻找这样的东西: virtualArray = VirtualArray((parent1,2:5,1:3), (parent2,1:15,5:7)) 它将构造类似于 SubArray 的东西,
我正在尝试运行一个名为 IsSubArray 的程序。有 2 个输入文本框。一个叫大阵,一个叫子阵。任务是检查子数组中的值是否连续匹配大数组中的数字。例如: 数组 1:1,3,6,5,4 数组 2:1
题目地址:https://leetcode.com/problems/binary-subarrays-with-sum/description/ 题目描述 Inan array A of 0s
题目地址:https://leetcode.com/problems/maximum-average-subarray-i/description/open in new window 题目描述
我曾经在网上看到这个编码问题.. Given an integer array nums, find the contiguous subarray (containing at least one
问。给定两个长度相等的数组 A 和 B,找到索引 [i,j] 的最大可能连续子数组,使得 max(A[i: j]) B[j]: Bq.pop() Bq.app
问。给定两个长度相等的数组 A 和 B,找到索引 [i,j] 的最大可能连续子数组,使得 max(A[i: j]) B[j]: Bq.pop() Bq.app
我听说 SubArray 和其他数组操作在未来是 eventually due for a performance overhaul。目前,由于 SubArray 运行缓慢,我的代码出现了一些相当小的
我试图用分而治之的方法解决最大子数组和问题,但是发生了运行时错误(StackOverFlow),我不知道如何处理它,我认为它的发生只是因为我的递归调用。这是我的方法(错误发生在第一个递归行): cla
我想获取 3D 数组的多个子数组。我可以使用 Stack 帖子中找到的函数在 2D 情况下拆分数组: def blockshaped(arr, nrows, ncols): h, w = ar
void kadane(int A[], int N, int& bestStart, int& bestEnd, int& bestSum) { int max_ending_here =
我正在试着解决Leetcode问题“1588,所有奇数长子数组之和”。‘给定一个正整数arr数组,返回arr的所有可能的奇数长子数组之和,其中一个子数组是数组的连续子序列’。。我的尝试如下:。它对一些
我正在试着解决Leetcode问题“1588,所有奇数长子数组之和”。‘给定一个正整数arr数组,返回arr的所有可能的奇数长子数组之和,其中一个子数组是数组的连续子序列’。。我的尝试如下:。它对一些
题目地址: https://leetcode.com/problems/minimum-size-subarray-sum/description/ 题目描述: Given an array of
题目地址:https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/ 题目描述: Given tw
我是一名优秀的程序员,十分优秀!