-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.js
144 lines (129 loc) · 3.17 KB
/
arrays.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
144
//Challenges in the array section
// 2D array hourglass
function hourglassSum(arr) {
let totals = [];
for (let i = 0; i < 4; i++) {
const [L0, L1, L2] = arr;
let temp0 = [...L0];
let temp1 = [...L1];
let temp2 = [...L2];
for (let y = 0; y < 4; y++) {
let [e, r, t] = temp0;
const top = e + r + t;
let [h, j] = temp1;
let [o, p, l] = temp2;
const bottom = o + p + l;
totals.push(top + j + bottom);
temp0.shift();
temp1.shift();
temp2.shift();
}
arr.shift()
}
return Math.max(...totals);
}
// Complete the rotLeft function below.
function rotLeft(a, d) {
for (let i = 0; i < d; i++) {
// remove first element
let first = a.shift();
// push into array
a.push(first);
}
return a;
}
// minnimum brides
// bubble sort
// Complete the minimumBribes function below.
function minimumBribes(q) {
// check whether the result is too chaotic
let swaps = 0;
let swapped = false;
for (let i = 0; i < q.length; i++) {
if (q[i] - (i + 1) > 2 ) {
return "Too chaotic";
}
}
// user bubble sort to track number of swaps
// if left is greater than right swap
// if swap occurs sort again
do{
swapped = false;
q.forEach((num, i) =>{
if (num > q[i + 1]) {
[q[i], q[i + 1]] = [q[i + 1 ], num];
swaps++;
swapped = true;
}
})
}while(swapped){
return swaps;
}
}
// loop through array and swap minVal
// Keep track of sorted and unsorted
// so sort look ahead etc
function minimumSwaps(arr) {
let swapped = 0;
arr.forEach((val, index) => {
let minValue = val;
let minIndex = index;
for (let i = index; i < arr.length; i++) {
if (arr[i] < minValue) {
minValue = arr[i];
minIndex = i;
}
}
if (arr[index] !== arr[minIndex]) {
let num = arr[index];
[arr[index], arr[minIndex]] = [arr[minIndex], num];
swapped++;
}
})
return swapped;
}
// quick sort uses the pivot to split and sort smaller sections
// left is less or equal to pivot, right is more
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
} else {
const pivot = arr.pop();
const length = arr.length;
let left = [];
let right = [];
for (var i = 0; i < length; i++) {
if (arr[i] <= pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
}
// graph theory tree
// how many cycles - 1
function minimumSwaps(arr) {
let counter = 0;
arr.unshift(0);
let length = arr.length;
let checked = new Array(length).fill(false);
let arrayDict = [];
arr.forEach((value, index) => {
return arrayDict[index] = value;
});
arrayDict.forEach((value, index) => {
if (checked[value] || value === index) {
return;
}
let cycle = 0;
while (!checked[value]) {
checked[value] = true;
value = arrayDict[value];
cycle++;
}
counter += cycle - 1;
})
return counter;
}