[Java] ๋ฐฑ์ค 1725๋ฒ - ํ์คํ ๊ทธ๋จ
https://www.acmicpc.net/problem/1725
----------------------------------------------------------------------------------------------------------------------
[๋ฌธ์ ํ์ด]
๊ฐ๊ฐ์ ์นธ์ ๊ธฐ์ค์ผ๋ก ์์์ผ๋ก ํ์ฅํ์ฌ ์ต๋ ์ง์ฌ๊ฐํ์ ๊ตฌํํ, max๊ฐ์ ๊ณ์ ๊ฐฑ์ ์์ผ์ฃผ๋ ๋ฐฉ๋ฒ ํ์ดํ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, 0๋ฒ~6๋ฒ์ ๋ธ๋ก์ด ์ฃผ์ด์ก์๋, index๋ฒ์งธ ๊ธฐ์ค๋ธ๋ก์ ๋์ด๋ฅผ height, ์ผ์ชฝ์ผ๋ก ์ต๋ํ ํ์ฅํ index๋ฅผ start, ์ค๋ฅธ์ชฝ์ผ๋ก ์ต๋ ํ์ฅํ index๋ฅผ end๋ผ๊ณ ํ๋ค๋ฉด, ๋์ด์์ ((end-start)+1)*height๊ฐ ๋ฉ๋๋ค.
1๋ฒ ๋ธ๋ก ๊ธฐ์ค์ ์ดํด๋ณด๋ฉด ์ผ์ชฝ์ผ๋ก 0๋ฒ๊น์ง, ์ค๋ฅธ์ชฝ์ผ๋ก 6๋ฒ๊น์ง ํ์ฅ์ด ๊ฐ๋ฅํฉ๋๋ค.
๋ฐ๋ผ์ 1๋ฒ ๋ธ๋ก๊ธฐ์ค ์ต๋ ๋์ด๋ ((6-0) + 1 )* 1 = 7
2๋ฒ ๋ธ๋ก ๊ธฐ์ค์ผ๋ก ์ดํด๋ณด๋ฉด ์ผ์ชฝ์ผ๋ก 2๋ฒ๊น์ง, ์ค๋ฅธ์ชฝ์ผ๋ก 3๋ฒ๊น์ง ํ์ฅ์ด ๊ฐ๋ฅํฉ๋๋ค.
๋ฐ๋ผ์ 2๋ฒ ๋ธ๋ก๊ธฐ์ค ์ต๋ ๋์ด๋ ((3-2) + 1 )* 4 = 8
[์ ๋ต ์ฝ๋]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int N;
public static int[] histogram;
public static int max = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
histogram = new int[N];
for(int i=0;i<N;i++) {
histogram[i] = Integer.parseInt(br.readLine());
}
for(int i=0;i<N;i++) {
int area = findArea(i);
if(max < area) {
max = area;
}
}
System.out.println(max);
}
public static int findArea(int index) {
int height = histogram[index];
// index ์ผ์ชฝ์ผ๋ก ํ์ฅํ ์ ์๋ ์ต๋ ๊ธธ์ด ๊ตฌํจ
int start = index;
while(start > 0) {
start--;
if(histogram[start] < height) {
start++;
break;
}
}
// index ์ค๋ฅธ์ชฝ์ผ๋ก ํ์ฅํ ์ ์๋ ์ต๋ ๊ธธ์ด ๊ตฌํจ
int end = index;
while(end < N - 1) {
end++;
if(histogram[end] < height) {
end--;
break;
}
}
return (end - start + 1) * height;
}
}