Skip to content

Commit 5c0fd80

Browse files
committed
102차 2번 문제풀이
1 parent 93dca17 commit 5c0fd80

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const filePath =
5+
process.platform === "linux"
6+
? "/dev/stdin"
7+
: path.join(__dirname, "input.txt");
8+
9+
const input = fs
10+
.readFileSync(filePath, "utf8")
11+
.toString()
12+
.trim()
13+
.split("\n")
14+
.map((line) => line.split(" ").map(Number));
15+
16+
function solution(input) {
17+
const N = input[0];
18+
const stocks = [...input[1]];
19+
let max = 0;
20+
let dp = stocks[0]; // 주식의 최소값을 기억
21+
22+
for (let i = 1; i < N; i++) {
23+
if (stocks[i] - dp > 0) max = Math.max(max, stocks[i] - dp);
24+
if (stocks[i] < dp) dp = stocks[i];
25+
}
26+
27+
return max;
28+
}
29+
30+
console.log(solution(input));

0 commit comments

Comments
 (0)