[Python] Which Subarray in this array does the sum equal to 11 [closed]

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    Am doing leetcode problem for Minimum Size Subarray Sum.


    Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
    ​

    I am getting this testcase wrong:

    [1,2,3,4,5], target 11.

    My program returns 0, but apparently the correct answer is 3? Which 3 elements return 11? I assume subarray means consecutively, otherwise 5 4 2 can work. I am confused, pretty sure I looped through all of them.

    i = 0 count 2
    sum 3
    i = 0 count 3
    sum 6
    i = 0 count 4
    sum 10
    i = 0 count 5
    sum 15
    i = 1 count 2
    sum 5
    i = 1 count 3
    sum 9
    i = 1 count 4
    sum 14
    i = 2 count 2
    sum 7
    i = 2 count 3
    sum 12
    i = 3 count 2
    sum 9

    Continue reading...

Compartilhe esta Página