Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 1725๋ฒˆ - ํžˆ์Šคํ† ๊ทธ๋žจ

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 5. 29. 18:20

https://www.acmicpc.net/problem/1725

 

1725๋ฒˆ: ํžˆ์Šคํ† ๊ทธ๋žจ

์ฒซ ํ–‰์—๋Š” N (1 ≤ N ≤ 100,000) ์ด ์ฃผ์–ด์ง„๋‹ค. N์€ ํžˆ์Šคํ† ๊ทธ๋žจ์˜ ๊ฐ€๋กœ ์นธ์˜ ์ˆ˜์ด๋‹ค. ๋‹ค์Œ N ํ–‰์— ๊ฑธ์ณ ๊ฐ ์นธ์˜ ๋†’์ด๊ฐ€ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์นธ์˜ ๋†’์ด๋Š” 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€

www.acmicpc.net

 

----------------------------------------------------------------------------------------------------------------------

[๋ฌธ์ œ ํ’€์ด]

๊ฐ๊ฐ์˜ ์นธ์„ ๊ธฐ์ค€์œผ๋กœ ์–‘์˜†์œผ๋กœ ํ™•์žฅํ•˜์—ฌ ์ตœ๋Œ€ ์ง์‚ฌ๊ฐํ˜•์„ ๊ตฌํ•œํ›„, 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;
    }
}