摘要:
Problem
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.
Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.
Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
Example:
Input: [1,2,1,2,6,7,5,1], 2 Output: [0, 3, 5] Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5]. We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
Note:
nums.length will be between 1 and 20000. nums[i] will be between 1 and 65535. k will be between 1 and floor(nums.length / 3).Solution
class Solution { public int[] maxSumOfThreeSubarrays(int[] nums, int k) { //three parts: 0 ~ i-1, i ~ i+k-1, i+k ~ n-1 (i >= k) // (n-1) - (i+k) + 1 >= k ... so (i <= n-2k) if (nums == null || nums.length < 3*k) return null; int n = nums.length; int[] sum = new int[n+1]; int[] left = new int[n]; int[] right = new int[n]; int[] res = new int[3]; int max = 0; for (int i = 0; i < n; i++) { sum[i+1] = sum[i] + nums[i]; } int leftMax = sum[k]-sum[0]; left[k-1] = 0; for (int i = k; i < n; i++) { if (sum[i+1]-sum[i+1-k] > leftMax) { left[i] = i+1-k; leftMax = sum[i+1]-sum[i+1-k]; } else { left[i] = left[i-1]; } } int rightMax = sum[n]-sum[n-k]; right[n-k] = n-k; for (int i = n-1-k; i >= 0; i--) { if (sum[i+k]-sum[i] > rightMax) { right[i] = i; rightMax = sum[i+k]-sum[i]; } else { right[i] = right[i+1]; } } for (int i = k; i <= n-2*k; i++) { int l = left[i-1]; int r = right[i+k]; int curMax = sum[l+k]-sum[l] + (sum[i+k]-sum[i]) + (sum[r+k]-sum[r]); if (curMax > max) { max = curMax; res[0] = l; res[1] = i; res[2] = r; } } return res; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71850.html
Problem Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note:You may assume the intervals end point is alway...
摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...
摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...
Problem Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isnt one, return 0 instead. Note The sum of the entire nums array is guaranteed to fit ...
摘要:复杂度思路对于每一节点,考虑到这一个节点为止,所能形成的最大值。,是经过这个节点为止的能形成的最大值的一条支路。 Leetcode[124] Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. For this problem, a path is defined as any se...
阅读 1691·2021-10-18 13:34
阅读 3841·2021-09-08 10:42
阅读 1510·2021-09-02 09:56
阅读 1585·2019-08-30 15:54
阅读 3095·2019-08-29 18:44
阅读 3268·2019-08-26 18:37
阅读 2174·2019-08-26 12:13
阅读 429·2019-08-26 10:20