diff --git "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.md" "b/Programmers/JH/Hash/[\354\240\225\355\230\204]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.md" deleted file mode 100644 index c120e90..0000000 --- "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.md" +++ /dev/null @@ -1,73 +0,0 @@ -[베스트앨범](https://programmers.co.kr/learn/courses/30/lessons/42579) - -```java -import java.util.*; -class Solution { - public Integer[] solution(String[] genres, int[] plays) { - ArrayList answer = new ArrayList<>(); - - HashMap> genreMap = new HashMap<>(); - for (int i=0; i songList; - - if (genreMap.containsKey(genreName)) - songList = genreMap.get(genreName); - else - songList = new ArrayList<>(); - songList.add(new Song(i, plays[i])); - genreMap.put(genreName, songList); - } - - ArrayList genreList = new ArrayList<>(); - for (String genreName : genreMap.keySet()) { - int totalPlays = 0; - for (Song song : genreMap.get(genreName)) - totalPlays += song.plays; - - genreList.add(new Genre(genreName, totalPlays)); - genreMap.get(genreName).sort(Collections.reverseOrder()); - } - - genreList.sort(Collections.reverseOrder()); - for (Genre genre : genreList) { - ArrayList songList = genreMap.get(genre.genreName); - for (int i = 0; i < 2; i++) { - if (i { - int songID; - int plays; - - public Song(int songID, int plays) { - this.songID = songID; - this.plays = plays; - } - - @Override - public int compareTo(Song o) { - return plays - o.plays; - } -} - -class Genre implements Comparable { - String genreName; - int totalPlays; - - public Genre(String genreName, int totalPlays) { - this.genreName = genreName; - this.totalPlays = totalPlays; - } - - @Override - public int compareTo(Genre o) { - return totalPlays - o.totalPlays; - } -} -``` diff --git "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" "b/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" deleted file mode 100644 index 4a3c9ea..0000000 --- "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" +++ /dev/null @@ -1,31 +0,0 @@ -[완주하지 못한 선수](https://programmers.co.kr/learn/courses/30/lessons/42576) - -```java -import java.io.*; -import java.util.*; - -class Solution { - public String solution(String[] participant, String[] completion) { - String answer = ""; - Map participantMap = new HashMap<>(); - - for (String p : participant) { - if (participantMap.containsKey(p)) - participantMap.put(p, participantMap.get(p)+1); - else - participantMap.put(p, 1); - } - - for (String c : completion) { - if (participantMap.get(c)==1) - participantMap.remove(c); - else - participantMap.put(c, participantMap.get(c)-1); - } - - for (String name : participantMap.keySet()) - return name; - return answer; - } -} -``` \ No newline at end of file diff --git "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\234\204\354\236\245.md" "b/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\234\204\354\236\245.md" deleted file mode 100644 index 0af02c9..0000000 --- "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\234\204\354\236\245.md" +++ /dev/null @@ -1,18 +0,0 @@ -[위장](https://programmers.co.kr/learn/courses/30/lessons/42578) -```java -class Solution { - public int solution(String[][] clothes) { - int answer = 1; - HashMap hashMap = new HashMap<>(); - - for (String[] clothPair : clothes) - hashMap.put(clothPair[1], hashMap.getOrDefault(clothPair[1],0)+1); - - Collection values = hashMap.values(); - for (int value : values) { - answer *= value+1; - } - return answer-1; - } -} -``` \ No newline at end of file diff --git "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" "b/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" deleted file mode 100644 index 6c3c3c1..0000000 --- "a/Programmers/JH/Hash/[\354\240\225\355\230\204]\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" +++ /dev/null @@ -1,21 +0,0 @@ -[전화번호 목록](https://programmers.co.kr/learn/courses/30/lessons/42577#) -```java -import java.util.*; - -class Solution { - public boolean solution(String[] phoneBook) { - Arrays.sort(phoneBook, (s1, s2) -> s1.length() - s2.length()); - HashSet set = new HashSet<>(); - - for (String phoneNumber : phoneBook) { - for (int i=1; i answer = new ArrayList<>(); - int[] requiredDay = new int[progresses.length]; - - for (int i = 0; i 0) - requiredDay[i]++; - } - - int previousDay = requiredDay[0]; - int num = 0; - for (int day : requiredDay) { - if (previousDay >= day) - num++; - else { - answer.add(num); - num = 1; - previousDay = day; - } - } - - answer.add(num); - return answer.toArray(new Integer[0]); - } -} -``` \ No newline at end of file diff --git "a/Programmers/JH/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" "b/Programmers/JH/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" deleted file mode 100644 index a213c6b..0000000 --- "a/Programmers/JH/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" +++ /dev/null @@ -1,58 +0,0 @@ -[프린터](https://programmers.co.kr/learn/courses/30/lessons/42587) - -```java -class Document implements Comparable { - - private int location; - private int priority; - - public Document(int location, int priority) { - this.location = location; - this.priority = priority; - } - - @Override - public int compareTo(Integer o) { - return priority - o; - } - - public int getLocation() { - return location; - } - - public int getPriority() { - return priority; - } -} - -class Solution { - public int solution(int[] priorities, int location) { - int answer = 1; - - PriorityQueue maxHeap = new PriorityQueue<>( - Collections.reverseOrder()); - - Queue printerQ = new LinkedList<>(); - for (int i=0; i { - Integer number; - Integer plays; - - public Song(Integer number, Integer plays){ - this.number = number; - this.plays = plays; - } - - @Override - public int compareTo(Song o) { - if(plays != o.plays){ - return o.plays - plays; - } - return number - o.number; - } -} - -class Solution { - public int[] solution(String[] genres, int[] plays) { - int[] answer = {}; - ArrayList answerList = new ArrayList<>(); - HashMap sumOfPlaysByGenre = new HashMap<>(); - HashMap> songsByGenre = new HashMap<>(); - - for(int i = 0; i < genres.length; i++){ - int sumOfPlays = sumOfPlaysByGenre.getOrDefault(genres[i], 0); - sumOfPlaysByGenre.put(genres[i], sumOfPlays + plays[i]); - - if(!songsByGenre.containsKey(genres[i])){ - songsByGenre.put(genres[i], new ArrayList()); - } - songsByGenre.get(genres[i]).add(new Song(i, plays[i])); - } - - ArrayList> entryListOfSumOfPlaysByGenre = new ArrayList<>(sumOfPlaysByGenre.entrySet()); - Collections.sort(entryListOfSumOfPlaysByGenre, (ob1, ob2) -> ob2.getValue() - ob1.getValue()); - - for(Map.Entry> entry : songsByGenre.entrySet()){ - Collections.sort(entry.getValue()); - } - - for(Map.Entry entry : entryListOfSumOfPlaysByGenre){ - String genre = entry.getKey(); - answerList.add(songsByGenre.get(genre).get(0).number); - if(songsByGenre.get(genre).size() >= 2){ - answerList.add(songsByGenre.get(genre).get(1).number); - } - } - - answer = new int[answerList.size()]; - for(int i = 0; i < answerList.size(); i++){ - answer[i] = answerList.get(i); - } - - return answer; - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(NlogN) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Juwon/Hash/\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" "b/Programmers/Juwon/Hash/\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" deleted file mode 100644 index d03ee35..0000000 --- "a/Programmers/Juwon/Hash/\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.md" +++ /dev/null @@ -1,40 +0,0 @@ -[완주하지 못한 선수](https://programmers.co.kr/learn/courses/30/lessons/42576) - -```java -import java.io.*; -import java.util.*; - -class Solution { - public String solution(String[] participants, String[] completion) { - String answer = ""; - HashMap participantCount = new HashMap<>(); - - for(String participant : participants){ - int count = participantCount.getOrDefault(participant, 0); - participantCount.put(participant, ++count); - } - - for(String finisher : completion){ - int count = participantCount.get(finisher); - participantCount.put(finisher, --count); - } - - for(Map.Entry entry : participantCount.entrySet()){ - if(entry.getValue() != 0) { - answer = entry.getKey(); - break; - } - } - - return answer; - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(N) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Juwon/Hash/\354\234\204\354\236\245.md" "b/Programmers/Juwon/Hash/\354\234\204\354\236\245.md" deleted file mode 100644 index 475080b..0000000 --- "a/Programmers/Juwon/Hash/\354\234\204\354\236\245.md" +++ /dev/null @@ -1,32 +0,0 @@ -[위장](https://programmers.co.kr/learn/courses/30/lessons/42578) - -```java -import java.io.*; -import java.util.*; - -class Solution { - public int solution(String[][] clothes) { - int answer = 1; - HashMap countOfClothe = new HashMap<>(); - - for(String[] clothe : clothes){ - String clotheType = clothe[1]; - countOfClothe.put(clotheType, countOfClothe.getOrDefault(clotheType, 0) + 1); - } - - for(String clotheType : countOfClothe.keySet()){ - answer *= (countOfClothe.get(clotheType) + 1); - } - - return --answer; - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(N) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Juwon/Hash/\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" "b/Programmers/Juwon/Hash/\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" deleted file mode 100644 index 1f29674..0000000 --- "a/Programmers/Juwon/Hash/\354\240\204\355\231\224\353\262\210\355\230\270 \353\252\251\353\241\235.md" +++ /dev/null @@ -1,39 +0,0 @@ -[전화번호 목록](https://programmers.co.kr/learn/courses/30/lessons/42577?language=java) - -```java -import java.io.*; -import java.util.*; - -class Solution { - public boolean solution(String[] phone_book) { - boolean answer = true; - HashSet saveSet = new HashSet<>(); - - Arrays.sort(phone_book, (s1, s2) -> s1.length() - s2.length()); - - for(String phone : phone_book){ - String partOfPhone = new String(); - - for(int i = 0; i < phone.length(); i++){ - partOfPhone += phone.charAt(i); - - if(saveSet.contains(partOfPhone)){ - return answer = false; - } - } - - saveSet.add(phone); - } - - return answer; - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(NlogN) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Juwon/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234.md" "b/Programmers/Juwon/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234.md" deleted file mode 100644 index 6c7a7cc..0000000 --- "a/Programmers/Juwon/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234.md" +++ /dev/null @@ -1,45 +0,0 @@ -[기능개발](https://programmers.co.kr/learn/courses/30/lessons/42586?language=java) - -```java -import java.io.*; -import java.util.*; - -class Solution { - public Integer[] solution(int[] progresses, int[] speeds) { - List answer = new ArrayList(); - List completedDays = new ArrayList(); - - for(int i = 0; i < progresses.length; i++){ - int completedDay = (100 - progresses[i]) / speeds[i]; - if((100 - progresses[i]) % speeds[i] != 0){ - ++completedDay; - } - completedDays.add(completedDay); - } - - int maxCompletedDay = completedDays.get(0); - int numOfCompleted = 0; - for(int completedDay : completedDays){ - if(completedDay > maxCompletedDay){ - answer.add(numOfCompleted); - maxCompletedDay = completedDay; - numOfCompleted = 1; - } - else{ - ++numOfCompleted; - } - } - answer.add(numOfCompleted); - - return answer.toArray(new Integer[0]); - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(N) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Juwon/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" "b/Programmers/Juwon/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" deleted file mode 100644 index 7615180..0000000 --- "a/Programmers/Juwon/Stack&Queue/\355\224\204\353\246\260\355\204\260.md" +++ /dev/null @@ -1,67 +0,0 @@ -[프린터](https://programmers.co.kr/learn/courses/30/lessons/42587?language=java) - -```java -import java.io.*; -import java.util.*; -import java.util.stream.Collectors; - -class Solution { - public Integer solution(int[] priorities, int location) { - Integer answer = 0; - List priorityList = Arrays.stream(priorities) - .boxed() - .collect(Collectors.toList()); - List documentList = priorityList.stream() - .map(Document::new) - .collect(Collectors.toList()); - documentList.get(location).setTarget(true); - Queue waitingQueue = new LinkedList(documentList); - - Collections.sort(priorityList, Collections.reverseOrder()); - Queue maxQueue = new LinkedList(priorityList); - - while (true){ - Document now = waitingQueue.poll(); - if(now.getPriority() == maxQueue.peek()){ - ++answer; - maxQueue.remove(); - if(now.isTarget()) break; - } - else{ - waitingQueue.add(now); - } - } - - return answer; - } -} - -class Document{ - private Integer priority; - private boolean target = false; - - Document(Integer a){ - priority = a; - } - - public Integer getPriority() { - return priority; - } - - public boolean isTarget() { - return target; - } - - public void setTarget(boolean target) { - this.target = target; - } -} -``` - - -### 모범 답안 -- 시간복잡도 - - O(NlogN) -- 공간복잡도 - - O(N) - diff --git "a/Programmers/Youngmin/Hash/MY-\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.js" "b/Programmers/Youngmin/Hash/MY-\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.js" new file mode 100644 index 0000000..4f62474 --- /dev/null +++ "b/Programmers/Youngmin/Hash/MY-\354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230.js" @@ -0,0 +1,21 @@ +function solution(participant, completion) { + participant.sort(); + completion.sort(); + + for (let i = 0; i < completion.length; i++) { + if (completion[i] !== participant[i]) { + return participant[i]; + } + } +} + +// Test Case +// console.log(solution(["leo", "kiki", "eden"], ["eden", "kiki"])); // "leo" +// console.log(solution(["marina", "josipa", "nikola", "vinko", "filipa"], ["josipa", "filipa", "marina", "nikola"])); // "vinko" +// console.log(solution(["mislav", "stanko", "mislav", "ana"], ["stanko", "ana", "mislav"])); // "mislav" + +/** + * 문제 : 프로그래머스 - 완주하지 못한 선수 + * 테마 : 해시 + * 출처 : https://programmers.co.kr/learn/courses/30/lessons/42576 + */ diff --git "a/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/MY-\352\270\260\353\212\245\352\260\234\353\260\234.js" "b/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/MY-\352\270\260\353\212\245\352\260\234\353\260\234.js" new file mode 100644 index 0000000..e680dbd --- /dev/null +++ "b/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/MY-\352\270\260\353\212\245\352\260\234\353\260\234.js" @@ -0,0 +1,41 @@ +function solution(progresses, speeds) { + const result = []; + const maxProgress = 100; + + let currentIdx = 0; + while (currentIdx < progresses.length) { + if (progresses[currentIdx] < maxProgress) { + for (let i = currentIdx; i < progresses.length; i++) { + progresses[i] = progresses[i] + speeds[i]; + } + } + + if (progresses[currentIdx] >= maxProgress) { + result.push(0); + while (progresses[currentIdx] >= maxProgress) { + result[result.length - 1] = result[result.length - 1] + 1; + currentIdx = currentIdx + 1; + } + } + } + + return result; +} + +// Test Case +console.log(solution([93, 30, 55], [1, 30, 5])); // [2, 1] +console.log(solution([95, 90, 99, 99, 80, 99], [1, 1, 1, 1, 1, 1])); // [1, 3, 2] + +/** + * 문제: 기능개발 - 프로그래머스 + * 테마: 스택 / 큐 + * 출처: https://programmers.co.kr/learn/courses/30/lessons/42586 + * + * [내 풀이] + * - 현재 인덱스를 가리키는 인덱스 변수를 사용 ( 최대 progresses 길이 ) + * - progresses 배열의 첫 번째 작업을 보면서 100% 작업이 완료되는 시점 이전까지는 매 차례 배열 요소들을 각 speed 만큼 증가 + * - progresses 배열의 첫 번째 작업이 100% 가 되는 시점마다 뒷 작업이지만 100% 가 된 작업 수 만큼 해당 step 의 완료 작업 수 증가 + * - step 이 끝나기 전에, 아직 작업이 끝나지 않은 작업의 인덱스로 설정 + * - 실행시간 O(N^2) + * + */ diff --git "a/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/\352\270\260\353\212\245\352\260\234\353\260\234.js" "b/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/\352\270\260\353\212\245\352\260\234\353\260\234.js" new file mode 100644 index 0000000..9555251 --- /dev/null +++ "b/Programmers/Youngmin/Stack&Queue/\352\270\260\353\212\245\352\260\234\353\260\234/\352\270\260\353\212\245\352\260\234\353\260\234.js" @@ -0,0 +1,31 @@ +function solution(progresses, speeds) { + let result = [0]; + let restDays = progresses.map((progress, idx) => Math.ceil((100 - progress) / speeds[idx])); + let currentMaxRestDay = restDays[0]; + + for (let i = 0, j = 0; i < restDays.length; i++) { + if (restDays[i] <= currentMaxRestDay) { + result[j] = result[j] + 1; + } else { + currentMaxRestDay = restDays[i]; + + j = j + 1; + result[j] = 1; + } + } + + return result; +} + +// Test Case +console.log(solution([93, 30, 55], [1, 30, 5])); // [2, 1] +console.log(solution([95, 90, 99, 99, 80, 99], [1, 1, 1, 1, 1, 1])); // [1, 3, 2] + +/** + * [더 좋은 풀이] + * - 각 작업이 완료되기 위한 잔여 작업일 = Math.ceil((100 - 현재 작업 진행률) / 현재 작업의 작업 속도) 배열을 구한다. + * - 비교 대상인 현재 최대 잔여 작업일을 첫 번째 작업의 잔여일로 설정 + * - 배열을 순회하면서 다음 작업을 진행 + * - 현재 최대 잔여 작업일 보다 작거나 같으면, 누적 완료 작업 수 증가 + * - 현재 최대 잔여 작업일 보다 크면, 최대 잔여 작업일을 현재 순회 중인 작업의 잔여일로 업데이트 후, 누적 완료 작업 배열의 인덱스 증가 + */ diff --git "a/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/MY-\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" "b/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/MY-\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" new file mode 100644 index 0000000..2d43224 --- /dev/null +++ "b/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/MY-\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" @@ -0,0 +1,47 @@ +function solution(bridge_length, weight, truck_weights) { + const trucks = truck_weights.map((truckWeight) => ({ truckWeight: truckWeight, currentBridgeLength: 0 })); + const trucksOnTheBridge = []; + const passedTrucks = []; + + let result = 0; + while (passedTrucks.length < truck_weights.length) { + result = result + 1; + + if (trucksOnTheBridge.length !== 0 && trucksOnTheBridge[0].currentBridgeLength >= bridge_length) { + passedTrucks.push(trucksOnTheBridge.shift()); + } + + const sumTrucksWeightsOnTheBridge = trucksOnTheBridge.reduce((acc, cur) => (acc += cur.truckWeight), 0); + + if (trucks.length > 0 && sumTrucksWeightsOnTheBridge + trucks[0].truckWeight <= weight) { + trucksOnTheBridge.push(trucks.shift()); + } + + trucksOnTheBridge.forEach((truck) => { + truck.currentBridgeLength = truck.currentBridgeLength + 1; + }); + } + + return result; +} + +// Test Case +// console.log(solution(2, 10, [7, 4, 5, 6])); // 8 +// console.log(solution(100, 100, [10])); // 101 +// console.log(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])); // 110 + +/** + * 문제: 다리를 지나는 트럭 - 프로그래머스 + * 테마: 스택 / 큐 + * 출처: https://programmers.co.kr/learn/courses/30/lessons/42583 + * + * [내 풀이] + * + 다리를 지난 트럭 수 === 다리를 지나기 전 트럭 수일 때까지 반복 + * - 시간을 +1 증감 + * - 현재 시간 기준으로, 다리를 지나게 되는 다리 위에 트럭이 있다면(= 현재 다리 위에 가장 앞에 있는 트럭) 내보내기 + * - 현재 다리 위에 트럭 무게 합을 구하기 + * - 현재 다리 위에 트럭 무게 합 + 아직 다리를 오르지 못한 트럭 중 가장 앞 트럭의 무게 <= 다리가 견딜 수 있는 최대 무게인지 확인 + * - 조건에 만족한다면, 다리 위에 트럭을 올리기 + * - 다리 위에 트럭들 전진 + * + 실행시간: O(N^2) = bridge_length(최대 = 10,000) * truck_weights 배열 길이(최대 = 10,000) + */ diff --git "a/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" "b/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" new file mode 100644 index 0000000..b26786f --- /dev/null +++ "b/Programmers/Youngmin/Stack&Queue/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/\353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255.js" @@ -0,0 +1,38 @@ +function solution(bridge_length, weight, truck_weights) { + // '다리'를 모방한 큐에 간단한 배열로 정리 : [트럭무게, 얘가 나갈 시간]. + let time = 0, + qu = [[0, 0]], + weightOnBridge = 0; + + // 대기 트럭, 다리를 건너는 트럭이 모두 0일 때 까지 다음 루프 반복 + while (qu.length > 0 || truck_weights.length > 0) { + // 1. 현재 시간이, 큐 맨 앞의 차의 '나갈 시간'과 같다면 내보내주고, + // 다리 위 트럭 무게 합에서 빼준다. + if (qu[0][1] === time) weightOnBridge -= qu.shift()[0]; + + if (weightOnBridge + truck_weights[0] <= weight) { + // 2. 다리 위 트럭 무게 합 + 대기중인 트럭의 첫 무게가 감당 무게 이하면 + // 다리 위 트럭 무게 업데이트, 큐 뒤에 [트럭무게, 이 트럭이 나갈 시간] 추가. + weightOnBridge += truck_weights[0]; + qu.push([truck_weights.shift(), time + bridge_length]); + } else { + // 3. 다음 트럭이 못올라오는 상황이면 얼른 큐의 + // 첫번째 트럭이 빠지도록 그 시간으로 점프한다. + // 참고: if 밖에서 1 더하기 때문에 -1 해줌 + if (qu[0]) time = qu[0][1] - 1; + } + // 시간 업데이트 해준다. + time++; + } + return time; +} + +// Test Case +// console.log(solution(2, 10, [7, 4, 5, 6])); // 8 +// console.log(solution(100, 100, [10])); // 101 +// console.log(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])); // 110 + +/** + * [더 나은 풀이] + * + "시간 점프" 가 포인트 ! + */ diff --git "a/Programmers/Youngmin/Stack&Queue/\355\224\204\353\246\260\355\204\260/MY-\355\224\204\353\246\260\355\204\260.js" "b/Programmers/Youngmin/Stack&Queue/\355\224\204\353\246\260\355\204\260/MY-\355\224\204\353\246\260\355\204\260.js" new file mode 100644 index 0000000..5e855ba --- /dev/null +++ "b/Programmers/Youngmin/Stack&Queue/\355\224\204\353\246\260\355\204\260/MY-\355\224\204\353\246\260\355\204\260.js" @@ -0,0 +1,30 @@ +function solution(priorities, location) { + const prioritiesObjs = priorities.map((priority, location) => ({ location, priority })); + + let result = 0; + while (prioritiesObjs) { + const popPriority = prioritiesObjs.shift(); + + if (prioritiesObjs.some((priority) => popPriority.priority < priority.priority)) { + prioritiesObjs.push(popPriority); + } + + if (prioritiesObjs.every((priority) => popPriority.priority >= priority.priority)) { + result = result + 1; + if (popPriority.location === location) break; + } + } + + return result; +} + +// Test Case +// console.log(solution([2, 1, 3, 2], 2)); // 1 +// console.log(solution([1, 1, 9, 1, 1, 1], 0)); // 5 +// console.log(solution([1], 0)); // 1 + +/** + * 문제: 프린터 - 프로그래머스 + * 테마: 스택 / 큐 + * 출처: https://programmers.co.kr/learn/courses/30/lessons/42587 + */