LeetCode solution summary 918. Maximum sum of circular subarrays

 Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

Given a  ring array of integersn  of length  , return  the largest possible sum of   non- empty subarrays . nums nums

A circular array  means that the end of the array will be connected to the beginning in a ring. Formally,  nums[i] the next element of is  nums[(i + 1) % n] ,  nums[i] and the previous element is  nums[(i - 1 + n) % n] .

A subarray  may contain  nums each element in the pinned buffer at most once. Formally, for subarrays  nums[i], nums[i + 1], ..., nums[j] , none exist  i <= k1, k2 <= j within  k1 % n == k2 % n .

Example 1:

Input: nums = [1,-2,3,-2]
 Output: 3
 Explanation: Get maximum sum 3 from subarray [3]

Example 2:

Input: nums = [5,-3,5]
 Output: 10
 Explanation: Get maximum sum from subarray [5,5] 5 + 5 = 10

Example 3:

Input: nums = [3,-2,2,-3]
 Output: 3
 Explanation: The maximum sum of 3 can be obtained from both the subarrays [3] and [3,-2,2]

hint:

  • n == nums.length
  • 1 <= n <= 3 * 104
  • -3 * 104 <= nums[i] <= 3 * 104

Problem-solving ideas:

* Problem-solving ideas: 
* First, we double the length of nums and find the prefix sum. 
* In this way, this question becomes a question of finding the maximum value of i>j, i<=j+n, prefixs[i]-prefixs[j] in the 2n array. 
* First, find the prefix sum, corresponding to the method maxSubarraySumCircular, and find the maximum value of prefixs[i]-prefixs[j] corresponding to the method findBixDiff. 
* In the findBixDiff method, construct a monotonic queue, which is divided into three scenarios: 
* 1. If the length of ij is greater than n, then j needs to be removed. 
* 2. If prefixes[i]-prefixs[j]>abs, update abs. 
*3. If prefixs[i]<=prefixs[k], the position of k in the queue loses meaning, because the position of i is later, and the value is smaller, which is more advantageous than k, so k is dequeued* and finally returns to 
abs .

code:

public class Solution918 {

    //    -2,1,-3,4,-1,2,1,-5,4
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        List<Integer> prefixs = new ArrayList<>();
        prefixs.add(0);
        int pre = prefixs.get(0);
        for (int i = 0; i < n * 2; i++) {
            pre += nums[i % n];
            prefixs.add(pre);
        }
        return findBixDiff(prefixs, n);
    }

    public int findBixDiff(List<Integer> list, int size) {
        int abs = Integer.MIN_VALUE;
        Deque<Pair<Integer, Integer>> deque = new ArrayDeque<>();
        deque.push(new Pair<>(0, list.get(0)));
        for (int i = 1; i < list.size(); i++) {
            if (!deque.isEmpty() && i - deque.peekFirst().getKey() > size) {
                deque.pollFirst();
            }
            Integer integer = list.get(i);
            abs = Math.max(integer - deque.peekFirst().getValue(), abs);
            while (!deque.isEmpty() && deque.peekLast().getValue() >= integer) {
                deque.pollLast();
            }
            deque.offerLast(new Pair<>(i, integer));
        }
        return abs;
    }

}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/131856137