티스토리 뷰

 

문제는 위와 같으며, 트로피 높이를 배열에 저장한 뒤 앞에서부터 비교하며 보이는 개수를 구하고, 다시 반대로 돌면서 보이는 개수를 구하는 방식으로 문제를 해결할 수 있습니다.

 

파이썬 코드는 다음과 같습니다.

from sys import stdin

n = int(stdin.readline())
trophies = []
for _ in range(n):
    trophies.append(int(stdin.readline()))

left_max = 0
left = 0
for trophy in trophies:
    if left_max < trophy:
        left += 1
        left_max = trophy

right_max = 0
right = 0
for i in range(len(trophies) - 1, -1, -1):
    if right_max < trophies[i]:
        right += 1
        right_max = trophies[i]

print(left)
print(right)

 

자바 코드는 다음과 같습니다.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        int[] trophies = new int[n];
        for (int i = 0; i < trophies.length; i++) {
            trophies[i] = Integer.parseInt(br.readLine());
        }
        
        int leftMax = 0;
        int left = 0;
        for (int i = 0; i < trophies.length; i++) {
            if (leftMax < trophies[i]) {
                left += 1;
                leftMax = trophies[i];
            }
        }
        
        int rightMax = 0;
        int right = 0;
        for (int i = trophies.length - 1; i >= 0; i--) {
            if (rightMax < trophies[i]) {
                right += 1;
                rightMax = trophies[i];
            }
        }
        
        System.out.println(left);
        System.out.println(right);
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함