티스토리 뷰
문제는 위와 같으며, 트로피 높이를 배열에 저장한 뒤 앞에서부터 비교하며 보이는 개수를 구하고, 다시 반대로 돌면서 보이는 개수를 구하는 방식으로 문제를 해결할 수 있습니다.
파이썬 코드는 다음과 같습니다.
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);
}
}
'알고리즘' 카테고리의 다른 글
[알고리즘 / 백준] 1991 - 트리 순회 (0) | 2020.11.13 |
---|---|
[알고리즘 / 백준] 1236 - 성 지키기 (0) | 2020.11.13 |
[알고리즘 / 백준] 1302 - 베스트셀러 (0) | 2020.11.13 |
[알고리즘 / 백준] 1568 - 새 (0) | 2020.11.13 |
[알고리즘 / 백준] 1543 - 문서 검색 (0) | 2020.11.13 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- EC2
- ionic
- permutation
- java
- map
- CodePipeline
- 수학
- 순열
- 소수
- 조합
- DFS
- search
- CodeDeploy
- CodeCommit
- sort
- ECR
- string
- SWIFT
- spring
- Algorithm
- programmers
- 프로그래머스
- AWS
- array
- Combination
- cloudfront
- Baekjoon
- 에라토스테네스의 체
- Dynamic Programming
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함