알고리즘 91

[Lv_3] 하노이 탑

🔍 아이디어 ✅ 각 층별로 옮기기 위한 점화식을 재귀코드로 표현 import java.util.*; class Solution { List moved = new ArrayList(); public List solution(int n) { hanoi(n, 1, 2, 3); return moved; } void hanoi(int count, int start, int sub, int end) { if (count == 1) { moved.add(new int[] { start, end }); return; } hanoi(count - 1, start, end, sub); moved.add(new int[] { start, end }); hanoi(count - 1, sub, start, end); } } ✔..

알고리즘 2023.11.13

[Lv_2] 주차요금계산

🔍 아이디어 ✅ Car 클래스를 생성해 값을 관리했다. import java.util.*; class Solution { public int[] solution(int[] fees, String[] records) { int[] answer = {}; Map carMap = new HashMap(); for (int i = 0; i < records.length; i++) { String[] record = records[i].split(" "); String time = record[0]; String name = record[1]; String type = record[2]; // Key가 없으면 값을 생성해서 값을 항상 얻을 수 있도록 보장 if (!carMap.containsKey(name)) {..

알고리즘 2023.11.12

[Lv_2] 소수 찾기

🔍 아이디어 ✅ 주어진 카드로 만들 수 있는 수를 구한다.(순열) ✅ 구해진 수를 체크하고, 체크된 개수만큼 반환한다. import java.util.*; class Solution { static Set output = new HashSet(); public int solution(String numbers) { int answer = 0; String[] nums = numbers.split(""); for (int i = 1; i < numbers.length() + 1; i++) { perm(nums, new boolean[numbers.length()], 0, i, ""); } for (Integer num : output) { // Set을 순회하며, 소수인 경우 답을 +1 해준다. if (i..

알고리즘 2023.11.04

[Lv_2] 괄호 회전하기

🔍 아이디어 ✅ _!(, {, [!_ 가 확인되면 스택에 담고, 닫는 부호가 확인되면 스택에서 꺼내 비교한다. ✅ 비교결과 짝이 맞지 않거나, 순회를 종료한 시점에 스택 내 남은 괄호가 있다면 _!false!_ 처리 import java.util.*; class Solution { Map map = new HashMap(); public int solution(String s) { int answer = 0; map.put(']', '['); map.put('}', '{'); map.put(')', '('); StringBuilder sb = new StringBuilder(s); if (check(sb.toString())) { answer++; } for (int i = 0; i < sb.length..

알고리즘 2023.10.29

[Lv_2] 순위검색

🔍 아이디어 ✅ 지원자들의 스펙을 가능한 모든 경우의 수를 구해 _!Map!_ 의 _!key!_ 로 저장한다. ✅ 저장한 곳의 _!value!_ 에는 지원자의 점수를 저장해두고, 모든 순회가 종료되면 오름차순으로 정렬한다. ✅ _!query!_ 를 저장해둔 _!key!_ 의 형태로 변환하고 저장된 값이 있는지 찾는다. ✅ 값이 있다면, _!query!_ 의 코딩 기준 점수보다 높은 사람이 몇 명이 있는지 구하고, _!answer!_ 변수에 담아 반환한다. import java.util.*; class Solution { Map map = new HashMap(); public int[] solution(String[] info, String[] query) { int[] answer = new int[q..

알고리즘 2023.10.29

[Lv_3] 표현 가능한 이진트리

🔍 아이디어✔️ 주어진 숫자를 이진수로 변환하고, 이진수를 이진트리로 표현할 경우의 최대 깊이를 구한다. ✔️ 최대 깊이를 통해 최대 노드 개수를 계산하고, 부족한 노드만큼 _!0!_ 을 붙여 완전포화이진트리로 만든다. ✔️ 중앙 인덱스 값을 체크하면서 완전포화이진트리 검증한다.import java.util.*; class Solution { // 정답 배열 static int[] ans = null; public int[] solution(long[] numbers) { ans = new int[numbers.length]; Arrays.fill(ans, 1); for (int i = 0; i < numbers.length; i++) { String binary = Long.toBinaryString(..

알고리즘 2023.10.20