본문 바로가기

알고리즘15

[프로그래머스] (스택/큐) 주식가격 다른 것보다 테스트케이스가 딱 1개뿐이라 검증에 애를 먹은 문제이다. 테스트 케이스를 추가하여 연습해보는 것을 추천한다. prices return [1, 2, 3, 2, 3] [4, 3, 1, 1, 0] [3, 1, 2, 2, 3, 1] [1, 4, 3, 2, 1, 0] [1, 2, 3, 1, 2, 3, 3, 1, 2] [8, 2, 1, 5, 3, 2, 1, 1, 0] class Solution { public int[] solution(int[] prices) { int[] answer = new int[prices.length]; for (int i = 0; i < prices.length -1; i++) { int nowPrice = prices[i]; int cnt = 0; for (int j =.. 2021. 3. 22.
[프로그래머스] (정렬) K번째 수 핵심은 Arrays.copyOfRange 를 사용하는 것. 제출후에 다시보니 쓸데 없이 List를 사용한 것 같음. 괜히 List를 Array로 변환하는 작업만 추가됨. import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; List ans = new ArrayList(); for (int[] command : commands) { int[] tmp = Arrays.copyOfRange(array, command[0]-1, command[1]); Arrays.sort(tmp); int t = tmp[command[2]-1];.. 2021. 3. 22.
[프로그래머스] (스택/큐) 다리를 지나는 트럭 스택과 큐를 사용해서 풀어야 하는 문제지만 ArrayList를 사용하여 다리위 상태를 구현하고, 트럭이 올라가고 내려가는 것을 계산하여 알맞게 loadTruck, unloadTruck 을 호출한다. 다리 위(BRIDGE class) 의 총 무게가 하중을 견딜수 있을만큼만 올려놓고 올려진 트럭이 탄 시간 + 다리위에 있는 시간을 계산하여 다리에서 내린다. 아직 타지 않은 트럭이 없으면 더 이상 loadTruck을 할 필요는 없어지고 최초상태를 제외하고 다리 위에 트럭의 무게가 0이라면 루프를 빠져나온다. import java.util.*; import java.util.stream.Collectors; class Solution { public int solution(int bridge_length, in.. 2021. 3. 22.
반응형