본문 바로가기

Java66

[Java] 프로그래머스 [Level-3] 네트워크 public class Solution { // 네트워크 static boolean[] visited = {}; public static void dfs(int i, int[][] computers) { visited[i] = true; for(int j = 0; j < computers[i].length; j++) { if(!visited[j] && computers[i][j] == 1) { dfs(j, computers); } } } public static int solution(int n, int[][] computers) { int answer = 0; visited = new boolean[n]; for(int i = 0; i < n; i++) { if(!visited[i]) { answer+.. 2023. 1. 1.
[Java] 프로그래머스 [Level-3] 단어 변환 public class Solution { // 단어 변환 static boolean[] visited = {}; static int gDepth = 0; public static boolean strCompare(String str1, String str2) { // str1과 str2의 길이가 같다는 조건 하에 int strLength = str1.length(); int cnt = 0; for(int i = 0; i < strLength; i++) { if(str1.charAt(i) != str2.charAt(i)) { cnt++; } } if(cnt == 1) { // 변형 조건에서 1글자만 바꿀 수 있는데 1글자만 다를 경우 return true; } else { return false; } }.. 2022. 12. 31.
[Java] 프로그래머스 [Level-2] 디펜스 게임 import java.util.PriorityQueue; public class Solution { // 디펜스 게임 public static int solution(int n, int k, int[] enemy) { int answer = enemy.length; // 모든 라운드 클리어 가능할 경우의 리턴 값 PriorityQueue pq = new PriorityQueue(); // 무적권을 사용해 해치울 적의 수를 남기기 위한 우선순위 큐 // 디펜스 게임 시작 for(int i = 0; i < enemy.length; i++) { // 4, 2, 4, 5, 3, 3, 1 pq.offer(enemy[i]); // 4 // 2, 4 // 2, 4, 4 // 2, 4, 4, 5 // 3, 4, 4, .. 2022. 12. 27.
[Java] 백준 [11048] 이동하기 public class Solution { // 이동하기 - DP 11048번 static int n = 3; static int m = 4; static int[][] arr = {{1, 2, 3, 4}, {0, 0, 0, 5}, {9, 8, 7, 6}}; static int[] dy = {1, 1, 0}; static int[] dx = {0, 1, 1}; static int[][] dp = new int[n][m]; public static void dfs(int a, int b) { for(int i = 0; i n - 1 || nx > m - 1) { continue; } // dp 갱신 .. 2022. 12. 26.
[Java] 백준 [2178] 미로 탐색 import java.util.LinkedList; import java.util.Queue; public class Solution { // 미로 탐색 - BFS 2178번 // 최단 거리 탐색 BFS static int r = 4; static int c = 6; static int[][] arr = {{1, 0, 1, 1, 1, 1}, {1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1}}; static boolean[][] visit = {}; static int[] dx = {0, 0, -1, 1}; static int[] dy = {-1, 1, 0, 0}; public static void bfs(int n, int m) { Queue q =.. 2022. 12. 24.
[Java] 백준 [1541] 잃어버린 괄호 public class Solution { // 잃어버린 괄호 - Greedy 1541번 public static int solution(String str) { String[] strArr1 = str.split("-"); // 문자 "-" 기준으로 split int totalSum = 0; int tempSum = 0; // strArr1 = {"55", "50+40", "20", "30+40"} for (int i = 0; i < strArr1.length; i++) { tempSum = 0; String[] strArr2 = strArr1[i].split("\\+"); // 특수문자 "+" 기준으로 split (특수문자의 경우 \\특수문자 or [특수문자]) for (int j = 0; j < s.. 2022. 12. 18.