목록분류 전체보기 (150)
코딩응급실
import java.util.LinkedList; import java.util.Queue; public class Solution { public int solution(int bridge_length, int weight, int[] truck_weights) { Queue bridge = new LinkedList(); int time = 0; int currentWeight = 0; // 현재 다리 위의 총 무게 int index = 0; // 대기 트럭 인덱스 while(index < truck_weights.length) { // 다리가 꽉 찼을 경우, 가장 앞에 있는 트럭이 나감 if(bridge.size() == bridge_length) { currentWeight -= bridge.p..
import java.util.HashSet; public class Solution { private HashSet numberSet = new HashSet(); public int solution(String numbers) { boolean[] visited = new boolean[numbers.length()]; StringBuilder temp = new StringBuilder(); for (int i = 0; i < numbers.length(); i++) { // 숫자 조합 생성 permutation(numbers, temp, visited, 0, i + 1); } // 소수 판별 return (int) numberSet.stream().filter(this::isPrime).count..
내가 짠 코드는 아니다. 매우 직관적으로 짜여서 놀랐다. "이렇게 깔끔하다고?"가 처음 든 생각이었다. import java.util.*; public class Solution { public String solution(int[] numbers) { String[] nums = new String[numbers.length]; for (int i=0; i
public class Solution { public long[] solution(long[] numbers) { long[] answer = new long[numbers.length]; for (int i = 0; i < numbers.length; i++) { answer[i] = findNext(numbers[i]); } return answer; } private long findNext(long number) { if (number % 2 == 0) { // 짝수인 경우, 가장 오른쪽 비트가 0이므로 1로 바꾸면 됩니다. return number + 1; } else { // 홀수인 경우, 0인 비트를 찾아 그 위치를 1로 바꾸고, 바로 오른쪽 비트를 0으로 만듭니다. // '0'이 나타나는..
import java.util.*; public class Solution { public int solution(int n) { if (n == 1) return 1; if (n == 2) return 2; int mod = 1_000_000_007; //계산 과정에서 나오는 모든 값은 1,000,000,007로 나눈 나머지를 사용하여 오버플로우를 방지합니다. int[] dp = new int[n + 1]; dp[1] = 1; dp[2] = 2; for (int i = 3; i
import java.util.*; public class Solution { public int solution(int m, int n, String[] board) { char[][] map = new char[m][n]; // 문자열 배열을 2차원 char 배열로 변환 for (int i = 0; i < m; i++) { map[i] = board[i].toCharArray(); } int count = 0; // 지워진 블록의 총 개수 while (true) { List toRemove = new ArrayList(); // 이번 라운드에 지워질 블록들의 위치 저장 // 2x2 체크 for (int i = 0; i < m - 1; i++) { for (int j = 0; j < n - 1; j++..
import java.util.*; class Solution { public int solution(int x, int y, int n) { // 방문한 상태를 저장하는 배열, x가 1,000,000까지 가능하므로 그에 맞게 배열 크기 설정 boolean[] visited = new boolean[1000001]; // BFS를 위한 큐, 배열을 사용하여 현재 값과 연산 횟수를 저장 Queue queue = new LinkedList(); // 시작점 추가 queue.add(new int[]{x, 0}); while (!queue.isEmpty()) { int[] current = queue.poll(); int currentValue = current[0]; int count = current[1];..