-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42587.js
39 lines (37 loc) · 990 Bytes
/
42587.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
function solution(priorities, location) {
var answer = 0;
var count=0;
while(1){
var result = findMax(priorities);
if(priorities[0]==priorities[result]){
count++;
if(location==0)
return count;
priorities.shift();
location--;
}
else{
priorities = [...priorities.slice(result),...priorities.slice(0,result)];
location = location - result;
if(location <0)
location = location +priorities.length;
count++;
if(location ==0)
return count;
priorities.shift();
location--;
}
}
return answer;
}
function findMax(priorities){
var max = priorities[0];
var maxIndex = 0;
for(var i=0; i<priorities.length; i++){
if(priorities[i]>max){
max = priorities[i];
maxIndex = i;
}
}
return maxIndex;
}