본문 바로가기

전체 글215

[Java] 프로그래머스 [Level-3] 순위 public class Solution { // 순위 // Floyd Warshall 알고리즘으로 풀이 public static int solution(int n, int[][] results) { int answer = 0; int zeroCnt = 0; int[][] board = new int[n][n]; // 0 ~ 4 for (int i = 0; i < results.length; i++) { // {4, 3}의 경우 board[results[i][0] - 1][results[i][1] - 1] = 1; // 4는 3에게 이겼으므로 board[3][2] = 1 board[results[i][1] - 1][results[i][0] - 1] = -1; // 3은 4에게 졌으므로 board[2][3].. 2022. 11. 29.
[Java] 프로그래머스 [Level-2] 롤케이크 자르기 import java.util.HashMap; public class Solution { // 롤케이크 자르기 public static int solution(int[] topping) { int answer = 0; HashMap hm1 = new HashMap(); HashMap hm2 = new HashMap(); for (int i = 0; i < topping.length; i++) { hm2.put(topping[i], hm2.getOrDefault(topping[i], 0) + 1); } // hm2 : {1=4, 2=2, 3=1, 4=1} for (int i = 0; i < topping.length; i++) { hm1.put(topping[i], hm1.getOrDefault(toppi.. 2022. 11. 29.
[Java] 프로그래머스 [Level-3] 양과 늑대 import java.util.ArrayList; import java.util.HashMap; public class Solution { // 양과 늑대 static int maxSheepCnt; static HashMap hm; public static void dfs(int currentIndex, int s, int w, ArrayList indexList, int[] info) { if (info[currentIndex] == 0) { s += 1; } else { w += 1; } if (s 2022. 11. 29.
[Java] 프로그래머스 [Level-3] 아이템 줍기 import java.util.LinkedList; import java.util.Queue; public class Solution { // 아이템 줍기 public static int solution(int[][] rectangle, int characterX, int characterY, int itemX, int itemY) { int answer = 0; int[][] board = new int[101][101]; // 전체 영역 boolean[][] visited = new boolean[101][101]; // 방문한 좌표인지 확인을 위한 2차원 boolean 배열 int[] dx = {0, 0, -1, 1}; // 상, 하, 좌, 우 이동에서 x 좌표 증감 값 int[] dy = {1, .. 2022. 11. 29.
[Java] 프로그래머스 [Level-2] 피로도 public class Solution { // 피로도 static int answer = 0; static boolean[] visited = {}; public static void dfs(int depth, int k, int[][] dungeons) { for (int i = 0; i depth가 0 & 세 번째 던전에서 dfs visited[i] = true; // 해당 던전을 방문한 상태에서 다음 depth로 dfs dfs(depth + 1, k - dungeons[i][1], dungeons); // depth가 1 & 위 3가지 각각의.. 2022. 11. 29.
[Java] 프로그래머스 [Level-2] 모음사전 import java.util.HashMap; public class Solution { // 모음사전 public static int solution(String word) { int answer = 0; HashMap hm = new HashMap(); hm.put('A', 0); hm.put('E', 1); hm.put('I', 2); hm.put('O', 3); hm.put('U', 4); // A 1 // AA 2 // AAA 3 // AAAA 4 // AAAAA 5 // AAAAE 6 (AAAAA보다 + 1) (+ 1) // ... // AAAAU 9 // AAAE 10 (AAAA보다 + 6) (1 + 5) // AAAEA 11 // AAAEU 15 // AAAI 16 // ... // AAA.. 2022. 11. 29.