-
프로그래머스 (Level 2) - 기능개발Coding Test/Coding Test 문제 풀이 2021. 12. 28. 12:01
문제 설명
풀이
문제를 해결하기 위해서 다음과 같이 두 단계로 나누었다.
1. 각 기능이 완료되기까지의 남은 일수를 구한다.
2. 계산한 남은 일수를 통해 배포마다 몇 개의 기능이 배포되는지를 구한다.
첫 번째 단계는 각 기능의 작업 진도와 100과의 차이를 구한 후, 각 기능의 작업 속도로 나머지 연산을 함으로써 쉽게 구할 수 있었다.
두 번째 단계는 List 자료구조를 활용하였으며, 가장 앞에 있는 기능의 남을 일수를 current 변수로 저장하고 배포되는 기능을 세기 위한 변수인 count를 1로 초기화하였다.
이후, 반복문을 통해서 나머지 기능들을 하나씩 탐색하였다.
탐색하고 있는 기능의 남은 일수가 current 변수보다 작을 경우 count 변수를 1씩 증가시키고, 탐색하고 있는 기능의 남은 일수가 current 변수보다 클 경우 List에 count를 삽입함과 동시에 current, count 변수를 갱신해 나가는 방식으로 문제를 해결하였다.
전체 소스코드
import java.io.*; import java.util.*; public class Solution02 { public int[] solution(int[] progresses, int[] speeds) { int[] remains = new int[progresses.length]; for (int i = 0; i < progresses.length; i++) { int target = 100 - progresses[i]; if (target % speeds[i] == 0) remains[i] = target / speeds[i]; else remains[i] = target / speeds[i] + 1; } List<Integer> list = new ArrayList<>(); int count = 1; int current = remains[0]; for (int i = 1; i < remains.length; i++) { if (current < remains[i]) { list.add(count); current = remains[i]; count = 1; } else { count++; } } list.add(count); return list.stream().mapToInt(i -> i.intValue()).toArray(); } }
728x90'Coding Test > Coding Test 문제 풀이' 카테고리의 다른 글
프로그래머스 (Level 2) - 소수 찾기 (0) 2022.01.03 프로그래머스 (Level 2) - 더 맵게 (0) 2021.12.28 프래그래머스 (Level 2) - 전화번호 목록 (0) 2021.12.28 프래그래머스 (Level 1) - 모의고사 (0) 2021.12.27 프로그래머스 (Level 1) - K번째수 (0) 2021.12.27