코딩테스트/자바 문제풀이 122

[프로그래머스: 정렬] K번째수

https://school.programmers.co.kr/learn/courses/30/lessons/42748 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # 코드import java.util.*;class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; for (int idx = 0; idx # ✨ 프로그래머스 1등 코드 ✨ import java.util.Arrays;class Solution { p..

[프로그래머스: 완전탐색] 피로도

https://school.programmers.co.kr/learn/courses/30/lessons/87946 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # DFS 코드class Solution { int maxCount = 0; // 최대 탐험 수를 저장할 변수 public int solution(int k, int[][] dungeons) { boolean[] visited = new boolean[dungeons.length]; // 던전 방문 여부 체크 배열 // DFS를 통해 던전 순서를 모두 탐색 dfs(k, dungeons, visit..

[프로그래머스: 완전탐색] 카펫

https://school.programmers.co.kr/learn/courses/30/lessons/42842 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # 코드class Solution { public int[] solution(int brown, int yellow) { int size = brown + yellow; // 카펫의 면적 (총 격자 수) for (int h = 3; h 갈색 타일 수 계산식 : (가로 + 세로 - 2) * 2 = brown노란색 타일 수 계산식 : (가로 - 2) * (세로 - 2) == yellow갈색 타일 수 계산..

[프로그래머스: 완전탐색] 소수 찾기

https://school.programmers.co.kr/learn/courses/30/lessons/42839 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # 코드import java.util.*;class Solution { Set set = new HashSet(); // 중복없이 저장하기 위한 set public int solution(String numbers) { permutation("", numbers); // 모든 숫자 조합을 만들어 set에 저장 int answer = 0; for (int num : set) {..

[프로그래머스: 완전탐색] 모의고사

https://school.programmers.co.kr/learn/courses/30/lessons/42840 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # 코드import java.util.*;class Solution { public int[] solution(int[] answers) { int[] pattern1 = {1, 2, 3, 4, 5}; int[] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5}; int[] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; int[] score = ..

[프로그래머스: 완전탐색] 최소직사각형

https://school.programmers.co.kr/learn/courses/30/lessons/86491 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # 초기 코드class Solution { public int solution(int[][] sizes) { int max_w = 0; int max_h = 0; for (int[] card : sizes) { // 세로 길이가 더 길면 눕힘 if (card[0] 카드 하나하나 눕히는 딩초코드 ┌( ´_ゝ` )┐ # ✨ 프로그래머스 1등 코드 ✨..

[프로그래머스: 해시] 완주하지 못한 선수

https://school.programmers.co.kr/learn/courses/30/lessons/42576 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # sort()를 이용한 코드import java.util.*;class Solution { public String solution(String[] participant, String[] completion) { Arrays.sort(participant); Arrays.sort(completion); for (int i = 0; i 시간복잡도 : O(n log n) # HashMa..

[프로그래머스: BFS] 단어 변환

https://school.programmers.co.kr/learn/courses/30/lessons/43163 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # BFS 코드import java.util.*;class Solution { List visited; // 방문 여부 체크 리스트 (방문한 단어를 저장) public int solution(String begin, String target, String[] words) { visited = new ArrayList(); // 방문 리스트 초기화 return bfs(begin, target, ..

[프로그래머스: DFS/BFS] 네트워크

https://school.programmers.co.kr/learn/courses/30/lessons/43162 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # DFS 코드class Solution { public int solution(int n, int[][] computers) { boolean[] visited = new boolean[n]; // 각 컴퓨터의 방문 여부 체크 배열 int answer = 0; for (int i = 0; i DFS/BFS 모두 방문 여부를 체크하는 visited 배열이 2차원이 아니라 1차원인 이유 : 방문 여부를 ..

[프로그래머스: DFS] 여행경로

https://school.programmers.co.kr/learn/courses/30/lessons/43164 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr # DFS 코드import java.util.*;class Solution { List route = new ArrayList(); // 최종 정답 경로를 저장할 ArrayList Map> map = new HashMap(); // 출발지 → 도착지를 저장할 map (알파벳 순 정렬 위해 우선순위 큐 사용) public String[] solution(String[][] tickets) { for (String[] ..