[프로그래머스/Lv.1] 키패드 누르기 (Java)
·
✏️/Programmers
키패드 누르기풀이Pos 클래스좌표 + 거리 계산(맨해튼 거리)static class Pos { int x, y; Pos(int x, int y) { this.x = x; this.y = y; } int dist(Pos p) { return Math.abs(this.x - p.x) + Math.abs(this.y - p.y); }} 숫자 -> 키패드 좌표 변환`(n - 1) / 3`: 행`(n - 1) % 3`: 열(0,0) (0,1) (0,2) 1 2 3(1,0) (1,1) (1,2) 4 5 6(2,0) (2,1) (2,2) 7 8 9(3,0) (3,1) (3,2) * ..
[프로그래머스/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..
aeongg
빙글빙글 돌아가는 Debug 하루