|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +/* |
| 10 | +* |
| 11 | +Problem Statement - |
| 12 | +Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray |
| 13 | +whose sum is greater than or equal to target. |
| 14 | +If there is no such subarray, return 0 instead. |
| 15 | +
|
| 16 | +Output - |
| 17 | +1. minSubArrayLen(7, [2, 3, 1, 2, 4, 3]): 2 |
| 18 | +---------------------------------------------------------------------------------------------------- |
| 19 | +2. minSubArrayLen(4, [1, 4, 4]): 1 |
| 20 | +---------------------------------------------------------------------------------------------------- |
| 21 | +3. minSubArrayLen(11, [1, 1, 1, 1, 1, 1, 1, 1]): 0 |
| 22 | +---------------------------------------------------------------------------------------------------- |
| 23 | +4. minSubArrayLen(10, [1, 2, 3, 4]): 4 |
| 24 | +---------------------------------------------------------------------------------------------------- |
| 25 | +5. minSubArrayLen(5, [1, 2, 1, 3]): 3 |
| 26 | +---------------------------------------------------------------------------------------------------- |
| 27 | +6. minSubArrayLen(15, [5, 4, 9, 8, 11, 3, 7, 12, 15, 44]): 1 |
| 28 | +---------------------------------------------------------------------------------------------------- |
| 29 | +
|
| 30 | +Solution |
| 31 | +We use the windowSize variable to store the size of the minimum subarray, initializing it to MaxInt64. |
| 32 | +We use a start variable to track the left end of the subarray. Initially, we’ll set it to 0. |
| 33 | +We loop over the input array using an end variable to track the right end of the subarray. |
| 34 | +In each iteration, we add the value that has just entered the window to the running sum of the window contents. |
| 35 | +If the sum of our current subarray exceeds or equals the target value, |
| 36 | +we’ll compare the current subarray size with the window size already present. |
| 37 | +The smaller of the two values will be stored in windowSize. We then try to find a smaller subarray that meets the same condition. |
| 38 | +We slide the starting point of the subarray forward and check the sum from that point to the end of the subarray, |
| 39 | +checking the condition and updating windowSize where applicable. |
| 40 | +We repeat the process until the end of the array is reached. If the required minimum subarray is present, we return its length. |
| 41 | +*/ |
| 42 | +func minSubArrayLen(target int, nums []int) int { |
| 43 | + // write your code here |
| 44 | + // your code will replace this placeholder return statement |
| 45 | + // Initializing windowSize to a max number |
| 46 | + windowSize := math.MaxInt64 |
| 47 | + |
| 48 | + // Initialize start pointer to 0 and sum to 0 |
| 49 | + start, totalSum := 0, 0 |
| 50 | + |
| 51 | + for end, _ := range nums { |
| 52 | + totalSum += nums[end] |
| 53 | + // Check if we can remove elements from the start side of the subarray |
| 54 | + // while still satisfying the target condition |
| 55 | + for totalSum >= target { |
| 56 | + // Finding size of current subarray |
| 57 | + currSubArrSize := (end + 1) - start |
| 58 | + windowSize = min(windowSize, currSubArrSize) |
| 59 | + totalSum -= nums[start] |
| 60 | + start += 1 |
| 61 | + } |
| 62 | + } |
| 63 | + if windowSize != math.MaxInt64 { |
| 64 | + return windowSize |
| 65 | + } else { |
| 66 | + return 0 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func min(v1 int, v2 int) int { |
| 71 | + if v1 < v2 { |
| 72 | + return v1 |
| 73 | + } |
| 74 | + return v2 |
| 75 | +} |
| 76 | + |
| 77 | +func main() { |
| 78 | + targets := []int{ |
| 79 | + 7, |
| 80 | + 4, |
| 81 | + 11, |
| 82 | + 10, |
| 83 | + 5, |
| 84 | + 15, |
| 85 | + } |
| 86 | + numsList := [][]int{ |
| 87 | + {2, 3, 1, 2, 4, 3}, |
| 88 | + {1, 4, 4}, {1, 1, 1, 1, 1, 1, 1, 1}, |
| 89 | + {1, 2, 3, 4}, {1, 2, 1, 3}, |
| 90 | + {5, 4, 9, 8, 11, 3, 7, 12, 15, 44}, |
| 91 | + } |
| 92 | + for i, target := range targets { |
| 93 | + result := minSubArrayLen(target, numsList[i]) |
| 94 | + fmt.Printf("%d.\tminSubArrayLen(%d, %s): %d\n", i+1, target, |
| 95 | + strings.Replace(fmt.Sprint(numsList[i]), " ", ", ", -1), result) |
| 96 | + fmt.Printf("%s\n", strings.Repeat("-", 100)) |
| 97 | + } |
| 98 | +} |
0 commit comments