https://www.acmicpc.net/problem/2846
2846๋ฒ: ์ค๋ฅด๋ง๊ธธ
๋ฌธ์ ์๊ทผ์ด๋ ์์ ๊ฑฐ๋ฅผ ํ๊ณ ๋ฑ๊ตํ๋ค. ์์ ๊ฑฐ ๊ธธ์ ์ค๋ฅด๋ง๊ธธ, ๋ด๋ฆฌ๋ง๊ธธ, ํ์ง๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ์๊ทผ์ด๋ ๊ฐ๊ฐ ์ฒซ ๋ ์์ ๊ฑฐ๋ฅผ ํ๊ณ ๊ฐ๋ฉด์ ์ผ์ ๊ฑฐ๋ฆฌ๋ง๋ค ๋์ด๋ฅผ ์ธก์ ํ๋ค. ์๊ทผ์ด๋ ๊ฐ์ฅ ํฐ ์ค๋ฅด๋ง๊ธธ์ ํฌ๊ธฐ๋ฅผ ๊ตฌํ๋ ค๊ณ ํ๋ค. ์ธก์ ํ ๋์ด๋ ๊ธธ์ด๊ฐ N์ธ ์์ด๋ก ๋ํ๋ผ ์ ์๋ค. ์ฌ๊ธฐ์ ์ค๋ฅด๋ง๊ธธ์ ์ ์ด๋ 2๊ฐ์ ์๋ก ์ด๋ฃจ์ด์ง ๋์ด๊ฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด์ด๋ค. ์ค๋ฅด๋ง๊ธธ์ ํฌ๊ธฐ๋ ๋ถ๋ถ ์์ด์ ์ฒซ ๋ฒ์งธ ์ซ์์ ๋ง์ง๋ง ์ซ์์ ์ฐจ์ด์ด๋ค. ์๋ฅผ ๋ค์ด, ๋์ด๊ฐ ๋ค์๊ณผ ๊ฐ์
www.acmicpc.net
์ฝ๋
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int before = 0;
int current = scan.nextInt();
int sum = 0;
int result = 0;
for(int i=1; i<N; i++) {
before = current;
current = scan.nextInt();
if(current - before > 0) // ์ฆ๊ฐ
sum += current - before;
else {
result = Math.max(result, sum);
sum = 0;
}
}
result = Math.max(result, sum);
System.out.println(result);
scan.close();
}
}
ํ์ด
์ฒ์์ ์ฆ๊ฐํ๋ ์์ด์ ์ฒซ๋ฒ์งธ์ ๋์ ์ฐพ์ ํ๋ฒ์ ํด๊ฒฐํ๋ ค๊ณ ํ๋ค.
๊ฒฐ๊ตญ ํ์ง๋ชปํ๊ณ ๋ค๋ฅธ ํ์ด๋ฅผ ์ฐพ์๋ดค๋ค..
๊ฐ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ๊ณ , ์์์๋ณด๋ค ํด๋(์ฆ๊ฐํ๋ ์์ด์ผ๋) ์ฐจ์ด๋ฅผ ๋ํด์ค๋ค.
๋ง์ฝ ์ฆ๊ฐํ๋ ์์ด์ด ๋๋ฌ์๋(๋ค์ ์ซ์๊ฐ ๋ ํด๋) ๋ ํฐ ์ซ์๋ฅผ ์ฐพ๋๋ค.
12 20 32 1 3 4 4 11 1 |
๋ง์ฝ ์ฃผ์ด์ง ์ซ์๊ฐ ์์ ๊ฐ์๋,
ํ๊ฐ์ฉ ์ ๋ ฅ๋ฐ์ผ๋ฉฐ ์ฆ๊ฐํ๋ ์์ด์ผ๋(12-20-32)
๊ฐ ์ฐจ์ด๋ฅผ ๋นผ์ ๋ํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ ์ฆ๊ฐํ๋ ์์ด์ด ๋๋ฌ์ ๋(32-1)result, sum ๊ฐ์ ๋น๊ตํ๋ค.
๊ทธ ํ ๋ค์ ์ฆ๊ฐํ๋ ์์ด(1-3-4, 4-11) ์ผ๋๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ค์์ซ์์ ์์ ์ซ์์ ์ฐจ์ด๋ฅผ ๋ํํ
result, sum ๊ฐ์ ๋น๊ตํ๋ค.
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2851๋ฒ: ์ํผ ๋ง๋ฆฌ์ค (0) | 2020.01.18 |
---|---|
[๋ฐฑ์ค] 1977๋ฒ: ์์ ์ ๊ณฑ์ (0) | 2020.01.18 |
[๋ฐฑ์ค] 14697๋ฒ: ๋ฐฉ ๋ฐฐ์ ํ๊ธฐ (0) | 2020.01.17 |
[๋ฐฑ์ค] 15953๋ฒ: ์๊ธ ํํฐ (0) | 2020.01.17 |
[๋ฐฑ์ค] 2446๋ฒ: ๋ณ ์ฐ๊ธฐ - 9 (0) | 2020.01.16 |
๋๊ธ