본문 바로가기

Algorithm/프로그래머스8

[해시] 위장 해시-위장 java import java.util.*; class Solution { public int solution(String[][] clothes) { HashMap map = new HashMap(); for (String[] cloth : clothes) { // map.compute(cloth[1], (k, v) -> v == null ? 1 : v + 1); // compute를 이용해도 되지만, 아래 코드에 비해서 시간이 조금 더 걸린다. // compute는 언제 사용하는 건지 잘 모르겠다. 구현 코드를 보면 꽤 복잡해보인다. // (복잡해서 제대로 보진 않았다.) map.put(cloth[1], map.getOrDefault(cloth[1], 0) + 1); } Integer ans.. 2021. 9. 24.
[해시] 전화번호 목록 해시-전화번호 목록 java HashMap HashMap과 HashSet은 입력된 값(혹은 key를 기준으로)을 자동 정렬한다. 나는 번호 길이도 Set을 만들어서 for문을 돌렸다. 번호의 길이가 다양한 경우엔 사실 필요없을 것 같다. (1부터 최대 번호 길이까지 대부분이 포함될 때? 여튼, 데이터 상태에 따라 많이 다를 것 같다.) 정답 페이지를 둘러보다가 HashSet 보다 HashMap이 빠르다는 댓글을 보았다. 왜 그런지 찾아볼 예정. 효율성 성능만 테스트 3 〉 통과 (86.31ms, 101MB) 테스트 4 〉 통과 (76.96ms, 97MB) import java.util.*; class Solution { public boolean solution(String[] phone_book) { .. 2021. 9. 16.
[해시] 완주하지 못한 선수 프로그래머스 완주하지 못한 선수 java import java.util.*; class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; Map map = new HashMap(); // .getOrDefault() 메소드를 참고함 Integer n; for (String s : participant) { map.put(s, (( n = (map.get(s))) != null) ? n + 1 : 1); } for (String s : completion) { if ((n = (map.get(s) - 1)) == 0) map.remove(s); else map.put(s, n); .. 2021. 9. 15.
[스택/큐] 기능개발 프로그래머스 [스택/큐]기능개발 링크 ​ java import java.util.ArrayList; ​ class Solution { public int[] solution(int[] progresses, int[] speeds) { ​ ArrayList list = new ArrayList(); int temp = 0; int count = 0; int length = progresses.length; ​ for (int i = 0; i = day) { count++; } else { if (count> 0) list.add(cou.. 2021. 9. 13.