Leetcode Problem 1400-1499
PrefixSum O(N) Time
- We calculate the running sum (prefix sum) of the array.
- Every time we find minimum sum we got until now.
- return abs(minimum sum) + 1.
O(N) Time and O(N) Space Solution:
- Use greedy approach. For each kid check if candies[i] + extraCandies ≥ maximum in all Candies
O(N) Time and O(N) Space solution
- Use two pointers to create the new array of 2n elements. The first starting at the beginning and the other starting at (n+1)th position. Alternate between them and create the new array.
O(N) Time and O(N) Space solution
- Create a sum variable to store current sum in it ans push back it in the result array.
O(N) Time and O(1) Space optimization
- We can modify input array directly