-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_max.sum.js
85 lines (61 loc) · 1.82 KB
/
min_max.sum.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
const arr = [396285104, 573261094, 759641832, 819230764, 364801279];
/*
* Complete the 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function miniMaxSum(array) {
const swap = (arr, left, right) => {
const swapValue = arr[left];
arr[left] = arr[right];
arr[right] = swapValue;
}
const partition = (arr, left, right) => {
const pivot = arr[Math.floor((left + right) / 2)];
let pointLeft = left;
let pointRight = right;
while (pointLeft <= pointRight) {
while(arr[pointLeft] < pivot) {
pointLeft++;
}
while(arr[pointRight] > pivot) {
pointRight--;
}
if (pointLeft <= pointRight) {
swap(arr, pointLeft, pointRight);
pointLeft++;
pointRight--;
}
}
return pointLeft;
}
const quickSort = (arr, left, right) => {
if (arr <= 1) {
return arr;
}
const index = partition(arr, left, right);
console.log("left", arr);
if (index -1 > left) {
quickSort(arr, left, index - 1);
}
if (index < right) {
quickSort(arr, index, right);
}
console.log("right", arr);
return arr;
}
const ordered = quickSort(array, 0, array.length - 1);
console.log(ordered);
const [max, min] = ordered.reduce((acc, a, index) => {
if (index === 0) {
return [a, 0];
}
if (index === ordered.length - 1) {
return [acc[0], acc[1] + a];
}
return [acc[0] + a, acc[1] + a];
}, [0, 0]);
console.log(`${max} ${min}`);
}
// expect 2093989309 2548418794
miniMaxSum(arr);