-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarrays.js
143 lines (138 loc) · 3.51 KB
/
subarrays.js
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
function maxSubarraySum(arr){
let result = arr[0];
let localResult = arr[0];
for(let i = 1; i < arr.length; i ++){
if(localResult > 0){
localResult += arr[i];
}
else{
localResult = arr[i];
}
if(localResult > result){
result = localResult;
}
}
return result;
}
function minSubarraySum(arr){
let result = arr[0];
let localResult = arr[0];
for(let i = 1; i < arr.length; i ++){
if(localResult < 0){
localResult += arr[i];
}
else{
localResult = arr[i];
}
if(localResult < result){
result = localResult;
}
}
return result;
}
function maxSubarrayProd(arr){
let result = arr[0];
let localResultPos = arr[0];
let localResultNeg = arr[0];
for(let i = 1; i < arr.length; i ++){
let localResultMax, localResultMin;
if(localResultPos * arr[i] > localResultNeg * arr[i]){
localResultMax = localResultPos * arr[i];
localResultMin = localResultNeg * arr[i];
}
else{
localResultMax = localResultNeg * arr[i];
localResultMin = localResultPos * arr[i];
}
if(arr[i] > localResultMax){
localResultPos = arr[i];
}
else{
localResultPos = localResultMax;
}
if(arr[i] < localResultMin){
localResultNeg = arr[i];
}
else{
localResultNeg = localResultMin;
}
if(localResultPos > result){
result = localResultPos;
}
}
return result;
}
function minSubarrayProd(arr){
let result = arr[0];
let localResultPos = arr[0];
let localResultNeg = arr[0];
for(let i = 1; i < arr.length; i ++){
let localResultMax, localResultMin;
if(localResultPos * arr[i] > localResultNeg * arr[i]){
localResultMax = localResultPos * arr[i];
localResultMin = localResultNeg * arr[i];
}
else{
localResultMax = localResultNeg * arr[i];
localResultMin = localResultPos * arr[i];
}
if(arr[i] > localResultMax){
localResultPos = arr[i];
}
else{
localResultPos = localResultMax;
}
if(arr[i] < localResultMin){
localResultNeg = arr[i];
}
else{
localResultNeg = localResultMin;
}
if(localResultNeg < result){
result = localResultNeg;
}
}
return result;
}
function maxCircularSubarraySum(arr){
let maxElem = arr[0];
let arrSum = 0;
for(let i = 0; i < arr.length; i ++){
if(arr[i] > maxElem){
maxElem = arr[i];
}
arrSum += arr[i];
}
if(maxElem < 0){
return maxElem;
}
let maxSimpleSum = maxSubarraySum(arr);
let maxCircleSum = arrSum - minSubarraySum(arr);
if(maxSimpleSum > maxCircleSum){
return maxSimpleSum;
}
else{
return maxCircleSum;
}
}
function minCircularSubarraySum(arr){
let minElem = arr[0];
let arrSum = 0;
for(let i = 0; i < arr.length; i ++){
if(arr[i] < minElem){
minElem = arr[i];
}
arrSum += arr[i];
}
if(minElem > 0){
return minElem;
}
let minSimpleSum = minSubarraySum(arr);
let minCircleSum = arrSum - maxSubarraySum(arr);
if(minSimpleSum < minCircleSum){
return minSimpleSum;
}
else{
return minCircleSum;
}
}