牛客题霸-算法篇
子数组的最大累加和问题
相似的企业真题
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 256M,其他语言512M
热度指数:23861
本题知识点:
分治
动态规划
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
给定一个数组arr,返回子数组的最大累加和
例如,arr = [1, -2, 3, 5, -2, 6, -1],所有子数组中,[3, 5, -2, 6]可以累加出最大的和12,所以返回12.
题目保证没有全为负数的数据
[要求]
时间复杂度为
,空间复杂度为
示例1
输入
复制
[1, -2, 3, 5, -2, 6, -1]
[1, -2, 3, 5, -2, 6, -1]
返回值
复制
12
12
备注:
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here } }
class Solution { public: /** * max sum of the subarray * @param arr int整型vector the array * @return int整型 */ int maxsumofSubarray(vector
& arr) { // write code here } };
# # max sum of the subarray # @param arr int整型一维数组 the array # @return int整型 # class Solution: def maxsumofSubarray(self , arr ): # write code here
/** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ function maxsumofSubarray( arr ) { // write code here } module.exports = { maxsumofSubarray : maxsumofSubarray };
# # max sum of the subarray # @param arr int整型一维数组 the array # @return int整型 # class Solution: def maxsumofSubarray(self , arr ): # write code here
package main /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ func maxsumofSubarray( arr []int ) int { // write code here }
/** * max sum of the subarray * @param arr int整型一维数组 the array * @param arrLen int arr数组长度 * @return int整型 */ int maxsumofSubarray(int* arr, int arrLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # max sum of the subarray # @param arr int整型一维数组 the array # @return int整型 # class Solution def maxsumofSubarray(arr) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ def maxsumofSubarray(arr: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ fun maxsumofSubarray(arr: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ export function maxsumofSubarray(arr: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ func maxsumofSubarray ( _ arr: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ pub fn maxsumofSubarray(&self, arr: Vec
) -> i32 { // write code here } }
[1, -2, 3, 5, -2, 6, -1]
12