0% completed
Solution: Smallest Subarray With a Greater Sum
.....
.....
.....
Anonymous
· 2 years ago
import math class Solution: def findMinSubArray(self, s, arr): sum = 0 j = 0 min_sum = float('inf') for i in range(len(arr)): sum += arr[i] while sum >= s: diff = i - j + 1 min_sum = min(min_sum, diff) sum -= arr[j] j += 1 # inf happens if we have nothing >= s we want to return 0 not inf return min_sum if min_sum != float('inf') else 0
Anonymous
· 3 years ago
I think there is a bug in the solution.
The line
min_length = min(min_length, window_end - window_start) should be min_length = min(min_length, window_end - window_start + 1)
It works with the +1.
Murthy
Anonymous
· 4 years ago
Why would time complexity be O(n+n) instead of maybe something like O(n*n). [1,2,3...infinity], s = infinity. While loop would have to run in the worst case infinity times with each process?
Anonymous
· 4 years ago
I was using if instead of while, wondering why my length result was wrong. I understand now why shrinking the window until the sum is less than S is important.

Anonymous
· 4 years ago
I dont understand the logic behind using the math.in and the integer.max_value. can someone please explain how this works
Anonymous
· 4 years ago
Can someone explain how did they calculate the time complexity?
Anonymous
· 4 years ago
In the python solution, here's a more pythonic way of returning min_length. I also just prefer using float('inf') instead of importing math.
return (min_length if min_length != float('inf') else 0)
Anonymous
· 5 years ago
can someone explain the necessity of the check:
if min_length == math.inf
in the Python3 solution? How would min_length ever be an infinite number in this scenario?
Anonymous
· 5 years ago
Could please describe the logic of inner while loop running only once? what is the worst case? Dont you think at worst it can be On2?
Anonymous
· 5 years ago
I think there is a better solution for
Smallest Subarray with a Greater Sum (easy)
We dont have to use two loops, even though the inner one runs only once. Can you please suggest if this function is ok? public int smallest(int[] array , int tarSum) {
int smallest_Elements = 0; int sum=0; int startPtr = 0; // O (N) for(int endPtr =0;endPtr=tarSum) { int currentElements = (endPtr - startPtr)+1; if(smallest_Elements==0 || currentElementsendPtr) { endPtr = startPtr; endPtr--; } }
}
return smallest_Elements;
}
Reading Progress
0%