Skip to content

Commit

Permalink
update file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanh An committed Dec 15, 2019
1 parent 57c46e7 commit 5222933
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
- Sliding Window
- [Average of any contiguous subarray of size k](gtci/avg_subarray_test.go)
- [Maximum sum of any contiguous subarray of size k](gtci/max_subarray_test.go)
- [Smallest subarray with a given sum](gtci/min_subarray_test.go)
- [Smallest subarray with a given sum](gtci/smallest_subarray_test.go)
- **Other**
- Data structures
- Linked List
Expand Down
14 changes: 6 additions & 8 deletions gtci/min_subarray_test.go → gtci/smallest_subarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Approach:
- The difference between the previous problem and this one is that the size of
the sliding window is not fixed.
- Can still use the similar strategy to add up elements until their sum is greater
than equal to s.
than equal to s and view them as our sliding window.
- Shrink the window until the window's sum is smaller than s again while keep
updating the minimum length.
Expand Down Expand Up @@ -51,9 +51,7 @@ func TestMinSubarray(t *testing.T) {
}

func minSubarray(a []int, s int) int {
// min keeps track of the minimum length for a subarray. initialize it to
// be the max int as a starter.
min := math.MaxInt64
minLength := math.MaxInt64

// sum keeps track of the sum of a window while start keeps track of
// its start index.
Expand All @@ -66,7 +64,7 @@ func minSubarray(a []int, s int) int {
// target sum.
for sum >= s {
// update the minimum length at each step.
min = common.Min(min, end-start+1)
minLength = common.Min(minLength, end-start+1)

// subtract the start element and increase the start index to move
// the window ahead by one element.
Expand All @@ -76,9 +74,9 @@ func minSubarray(a []int, s int) int {
}

// let min=0 if there is no such subarray exists.
if min == math.MaxInt64 {
min = 0
if minLength == math.MaxInt64 {
minLength = 0
}

return min
return minLength
}

0 comments on commit 5222933

Please sign in to comment.