-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2875-MinimumSizeSubarrayInInfiniteArray.go
More file actions
185 lines (169 loc) · 5.98 KB
/
2875-MinimumSizeSubarrayInInfiniteArray.go
File metadata and controls
185 lines (169 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
// 2875. Minimum Size Subarray in Infinite Array
// You are given a 0-indexed array nums and an integer target.
// A 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.
// Return the length of the shortest subarray of the array infinite_nums with a sum equal to target.
// If there is no such subarray return -1.
// Example 1:
// Input: nums = [1,2,3], target = 5
// Output: 2
// Explanation: In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
// The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
// It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
// Example 2:
// Input: nums = [1,1,1,2,3], target = 4
// Output: 2
// Explanation: In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
// The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
// It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
// Example 3:
// Input: nums = [2,4,6,8], target = 3
// Output: -1
// Explanation: In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
// It can be proven that there is no subarray with sum equal to target = 3.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^5
// 1 <= target <= 10^9
import "fmt"
// // 解答错误 533 / 535
// func minSizeSubarray(nums []int, target int) int {
// inf := int(1e5)
// res, sum, left, right, n := inf, nums[0], 0, 1, len(nums)
// min := func (x, y int) int { if x < y { return x; }; return y; }
// for right < inf {
// if sum == target {
// res = min(res, right - left)
// }
// if sum > target {
// sum -= nums[left % n]
// left++
// } else {
// sum += nums[right % n]
// right++
// }
// }
// if res >= inf {
// return -1
// }
// return res
// }
func minSizeSubarray(nums []int, target int) int {
a, b, n, sum, pre := 0, 1 << 31, len(nums), 0, 0
for _, v := range nums {
sum += v
}
if target > sum {
a = n * (target / sum)
target -= target / sum * sum
}
if target == sum {
return n
}
min := func (x, y int) int { if x < y { return x; }; return y; }
pos := map[int]int{0: -1}
for i, v := range nums {
pre += v
if j, ok := pos[pre-target]; ok {
b = min(b, i - j)
}
if j, ok := pos[pre - (sum - target)]; ok {
b = min(b, n - (i - j))
}
pos[pre] = i
}
if b == 1 << 31 {
return -1
}
return a + b
}
func minSizeSubarray1(nums []int, target int) int {
sum, n := 0, len(nums)
for _, v := range nums {
sum += v
}
count := (target / sum) * n
target = target % sum
sum = 0
res := n + 1
min := func (x, y int) int { if x < y { return x; }; return y; }
for i, j := 0, 0; j < n * 2; j++ {
sum += nums[j % n]
for i <= j && sum > target {
sum -= nums[i%n]
i++
}
if sum == target {
res = min(res, j - i + 1)
}
}
if res == n + 1 {
return -1
}
return res + count
}
func minSizeSubarray2(nums []int, target int) int {
res, sum, n := 1 << 31, 0, len(nums)
for _, v := range nums {
sum += v
}
repeats := target / sum
target = target % sum
if target == 0 {
return repeats * n
}
min := func (x, y int) int { if x < y { return x; }; return y; }
left, subsum := 0, 0
// Perform sliding window on 2x of nums
for right := 0; right < 2 * n; right++ {
subsum += nums[right%n]
for left < right && subsum > target {
subsum -= nums[left%n]
left++
}
if subsum == target {
res = min(res, right - left + 1)
}
}
if res == 1 << 31 {
return -1
}
return repeats * n + res
}
func main() {
// Example 1:
// Input: nums = [1,2,3], target = 5
// Output: 2
// Explanation: In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
// The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
// It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
fmt.Println(minSizeSubarray([]int{1,2,3}, 5)) // 2
// Example 2:
// Input: nums = [1,1,1,2,3], target = 4
// Output: 2
// Explanation: In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
// The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
// It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
fmt.Println(minSizeSubarray([]int{1,1,1,2,3}, 4)) // 2
// Example 3:
// Input: nums = [2,4,6,8], target = 3
// Output: -1
// Explanation: In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
// It can be proven that there is no subarray with sum equal to target = 3.
fmt.Println(minSizeSubarray([]int{2,4,6,8}, 3)) // -1
fmt.Println(minSizeSubarray([]int{1,2,3,4,5,6,7,8,9}, 3)) // 1
fmt.Println(minSizeSubarray([]int{9,8,7,6,5,4,3,2,1}, 3)) // 1
fmt.Println(minSizeSubarray([]int{1,1,1}, 1000000000)) // 1000000000
fmt.Println(minSizeSubarray1([]int{1,2,3}, 5)) // 2
fmt.Println(minSizeSubarray1([]int{1,1,1,2,3}, 4)) // 2
fmt.Println(minSizeSubarray1([]int{2,4,6,8}, 3)) // -1
fmt.Println(minSizeSubarray1([]int{1,2,3,4,5,6,7,8,9}, 3)) // 1
fmt.Println(minSizeSubarray1([]int{9,8,7,6,5,4,3,2,1}, 3)) // 1
fmt.Println(minSizeSubarray1([]int{1,1,1}, 1000000000)) // 1000000000
fmt.Println(minSizeSubarray2([]int{1,2,3}, 5)) // 2
fmt.Println(minSizeSubarray2([]int{1,1,1,2,3}, 4)) // 2
fmt.Println(minSizeSubarray2([]int{2,4,6,8}, 3)) // -1
fmt.Println(minSizeSubarray2([]int{1,2,3,4,5,6,7,8,9}, 3)) // 1
fmt.Println(minSizeSubarray2([]int{9,8,7,6,5,4,3,2,1}, 3)) // 1
fmt.Println(minSizeSubarray2([]int{1,1,1}, 1000000000)) // 1000000000
}