자바로 백준1449 풀기

hong030·2023년 5월 6일
0
  • 실버 3단계 문제

풀이)

그리디 방식으로 풀어야 한다. 최소한의 테이프를 사용해 물이 새는 것을 막아야 하는데, 범위가 작아 그리디로 배열 정렬을 하며 하나하나 해볼 수 있다.

내 코드)

import java.io.*;
import java.util.*;
public class Main {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] t = br.readLine().split(" ");
		int  N = Integer.parseInt(t[0]);
		int  L = Integer.parseInt(t[1]);
		int pos[] = new int[1001];
		int tape=0;
		String[] input = br.readLine().split(" ");
		for(int i=0; i<N; i++) {
			pos[Integer.parseInt(input[i])] = 1;
		}


		for(int i=1;i<=1000;i++) {
			if(pos[i]!= 0) {
				i+=L-1;
				tape++;
			}
		}

		System.out.println(tape);
	}

}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글