|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1031 lang=java |
| 3 | + * |
| 4 | + * [1031] Maximum Sum of Two Non-Overlapping Subarrays |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (56.54%) |
| 10 | + * Likes: 456 |
| 11 | + * Dislikes: 25 |
| 12 | + * Total Accepted: 16.2K |
| 13 | + * Total Submissions: 28.7K |
| 14 | + * Testcase Example: '[0,6,5,2,2,5,1,9,4]\n1\n2' |
| 15 | + * |
| 16 | + * Given an array A of non-negative integers, return the maximum sum of |
| 17 | + * elements in two non-overlapping (contiguous) subarrays, which have lengths L |
| 18 | + * and M. (For clarification, the L-length subarray could occur before or |
| 19 | + * after the M-length subarray.) |
| 20 | + * |
| 21 | + * Formally, return the largest V for which V = (A[i] + A[i+1] + ... + |
| 22 | + * A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either: |
| 23 | + * |
| 24 | + * |
| 25 | + * 0 <= i < i + L - 1 < j < j + M - 1 < A.length, or |
| 26 | + * 0 <= j < j + M - 1 < i < i + L - 1 < A.length. |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * |
| 34 | + * |
| 35 | + * Example 1: |
| 36 | + * |
| 37 | + * |
| 38 | + * Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2 |
| 39 | + * Output: 20 |
| 40 | + * Explanation: One choice of subarrays is [9] with length 1, and [6,5] with |
| 41 | + * length 2. |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * Example 2: |
| 46 | + * |
| 47 | + * |
| 48 | + * Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2 |
| 49 | + * Output: 29 |
| 50 | + * Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] |
| 51 | + * with length 2. |
| 52 | + * |
| 53 | + * |
| 54 | + * |
| 55 | + * Example 3: |
| 56 | + * |
| 57 | + * |
| 58 | + * Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3 |
| 59 | + * Output: 31 |
| 60 | + * Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] |
| 61 | + * with length 3. |
| 62 | + * |
| 63 | + * |
| 64 | + * |
| 65 | + * |
| 66 | + * Note: |
| 67 | + * |
| 68 | + * |
| 69 | + * L >= 1 |
| 70 | + * M >= 1 |
| 71 | + * L + M <= A.length <= 1000 |
| 72 | + * 0 <= A[i] <= 1000 |
| 73 | + * |
| 74 | + * |
| 75 | + * |
| 76 | + * |
| 77 | + * |
| 78 | + */ |
| 79 | + |
| 80 | +// @lc code=start |
| 81 | +class Solution { |
| 82 | + int callme(int[] A, int L, int M){ |
| 83 | + int n = A.length, cur = 0; |
| 84 | + int[] dp = new int[n]; |
| 85 | + for(int i=0; i<n; i++){ |
| 86 | + cur += A[i]; |
| 87 | + if(i>=L) |
| 88 | + cur -= A[i-L]; |
| 89 | + else if(i < L-1) |
| 90 | + continue; |
| 91 | + dp[i] = Math.max( i == 0 ? 0 : dp[i-1], cur); |
| 92 | + } |
| 93 | + cur = 0; |
| 94 | + int maxtillnow = 0, ans = 0; |
| 95 | + for(int i=n-1; i>=L; i--){ |
| 96 | + cur += A[i]; |
| 97 | + if(i+M < n) |
| 98 | + cur -= A[i+M]; |
| 99 | + else if(i+M > n) |
| 100 | + continue; |
| 101 | + maxtillnow = Math.max(maxtillnow, cur); |
| 102 | + ans = Math.max(ans, dp[i-1]+maxtillnow); |
| 103 | + } |
| 104 | + return ans; |
| 105 | + } |
| 106 | + public int maxSumTwoNoOverlap(int[] A, int L, int M) { |
| 107 | + return Math.max(callme(A, L, M), callme(A, M, L)); |
| 108 | + } |
| 109 | +} |
| 110 | +// @lc code=end |
0 commit comments