코딩테스트/Java 109

[JAVA] 힙 "더 맵게"

이 문제는 힙을 이용하는 원리인데, JAVA에서는 "우선순위 큐"라는 큐 라이브러리를 이용한다. PriorityQueue() = new PriorityQueue(); 필자는 구글검색을 통해서 PriorityQueue에 대해 학습했는데, 각각의 음식을 객체로 할당하는 방식으로 구현했다. import java.util.PriorityQueue; class Solution { class food implements Comparable{ int scoville; food(int sco){ scoville = sco; } @Override public int compareTo(food target) { return this.scoville >= target.scoville ? 1 : -1; } } public i..

[JAVA] 스택/큐 "프린터"

필자가 작성한 코드는 통과하였지만, 실행시간이 좀 오래걸리는 단점이 있다. import java.util.*; class Solution { class Printing{ int pri, loc; Printing(int priority, int locate){ pri = priority; loc = locate; } } public int solution(int[] priorities, int location) { int answer = 0; Integer[] Priorities = new Integer[priorities.length]; Queue q = new LinkedList(); for(int i =0; i= priority.get(0)){//최고순위인경우 priority.remove(0);//해..

[JAVA] 스택/큐 "다리를 지나는 트럭"

지금까지 코딩방식과는 느낌이 다르게, 내용 그대로 객체를 코드에 반영하는 느낌이다. 필자는 위 방식이 아니라, 다른 수학적으로 계산을 하려다가 실패하여 다른 코드를 리뷰한다. 첫번째, 코드는 Queue에 대기큐와 무브큐를 구현해서, 1. 무브큐가 비어있을때 if 2. 무브큐의 트럭이 다리를 다 건넌걸 판단하는 if 3. 대기큐에 트럭이 있고, 대기큐의 다음 차례트럭 무게+현재 다리의 무게 < 무게 제한 인 경우 if import java.util.*; class Solution { class Truck { int weight; int move; public Truck(int weight) { this.weight = weight; this.move = 1; } public void moving() { m..

[JAVA] 스택/큐 "기능개발"

progresses[] 와 speeds[]에 따른 기능 구현. 핵심은, 앞의 기능이 100이 될때까지 뒤에있는 기능들은 배포가 안되고 대기.] 필자는 테스트케이스11에서 막혀서 다른 코드를 참고하였다. 필자코드 import java.util.Stack; class Solution { public int[] solution(int[] progresses, int[] speeds) { Stack stack = new Stack(); int check = 0; int a=0; while(check < progresses.length) { int count= 0; if(check 100 ){ while(check 100){ count ++; check++; } stack.push(count); a++; } //..

[JAVA] 해시 "위장"

수학적인 규칙을 알아내서 알고리즘을 구하는 방식이다. 위 문제에 따르면 각각의 종류에 따라서 최소 1가지의 종류를 이용하여 옷을 입어야 한다. 종류 A B C 옷의 가지수(개) a b c 위와 같다면, 총 나올수 있는 옷의 조합은 (각 옷을 안입는경우수 포함)곱의법칙 - (아무것도 안입는 경우) = (a+1)*(b+1)*(c+1) -1 이 계산식을 이용하여 값을 구하면 된다. 필자의 코드는 아래와 같다. import java.util.*; class Solution { public int solution(String[][] clothes) { int answer = 0; HashMap hm = new HashMap(); for (int i = 0; i < clothes.length; i++) { hm.p..

[JAVA] 해시 "베스트앨범"

내가 작성한 코드 : 시간초과. import static java.util.stream.Collectors.*; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class CodingTestExample이니까다하고삭제 { public static int findMaxMusic(String[] genres, int[] plays , String max_key){ int maxOftheGenre = 0; int maxi = 0; for(int i = 0; i maxOftheGenre){ maxOftheGenre = plays[i]; maxi = i; } } return maxi; } public int[] solu..