-
Notifications
You must be signed in to change notification settings - Fork 160
/
Length of shortest subarray with sum larger than S.cpp
157 lines (103 loc) · 2.88 KB
/
Length of shortest subarray with sum larger than S.cpp
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
/*
Given an array, find the length of the shortest subarray, the sum of which is greater than or equal to S.
*/
/*
solution1: iterate the end and begin position
O(n^2) time, O(1) space
*/
int FindShortest1(vector<int>& num, int s) {
int sum = 0;
int start = 0, end = 0;
int result = num.size() + 1;
while (end < num.size()) {
if (sum < s) sum += num[end];
while (sum >= s) {
result = min(result, end - start + 1);
sum -= num[start++];
}
end++;
}
return result;
}
/*
solution2: use queue to store the subarray
O(n^2) worst time, O(n) space
*/
int FindShortest2(vector<int>& num,int s) {
int len = num.size();
queue<int> q;
int result = len + 1, sum = 0;
for (int i = 0; i < len; ++i) {
if (q.empty() || sum < s) {
q.push(num[i]);
sum + =num[i];
}
while (!q.empty() && sum >= s) {
result = min(result,(int)q.size());
sum -= q.front();
q.pop();
}
}
return result;
}
/*
solution3: quick partion for the positive elements of array
O(nlogn) time, O(logn) space
*/
#include<iostream>
using namespace std;
int SearchSubarray(int arr[], int len, int k) {
//quicksort partition
if (1 == len || 0 == len) return len;
int i = 1;
int j = 1;
int sum = arr[0];
//the elements from arr[0] to arr[i-1] <=a[0], the elements from a[i] to a[n-1]>a[0]
for (; j < len; ++j) {
if (arr[j] >= arr[0]) {
swap(arr[i], arr[j]);
sum += arr[i++]; //all >= arr[0] element
}
}
swap(arr[0], arr[i-1]);
int partition = i-1;
if (sum >= k && sum - arr[partition] < k) { //arr[0] to a[partition] is the shortest subarray
return partition + 1;
}
if (sum < k) { //search right
return SearchSubarray(arr+partition+1, len-partition-1, k-sum) +partition + 1;
}
//search left
return SearchSubarray(arr, partition, k);
}
int FindShortest3(int arr[], int len, int k) {
if (k < 0) { //k negative is a special case
int maxnum = arr[0];
for (int i = 1; i < len; i++) {
maxnum = max(arr[i], maxnum);
}
return maxnum >= k ? 1 : 0;
}
int i = 0;
int j = len-1;
int sumpos = 0; //sum of all positive elements
//sort a such that negative element comes first
while (i <= j) {
if (arr[i] < 0) {
i++;
} else if (arr[j] >= 0) {
sumpos += arr[j];
j--;
}
else swap(arr[i], arr[j]);
}
if (i >= len || sumpos < k) return 0; //no such subarray
return SearchSubarray(arr+i, len-i, k); //search from positive element
}
int main() {
int arr[] = {1,2,3,4};
int len = sizeof(arr)/sizeof(arr[0]);
int k = 6;
cout<<FindShortest3(arr, len, k)<<endl;
return 0;
}