[프로그래머스/Lv.1] 문자열 내 마음대로 정렬하기 (Java)
·
✏️/Programmers
문자열 내 마음대로 정렬하기https://school.programmers.co.kr/learn/courses/30/lessons/12915풀이객체 비교Comparable (자기 자신 기준)클래스 정렬 기준 정하고 싶을 때 사용클래스 내부에 구현정렬 기준 1개`compareTo` 메서드 사용-> 재정의(Override) 해야 함class Person implements Comparable { int age; Person(int age) { this.age = age; } @Override public int compareTo(Person o) { return this.age - o.age; // 나이 오름차순 }}Arrays.sort(persons..
[프로그래머스/Lv.1] PCCP - 붕대 감기 (Java)
·
✏️/Programmers
붕대 감기https://school.programmers.co.kr/learn/courses/30/lessons/250137풀이공격이 있는 경우if (attackIdx 공격이 없는 경우 -> 회복else { cnt++; health += bandage[1]; if (cnt == bandage[0]) { health += bandage[2]; cnt = 0; } if (health > maxHealth) health = maxHealth;}코드class Solution { public int solution(int[] bandage, int health, int[][] attacks) { int maxHealth = health; ..
[프로그래머스/Lv.1] PCCP - 동영상 재생기 (Java)
·
✏️/Programmers
동영상 재생기https://school.programmers.co.kr/learn/courses/30/lessons/340213풀이주어진 분:초 -> 초로 변경static int toTotalSec(String time) { String[] p = time.split(":"); int m = Integer.parseInt(p[0]); int s = Integer.parseInt(p[1]); return m * 60 + s;}코드class Solution { static int toTotalSec(String time) { String[] p = time.split(":"); int m = Integer.parseInt(p[0]); int ..
[프로그래머스/Lv.2] 메뉴 리뉴얼 (Java)
·
✏️/Programmers
메뉴 리뉴얼https://school.programmers.co.kr/learn/courses/30/lessons/72411풀이가능한 모든 조합목표 길이 도달 -> 조합 완성// 조합, 빈도static Map HM;static void comb(char[] arr, int start, int len, StringBuilder sb) { if (sb.length() == len) { String com = sb.toString(); HM.put(com, HM.getOrDefault(com, 0) + 1); return; } for (int i = start; i 2회 이상인 최대 빈도 찾기 -> 최대 빈도 조합 결과에 추가for (int cnt : H..
[프로그래머스/Lv.2] 오픈채팅방 (Java)
·
✏️/Programmers
오픈채팅방https://school.programmers.co.kr/learn/courses/30/lessons/42888풀이처음 코드`record`를 2번 돌면서 닉네임 갱신, 결과 생성1차 순회: `record``userId` -> 최종 닉네임`Map HM = new HashMap()`결과 개수 계산(`n`)2차 순회: `record``Enter` / `Leave`에 대해 결과 문자열 생성 개선한 코드 상태로 닉네임 관리, `Enter` / `Leave` 로그로 분리해 한 번에 출력1차 순회: `record``userId` -> 최종 닉네임`Map HM = new HashMap()`출력 대상 로그: `userId`, `word``List logs = new ArrayList()`2차 순회: `logs..
[프로그래머스/Lv.1] 신고 결과 받기 (Java)
·
✏️/Programmers
신고 결과 받기https://school.programmers.co.kr/learn/courses/30/lessons/92334풀이id -> index 매핑신고 결과 계산할 때 "muzi"같은 문자열 id를 결과 배열 인덱스로 바로 접근해야 함 -> O(1)X => 매번 id_list를 탐색해야 함Map HM = new HashMap();for (int i = 0; i 신고 관계 정리피신고자 -> 자기를 신고한 사람들 집합 => 1번 신고만 인정피신고자(`to`) X -> 새로운 `HashSet` 생성O -> 기존 `Set` 사용신고자(`from`)을 `Set`에 추가Map> reportedBy = new HashMap();for (String r : report) { String[] str = ..
aeongg
빙글빙글 돌아가는 Debug 하루