[BOJ 14719] ๋น๋ฌผ
1. ๋ฌธ์ : Link
์งํ์ ๋์ด๊ฐ ๋ฐฐ์ด๋ก ์ฃผ์ด์ก์๋ ๊ณ ์ธ ๋น๋ฌผ์ ์์ ๊ตฌํ๋ ๋ฌธ์
2. ํ์ด
ํ์ฌ ๋์ด๋ฅผ ์ค์ฌ์ผ๋ก ์์ ์ต๋๋์ด๋ฅผ ํ์ํ๋ ๋ฐฉ๋ฒ
3. ์ฝ๋
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int H = sc.nextInt();
int W = sc.nextInt();
int[] arr = new int[W];
int answer = 0;
for (int i = 0; i < W; i++) {
arr[i] = sc.nextInt();
}
for (int i = 1; i < W - 1; i++) {
int current = arr[i]; //ํ์ฌ ๋ฒฝ ๋์ด
int leftMax = current; //์ผ์ชฝ ๋ฒฝ ์ต๋๋์ด
int rightMax = current; //์ค๋ฅธ์ชฝ ๋ฒฝ ์ต๋๋์ด
for (int k = i - 1; k >= 0; k--) { //์ผ์ชฝ ์ต๋๋ฒฝ ๋์ด ํ์
if (arr[k] > current) {
leftMax = Math.max(leftMax, arr[k]);
}
}
for (int k = i + 1; k < W; k++) { //์ค๋ฅธ์ชฝ ์ต๋๋ฒฝ ๋์ด ํ์
if (arr[k] > current) {
rightMax = Math.max(rightMax, arr[k]);
}
}
if (Math.min(leftMax, rightMax) > current) { // ํ์ฌ ๋ฒฝ๋ณด๋ค ๋์ ๋ฒฝ์ด ์์ชฝ์ ์๋ ๊ฒฝ์ฐ
answer += (Math.min(leftMax, rightMax) - arr[i]);
}
}
System.out.println(answer);
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ (์๋ฐ java) (0) | 2023.05.15 |
[LeetCode] 3Sum (0) | 2023.04.12 |
[BOJ 1806] ๋ถ๋ถํฉ (java) (0) | 2023.04.05 |
[LeetCode] Container With Most Water (0) | 2023.03.28 |
์ต๊ทผ๋๊ธ