-
Notifications
You must be signed in to change notification settings - Fork 0
/
!11501_stock.js
48 lines (37 loc) · 1.11 KB
/
!11501_stock.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
//11501 주식
// 메모리 사용량 실패
let fs = require("fs");
let [n, ...arr] = fs.readFileSync("input.txt").toString().trim().split("\n");
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0) continue;
let list = arr[i].split(" ").map((x) => +x);
let sum = 0;
let index = 0;
while (list.length !== 0) {
const max = Math.max(...list);
index = list.findIndex((item) => item === max);
for (let j = 0; j <= index; j++) {
sum += max - list[j];
}
for (let j = 0; j <= index; j++) {
list.shift();
}
}
console.log(sum);
}
// for (let i = 0; i < arr.length; i++) {
// if (i % 2 === 0) continue;
// let list = arr[i].split(" ").map((x) => +x);
// let sum = 0;
// while (list.length !== 0) {
// const max = Math.max(...list);
// const index = list.findIndex((item) => item === max);
// const sliced = list.slice(0, index + 1);
// const rest = list.slice(index + 1);
// if (sliced.length === 1) break;
// const reduced = sliced.reduce((acc, cur) => acc + max - cur, 0);
// sum += reduced;
// list = rest;
// }
// console.log(sum);
// }