본문 바로가기

Algorithm

(138)
[프로그래머스_JAVA_알고리즘] 디스크 컨트롤러 #힙 #우선순위 큐 PriorityQueue를 오름차순 정렬 후, 작업 시간이 짧은 순서대로 저장하는 로직을 구현합니다. import java.util.*;class Solution { public int solution(int[][] jobs) { int answer = 0; int endTime = 0; // 오름차순 정렬 Arrays.sort(jobs, (a, b) -> a[0] - b[0]); PriorityQueue priorityQueue = new PriorityQueue((a, b) -> a[1] - b[1]); // 작업의 소요시간이 짧은 것, 작업의 요청 시각이 빠른 것..
[백준_JAVA_알고리즘] 11279 최대 힙 #힙 #우선순위 큐 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Collections;import java.util.PriorityQueue;public class Main { public static void main(String[] args) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bufferedReader.readLine()); // 큰 숫..
[프로그래머스_JAVA_알고리즘] 더 맵게 #힙 #우선순위 큐 힙 문제는 우선순위 큐 Priority Queue를 이용해서 풀 수 있습니다.import java.util.*;class Solution { public int solution(int[] scoville, int K) { int answer = 0; PriorityQueue priorityQueue = new PriorityQueue(); for(int s: scoville) priorityQueue.add(s); int num = priorityQueue.peek(); while(num = 2){ priorityQueue.add(priorityQueue.poll..
[백준_JAVA_알고리즘] 11286 절댓값 힙 #힙 #우선순위 큐 힙 문제는 우선순위 큐를 이용해서 해결할 수 있습니다.import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Comparator;import java.util.PriorityQueue;public class Main { public static void main(String[] args) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bufferedRead..
[알고리즘] 우선순위 큐, 힙 우선순위 큐는 들어오는 순서에 상관없이 우선순위가 높은 데이터가 먼저 나가는 구조이다.우선순위 큐는 힙을 이용해서 구현한다.PriorityQueue를 이용해서 우선순위 큐를 구현하며, 우선순위 큐에 저장할 객체는 필수적으로 Comparable Interface를 구현해야 한다. Comparable Interface를 구현하면 compareTo 메서드를 오버라이드 하게 되고, 해당 객체에서 처리할 우선순위 조건을 리턴해주면 PriorityQueue가 알아서 우선순위가 높은 객체를 추출해준다. PriorityQueue 선언 방법// 우선순위가 낮은 숫자가 먼저 나옴PriorityQueue priorityQueue = new PriorityQueue();// 우선순위가 높은 숫자가 먼저 나옴PriorityQu..
[프로그래머스_JAVA_알고리즘] 의상 #해시 HashMap을 이용해서 문제를 해결할 수 있습니다.import java.util.*;class Solution { public int solution(String[][] clothes) { HashMap hashmap = new HashMap(); for(String[] cloth : clothes){ String category = cloth[1]; // 의상의 종류를 추출 hashmap.put(category, hashmap.getOrDefault(category, 0) + 1); } int answer = 1; for(int count : hashmap.value..
[프로그래머스_JAVA_알고리즘] 문자열 정렬하기 (2) #정렬 Arrays.sort()를 이용해서 문제를 해결할 수 있습니다.import java.util.*;class Solution { public String solution(String my_string) { my_string = my_string.toLowerCase(); char[] chArr = my_string.toCharArray(); Arrays.sort(chArr); return new String(chArr); }} Arrays.sort() 메서드는 배열 타입만 받기 때문에 문자열을 바로 정렬할 수 없습니다. 예를 들어, String은 하나의 문자열 객체이기 때문에 Arrays.sort()에 바로 전달하면 오류가 발생합니다. 이를 해결..
[프로그래머스_JAVA_알고리즘] H-Index #정렬 Arrays.sort를 이용해서 정렬 후 최대값을 리턴할 수 있습니다.import java.util.*;class Solution { public int solution(int[] citations) { int answer = 0; Arrays.sort(citations); // 인용 수를 오름차순 정렬 for(int i = 0; i = h){ // 조건 확인: 인용 수가 h 이상인 논문이 h편 이상 answer = h; break; } } return answer; }} citations를 정렬하면 [0, 1, 3, 5, 6]이 됩니다.해당 논..