Leetcode algorithm Series _0862_ and minimum sub-arrays of at least K

Source: Internet
Author: User

0862_ and minimum sub-arrays of at least K

Title Description

Returns the length of the shortest non-empty contiguous subarray of a, and at least K for that subarray.

Returns 1 if there are no non-empty-empty arrays and at least K.

Example 1:

输入:A = [1], K = 1输出:1

Example 2:

输入:A = [1,2], K = 4输出:-1

Example 3:

输入:A = [2,-1,2], K = 3输出:3

Note:

1.  1 <= A.length <= 500002.  -10 ^ 5 <= A[i] <= 10 ^ 53.  1 <= K <= 10 ^ 9

Brute Force algorithm

func shortestSubarray(A []int, K int) int {    minLen := 50001    tail := len(A) - 1    for i := 0; i <= tail; i++ {        sum := A[i]        if sum >= K {            minLen = 1            break        }        for j := i + 1; j <= tail; j++ {            sum += A[j]            if sum >= K {                l := j - i + 1                if l < minLen {                    minLen = l                }                break            }        }    }    if minLen != 50001 {        return minLen    }    return -1}

High-performance version algorithm

Func Shortestsubarray (A []int, K int) int {size: = Len (A) Sums: = Make ([]int, size+1) for I, N: = Range A { If n >= K {return 1} sums[i+1] = Sums[i] + n} Res: = size + 1//Storage 0----I, may be compliant The head deque of the shortest substring of the condition: = Make ([]int, 0, 0) for I: = 0; i < size+1; i++ {for Len (deque) > 0 && (sums[i]-sums[deque[0]] >= K) {//I increment//may have J&GT I make sums[j]-sums[deque[0]] >= K//But due to j>i, so deque[0]---i is with deque[0] as the shortest qualifying substring of the head//Put de QUE[0] Delete res = min (res, i-deque[0]) deque = deque[1:]} for Len (deque) > 0 &AMP;&A mp (Sums[i] <= Sums[deque[len (deque)-1]) {//If present j>i>deque[r] makes Sums[j]-sums[deque[r]] >= K//due to Sums[deque[r]] >= sums[i]            then//SUMS[J]-sums[i] >= K must be established, and J-i < j-deque[r]//So, with Deque[r] as head, cannot be the shortest qualifying substring, delete Deque = Deque[:len (deQue)-1]} deque = append (Deque, i)} if res = = size+1 {return-1} return res} 

Personal ideas

    1. As opposed to brute force cracking, repeated calculations between the two elements and so must be optimized here, only need to loop through the array, storing the 0-i elements and
    2. If A<B,SUMS[B]-SUMS[A]>=K,B-A is the substring length
    3. Deque (double-ended queue) storage 0-i, head of the shortest substring that may match the condition
    4. Traverse sums, take deque [0], judge the element as the head of the substring, whether it meets the conditions, if it matches the condition, then it will be the array element as the head of the matching substring, the shortest substring, removed from the deque [0], reduce the number of sums later
    5. Take the tail of deque, the value is r,sums[r]>=sums[i], then if there is sums[j]-sums[r]>=k, then the substring between j>i>r,i->j must also meet the conditions, and shorter, R is the head of the substring, it is not possible to be the shortest field, where you can delete the tail of deque, reduce the number of times

Summarize

    • In the Leetcode community, there is the Java version of the algorithm code, the idea and the idea is exactly the same, but the time efficiency than Golang version, Golang version of 170ms, Java version 40+ms
    • The author after the comparison code, the main difference is that deque this storage container, in the JDK has the corresponding data structure deque double-ended queue, the data structure of the insertion efficiency caused, I guess Java in the structure of the two-way linked list implementation, in the process of inserting elements, do not need to expand, memory allocation efficiency is high
    • Golang version code, deque with slice slices, in the append process, continuous expansion, memory allocation, resulting in low efficiency
    • Interested friends can optimize deque this data structure, using a struct object, to build a doubly linked list structure, I believe that the efficiency and Java version should be similar, in this, space constraints, doubly linked list is not the core of the algorithm, the author ends
    • Data structure is easy to store and the effect on the algorithm is huge.

GitHub

    • Project Source is here
    • The author will always maintain the project, solve the algorithm problem in Leetcode, and write down his own ideas and opinions, and devote to the algorithm that everyone can understand.

Personal public number

    • Favorite friends can pay attention to, thank you for your support
    • Record the learning and life of the farmers in the code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.