Coding Test
-
[프로그래머스 - Lv.1 / LinkedHashMap] 신고 결과 받기Coding Test 2023. 7. 30. 21:11
import java.util.*; class Solution { public Collection solution(String[] id_list, String[] report, int k) { // 유저 ID 순서가 필요해서 LinkedHashMap 사용 Map map1 = new LinkedHashMap(); Map map2 = new LinkedHashMap(); // 정지된 ID 저장할 List List list = new ArrayList(); // 중복 제거를 위해 report를 map1에 저장 // (Map에서 Key는 중복 불가) for(String key : report) { map1.put(key, 0); } // id_list를 map2에 저장 for(String key : id_lis..
-
[프로그래머스 - 입문] 안전지대Coding Test 2023. 7. 5. 23:26
class Solution { public int solution(int[][] board) { int answer = 0; // board보다 상하좌우 1칸씩 큰 boardCopy 배열 선언 int[][] boardCopy = new int[board.length+2][board.length+2]; // boardCopy에 지뢰가 있는 곳 마킹 for(int i = 0; i < board.length; i++) { for(int j = 0; j < board.length; j++) { boardCopy[i+1][j+1] = board[i][j]; } } // 지뢰 기준 상하좌우, 대각선 총 8곳 위험지역 마킹 for(int i = 0; i < board.length; i++) { for(int j = ..
-
[프로그래머스 - charAt()] 가장 까까운 같은 글자Coding Test 2023. 7. 2. 21:11
class Solution { public int[] solution(String s) { int[] answer = new int[s.length()]; String[] arr = s.split(""); // 주어진 문자열을 하나씩 저장 answer[0] = -1; // 맨 앞은 무조건 -1 for(int i = 1; i = 0; j--) { if (s.charAt(j) == s.charAt(i)) { // 일치 answer[i] = i - j; break; } else { // 불일치 answer[i] = -1; } } } return answer; } }
-
[프로그래머스] 체육복Coding Test 2023. 7. 2. 15:36
import java.util.*; class Solution { public int solution(int n, int[] lost, int[] reserve) { List lostList = new ArrayList(); List reserveList = new ArrayList(); for (int i : lost) { lostList.add(i); } for (int i : reserve) { reserveList.add(i); } // 정렬 Collections.sort(lostList); Collections.sort(reserveList); // 여벌 체육복을 가져온 학생이 체육복을 도난당한 경우 우선 처리 for (int i = 0; i < reserveList.size(); i++) { ..
-
[프로그래머스 - 소수] 소수 찾기Coding Test 2023. 6. 25. 22:55
import java.util.*; class Solution { private HashSet set = new HashSet(); public int solution(String numbers) { boolean[] visited = new boolean[numbers.length()]; StringBuilder sb = new StringBuilder(); // 종이 조각으로 만들 수 있는 모든 숫자 조합 생성 generateNumbers(numbers, visited, sb); int count = 0; for (int num : set) { if (isPrime(num)) { count++; } } return count; } // 종이 조각으로 만들 수 있는 모든 숫자 조합 생성 private ..
-
[프로그래머스 - 해시] 의상Coding Test 2023. 6. 24. 23:31
import java.util.*; class Solution { public int solution(String[][] clothes) { Map map = new HashMap(); // 옷의 종류별로 개수를 카운트 for (String[] cloth : clothes) { String type = cloth[1]; // getOrDefault(key, 기본값): key값이 있으면 기본값으로 설정 map.put(type, map.getOrDefault(type, 0) + 1); } int answer = 1; // 옷의 종류별로 선택할 수 있는 개수를 곱하여 조합 계산 for (int count : map.values()) { answer *= count + 1; } // -1 -> 아무 의상도 선택..
-
[프로그래머스 - DFS] 네트워크Coding Test 2023. 6. 18. 21:04
class Solution { public int solution(int n, int[][] computers) { int answer = 0; boolean[] visited = new boolean[n]; // 컴퓨터 방문 여부, boolean[] 초기값 false for (int i = 0; i < n; i++) { if (!visited[i]) { // 아직 방문하지 않은 경우 dfs(i, n, computers, visited); // DFS로 네트워크를 탐색하고 방문 처리 answer++; // 네트워크 개수 증가 } } return answer; } // DFS를 활용하여 네트워크 탐색 private void dfs(int com, int n, int[][] computers, boolean..