분류 전체보기
-
[프로그래머스] 완주하지 못한 선수Coding Test 2023. 6. 8. 11:44
import java.util.*; class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; Map map1 = new HashMap (); // 참가자 저장할 map1 Map map2 = new HashMap (); // 완주자 저장할 map2 for(String key : participant) { // 참가자는 key, 인원수는 value // getOrDefault(): map에 key가 없으면 기본값(0) 지정 map1.put(key, map1.getOrDefault(key, 0) + 1); // 인원 세려고 +1 } for(String key : completion..
-
[프로그래머스] 타겟 넘버Coding Test 2023. 6. 6. 23:41
입출력 예시 numbers target return [4, 1, 2, 1] 4 2 class Solution { static int answer = 0; // dfs에서 사용할 수 있도록 전역 변수 선언 public int solution(int[] numbers, int target) { dfs(numbers, target, 0, 0); // 초깃값으로 depth, sum 모두 0으로 세팅 return answer; } // dfs(Depth-First Search: 깊이 우선 탐색) public void dfs(int[] numbers, int target, int depth, int sum) { // 마지막 노드까지 탐색을 완료했을 때(= numbers 끝까지 순회 완료) if(depth == nu..
-
[프로그래머스] 최소직사각형Coding Test 2023. 6. 6. 00:28
import java.util.*; class Solution { public int solution(int[][] sizes) { // 두 변 중에서 긴 쪽을 가로로, 짧은 쪽을 세로로 정렬하고 // 가로 열과 세로 열에서 각각의 최댓값을 곱하면 되는 문제 int answer = 0; int maxW = 0; int maxH = 0; for(int i = 0; i < sizes.length; i++) { int w = Math.max(sizes[i][0], sizes[i][1]); // 두 변중 긴 부분을 가로로 int h = Math.min(sizes[i][0], sizes[i][1]); // 두 변중 짧은 부분을 세로로 maxW = Math.max(maxW, w); // 초깃값 0부터 시작해서 fo..
-
[프로그래머스] K번째 수Coding Test 2023. 6. 4. 21:21
import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; // commands의 길이만큼 초기화 ArrayList arr = new ArrayList(); // ArrayList 선언 for(int i = 0; i < commands.length; i++) { for(int j = commands[i][0]; j
-
[프로그래머스] 같은 숫자는 싫어Coding Test 2023. 6. 4. 20:39
Stack -> Fist In Last Out import java.util.*; public class Solution { public int[] solution(int []arr) { Stack st = new Stack(); st.push(arr[0]); // arr[0]을 초기값으로 설정 for(int i = 1; i < arr.length; i++) { if(st.peek() == arr[i]) { // 스택에 저장된 맨 위 요소와 arr 비교 continue; } else { st.push(arr[i]); // 스택에 저장 } } int[] answer = new int[st.size()]; // 스택 크기만큼 answer 크기 지정 for(int i = answer.length - 1; i ..
-
[프로그래머스] 폰켓몬Coding Test 2023. 6. 4. 15:07
- HashMap은 “key-value” 구조 - key는 고유한 속성 -> 중복 불가 - value는 중복 가능 import java.util.HashMap; class Solution { public int solution(int[] nums) { int answer = 0; answer = nums.length / 2; // 가질 수 있는 폰켓몬의 최댓값 HashMap map = new HashMap(); // HashMap에서 Key는 중복 불가능 for(int i = 0; i map.size()) { //..