-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbribes2.js
More file actions
67 lines (58 loc) · 1.4 KB
/
Copy pathbribes2.js
File metadata and controls
67 lines (58 loc) · 1.4 KB
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
// https://www.hackerrank.com/challenges/new-year-chaos/problem
const allBribs = []
const allChaos = []
const printedBibs = {}
function minimumBribes(q) {
const n = q.length
let subornos = {}
for (let position = 0; position < n; position++) {
let bribs = 0
const pnumber = q[position]
const actualPosition = position + 1
for (let y = actualPosition; y < n; y++) {
const nnumber = q[y]
if (pnumber > nnumber) {
bribs = bribs + 1
}
}
if(bribs > 0) subornos['n_'+pnumber] = bribs
// console.log(pnumber, bribs)
}
// console.log(subornos)
let isChaotic = false
let minbribs = 0
for (let key in subornos) {
if (subornos[key] >= 2) {
isChaotic = true;
}
minbribs = minbribs + subornos[key]
}
allChaos.push(isChaotic)
allBribs.push(minbribs)
//console.log(minbribs)
//if(isChaotic) console.log('Too chaotic')
//console.log(allBribs)
//console.log(allChaos)
}
minimumBribes([ 2, 1, 5, 3, 4 ])
minimumBribes([2, 5, 1, 3, 4])
let minbrib = 0
let fchaos = false
for (let x = 0; x < allBribs.length; x++) {
const brib = allBribs[x]
if (x === 0) {
minbrib = brib
if (allChaos[x]) {
fchaos = true
}
}
if (brib < minbrib) {
minbrib = brib
if (allChaos[x]) {
fchaos = true
}
// console.log(allChaos[x])
}
}
console.log(minbrib)
if(fchaos) console.log('Too chaotic')