-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-size-subarray-sum.cs
145 lines (116 loc) · 3.51 KB
/
minimum-size-subarray-sum.cs
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
using System;
using System.Collections.Generic;
public class Solution
{
public static void Main(string[] args)
{
int[] nums = { 2, 3, 1, 2, 4, 3 };
int target = 7;
Console.WriteLine(MinSubArrayLen2(target, nums));
Console.ReadKey();
}
#region Approach1
public static int MinSubArrayLen(int target, int[] nums)
{
if (nums is null || nums.Length is 0 || target is 0)
return 0;
int sum = 0;
int count = 0;
SortedSet<int> lengthOfSubArrays = new SortedSet<int>();
for (int i = 0; i < nums.Length; i++)
{
// the length in this case equals 1
if (nums[i] >= target)
return 1;
sum += nums[i];
count++;
for (int j = i + 1; j < nums.Length && sum < target; j++)
{
sum += nums[j];
count++;
if (sum >= target)
{
lengthOfSubArrays.Add(count);
if (lengthOfSubArrays.Count > 1)
lengthOfSubArrays.Remove(lengthOfSubArrays.Max);
break;
}
}
sum = 0;
count = 0;
}
return lengthOfSubArrays.Count is 0 ? 0 : lengthOfSubArrays.Min;
}
#endregion
#region Approach2
public static int MinSubArrayLen2(int target, int[] nums)
{
if (nums is null || nums.Length is 0 || target is 0)
return 0;
int currentWindowSum = nums[0];
int startWindow = 0;
int minWindowLength = int.MaxValue;
for (int endWindow = 1; endWindow <= nums.Length; endWindow++)
{
// the length in this case equals 1
if (nums[startWindow] >= target)
return 1;
if (endWindow < nums.Length)
currentWindowSum += nums[endWindow];
while (currentWindowSum >= target)
{
minWindowLength = Math.Min(minWindowLength, (endWindow - startWindow + 1));
if (minWindowLength is 1)
return 1;
currentWindowSum -= nums[startWindow];
startWindow++;
}
}
return minWindowLength is int.MaxValue ? 0 : minWindowLength;
}
#endregion
#region Approach3
public static int MinSubArrayLen3(int target, int[] nums)
{
int n = nums.Length;
int[] preSum = _GetThePrefixSumArray(nums, n);
int start = 1, end = n, ans = 0;
while (start <= end)
{
int mid = start + (end - start) / 2;
if (_PerformBinarySearch(mid, nums, target, preSum))
{
ans = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return ans;
}
private static int[] _GetThePrefixSumArray(int[] nums, int n)
{
int[] preSum = new int[n + 1];
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += nums[i];
preSum[i] = sum;
}
return preSum;
}
private static bool _PerformBinarySearch(int mid, int[] nums, int target, int[] presum)
{
if (presum[mid - 1] >= target)
return true;
for (int i = mid; i < nums.Length; i++)
{
if (presum[i] - presum[i - mid] >= target)
return true;
}
return false;
}
#endregion
}