분류 전체보기 166

[프로그래머스: 정렬] 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..

[Java] 람다(Lambda)를 활용한 커스텀 정렬 이해하기

왜 커스텀 정렬이 필요할까?Java에서 Arrays.sort()나 Collections.sort()를 사용하면 기본적으로 오름차순 정렬이 된다.내림차순 정렬은 Arrays.sort(배열, Collections.reverseOrder()); 와 같이 사용한다.(단, sort()에 Collections.reverseOrder()를 인자로 전달하기 위해서 배열은 int[]가 아닌 Integer[]처럼 래퍼클래스여야 함)Arrays.sort(배열); // 오름차순 (default)Arrays.sort(배열, Collections.reverseOrder()); // 내림차순하지만 코딩테스트에서는 종종 특정 조건에 따라 정렬해야 할 상황이 생긴다.문자열 길이 기준 정렬객체의 점수 기준 내림차순 정렬두 가지 조건(점..

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

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차원인 이유 : 방문 여부를 ..