首页 > 试题广场 >

连续子链表最大和

[编程题]连续子链表最大和
  • 热度指数:1396 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入一个单链表,链表中一个或多个连续的整数组成一个子链表。求所有子链表和的最大值。

1.你能不使用额外的O(n)空间复杂度来完成该题吗?
2.你能使用递归解法来完成该题吗?
3.如果你能使用递归解法完成本题,本题也可以看成二叉树中的最大路径和的一个子问题,可以尝试递归解法该题之后,去突破二叉树中的最大路径和

数据范围:链表长度满足 ,链表中的值满足

示例1

输入

{1,-2,3,10,-4,7,2,-5}

输出

18
示例2

输入

{2}

输出

2
示例3

输入

{-10}

输出

-10

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
int FindGreatestSumOfSubArray(ListNode* head) {
        // write code here
        int res =head->val;
        int presum =0;
        while(head){
            presum =max(presum+head->val,head->val);
            res =max(res,presum);
            head =head->next;
        }
        return res;

发表于 2022-05-21 12:41:58 回复(0)
这个是最简单的了,和为0时重置
public int FindGreatestSumOfSubArray (ListNode head) {
        int max = 0;
        int sum = 0;
        while(head != null) {
            sum += head.val;
            if (sum < 0) {
                sum = 0;
            }

            if (sum > max) {
                max = sum;
            }
            head = head.next;
        }

        return max;
    }


发表于 2023-05-13 01:47:17 回复(0)
package main
import _"fmt"
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @return int整型
*/
func FindGreatestSumOfSubArray( head *ListNode ) int {
    sum,max:=head.Val,head.Val
    for p:=head.Next;p!=nil;p=p.Next{
        if sum+p.Val<=p.Val{
            sum=p.Val
        }else{
            sum+=p.Val
        }
        if sum>max{
            max=sum
        }
    }
    return max
}

发表于 2023-03-09 08:09:30 回复(0)
		// write code here
		if (head.next == null) {
			return head.val;
		}
		ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
		while (head != null) {
			integerArrayList.add(head.val);
			head = head.next;
		}
		int[] headIntArray = integerArrayList.stream().mapToInt(Integer::intValue).toArray();
        int len = headIntArray.length;
        int[] dp = new int[len];
        int max = headIntArray[0];
        dp[0] = headIntArray[0];
        for (int i = 1; i < len; i++) {
            dp[i] = Math.max(dp[i-1]+headIntArray[i],headIntArray[i]);
            max = Math.max(dp[i],max);
        }
        return max;

发表于 2022-08-10 23:06:57 回复(0)
import java.util.*;
public class Solution {
  
    public int FindGreatestSumOfSubArray (ListNode head) {
        // 使用动态规划的思想,达到时间复杂度O(n),空间复杂度O(1)
        //遍历到每一个节点,就记录下当前节点的最大子链表和,遍历到末尾,返回就行
        //这里是链表,事先不知道长度,所以要创建dp[] 数组还得先遍历一次链表,所以‘
        //这里只用两个个变量,一个pre代表dp[i] ,一个max记录最大和,
        //递推方程: dp[i]=Math.max(nums[i],dp[i-1]+nums[i]); 
        if(head.next==null)return head.val;
        int max=0;
        int pre=0;
        while(head!=null){
            pre=Math.max(head.val,pre+head.val);
            max=Math.max(max,pre);
            head=head.next;
        }
        return max;
    }
}

发表于 2022-07-30 12:06:24 回复(0)
int FindGreatestSumOfSubArray(ListNode* head) {
        vector<int>nums;
        while(head){
            nums.push_back(head->val);
            head=head->next;
        }
        vector<int>dp(nums.size());
        int ans=dp[0]=nums[0];
        for(int i=1;i<nums.size();i++){
            dp[i]=max(dp[i-1]+nums[i],nums[i]);
            ans=max(ans,dp[i]);
        }
        return ans;
    }

发表于 2022-07-27 16:23:22 回复(0)
static const auto io_sync_off = []() {
    std::ios::sync_with_stdio(false);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
    return nullptr;
}
();
class Solution {
  public:
    int FindGreatestSumOfSubArray(ListNode* head) {
        int a = head->val, b = INT_MIN;
        while (head) {
            b = max(head->val, b + head->val);
            a = max(a, b);
            head = head->next;
        }
        return a;
    }
};

发表于 2022-07-21 17:04:18 回复(0)
与连续子数组的最大和方法一致
    public int FindGreatestSumOfSubArray (ListNode head) {
        int ret = head.val, pre = 0;
        while (head != null) {
            pre = Math.max(pre + head.val, head.val);
            ret = Math.max(ret, pre);
            head = head.next;
        }
        return ret;
    }


发表于 2022-02-09 12:04:02 回复(0)