본문 바로가기

프로그래머스 - JAVA

(24)
코딩테스트 연습 > 힙(Heap) > 더 맵게 기존코드 import java.util.*; class Solution { public int solution(int[] scoville, int K) { int answer = 0; PriorityQueue priorityQueue = new PriorityQueue(); for (int scov : scoville){ priorityQueue.add(scov); } if(priorityQueue.peek() >= K){ return 0; } while (priorityQueue.peek() < K){ if(priorityQueue.size() < 2){ return -1; } answer ++; int scovFirst = priorityQueue.poll(); int scovSecond = prio..
코딩테스트 연습 > 스택/큐 > 주식가격 이전 코드 import java.util.*; class Solution { public int[] solution(int[] prices) { int[] ans = new int[prices.length]; Queue priceListQueue = new LinkedList(); ArrayList ansArrayList = new ArrayList(); for(int i = 0 ; i < prices.length ; i ++){ priceListQueue.add(prices); } for (int i = 0 ; i < prices.length ; i++ ){ int [] priceArray = priceListQueue.poll(); int price = priceArray[i]; int period ..
코딩테스트 연습 > 스택/큐 > 다리를 지나는 트럭 이전코트 import java.util.*; class Solution { public int solution(int bridge_length, int weight, int[] truck_weights) { int answer = 0; // int queueLocation = bridge_length; int weightSum = 0; Queue queue = new LinkedList(); for (int i = 0 ; i < bridge_length ; i++){ queue.offer(0); } int i = 0; while(true){ if(i < truck_weights.length){ if((weightSum + truck_weights[i] - queue.peek()) 0){ weightSum..
코딩테스트 연습 > 스택/큐 > 프린터 import java.util.*; class Solution { public int solution(int[] priorities, int location) { int answer = 0; Queue qu = new LinkedList(); Integer [] prioritiesClone = new Integer[priorities.length]; for (int i = 0 ; i < priorities.length ; i++){ qu.add(new Document(priorities[i], i)); prioritiesClone[i] = priorities[i]; } Arrays.sort(prioritiesClone, Collections.reverseOrder()); for (int i = 0 ; !..
코딩테스트 연습 > 스택/큐 > 기능개발 기존코드 import java.util.*; import java.lang.*; class Solution { public int[] solution(int[] progresses, int[] speeds) { ArrayList ansArrayList = new ArrayList(); Queue qu = new LinkedList(); for(int i = 0 ; i < progresses.length ; i++){ qu.add((int)(Math.ceil((double)(100 - progresses[i]) / speeds[i]))); } int ans = 1; int pastVal = qu.poll(); while(true){ if(qu.peek()
코딩테스트 연습 > 해시 > 베스트앨범 기존 코드 import java.util.*; class Solution { public Integer[] solution(String[] genres, int[] plays) { HashMap hm = new HashMap(); HashMap playsHm = new HashMap(); for(int i = 0 ; i < genres.length ; i++){ playsHm.put(genres[i], playsHm.getOrDefault(genres[i], 0) + plays[i]); } for(int i = 0 ; i < genres.length ; i++){ hm.put(i, new Album(genres[i], plays[i], i, playsHm.get(genres[i]))); } ArrayLi..
코딩테스트 연습 > 해시 > 위장 이전 코드 import java.util.*; class Solution { public int solution(String[][] clothes) { int answer = 1; HashMap hm = new HashMap(); for (String [] item : clothes){ if(hm.get(item[1]) == null){ hm.put(item[1], 1); } else { hm.put(item[1], hm.get(item[1]) + 1); } } for (int val : hm.values()){ answer *= (val+1); } return answer-1; } } 최근 코드 import java.util.*; class Solution { public int solution(Str..
코딩테스트 연습 > 해시 > 전화번호 목록 기존 코드 import java.util.*; class Solution { public boolean solution(String[] phone_book) { HashMap map = new HashMap(); for (String num : phone_book){ if (!map.containsKey(num)){ map.put(num, 0); } else{ return false; } } for (String phone : phone_book){ for (int i = 0 ; i < phone.length() ; i++){ if(map.containsKey(phone.substring(0, i))){ return false; } } } return true; } } 최근 코드 import java.u..