백준 2846 오르막길

전재우·2021년 5월 2일
0

백준 2846 오르막길

구현전 생각

오르막길의 첫째항과 마지막 항을 빼는것은 결국 원소들의 차의 합을 구하는것과 같다

아쉬운 점

엣지 케이스 ~ 범위를 잘 생각하자

코드

package backjoon_4월;

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class backjoon_2846_오르막길 {
	static int N;
	static int height[];
	static int max;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		height = new int[N+1];
		StringTokenizer st = new StringTokenizer(br.readLine()," ");
		for (int i = 0; i < N; i++) {
			height[i]= Integer.parseInt(st.nextToken());
			
		}
		int hill =0;
		int max = 0;
		for (int i = 0; i <= N-1; i++) {
			if(height[i]<height[i+1]) {
				hill+=height[i+1]-height[i];
			}
			if(height[i]>=height[i+1]) {
				max = Math.max(max, hill);
				hill=0;
			}
		}
		
		System.out.println(max);
	}

}
profile
코린이

0개의 댓글