diff --git "a/BOJ/1000-10000\353\262\210/HW_2531.java" "b/BOJ/1000-10000\353\262\210/HW_2531.java" new file mode 100644 index 00000000..5ff29f62 --- /dev/null +++ "b/BOJ/1000-10000\353\262\210/HW_2531.java" @@ -0,0 +1,60 @@ +import java.io.*; +import java.util.StringTokenizer; + +// 손님이 먹을 수 있는 초밥 가짓수의 최댓값을 구하는 프로그램 +public class HW_2531 { + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); // 초밥 벨트에 놓인 접시의 수 + int d = Integer.parseInt(st.nextToken()); // 초밥의 가짓 수 + int k = Integer.parseInt(st.nextToken()); // 연속해서 먹는 접시의 수 + int c = Integer.parseInt(st.nextToken()); // 쿠폰 번호 + + int rail[] = new int[N]; // 레일배열 + + for(int i=0; i 슬라이딩 윈도우 + // window : k, if(c) 쿠폰 번호 여부 확인하여 count++ + int checkArr[] = new int[d+1]; // 초밥 종류 배열 + int count = 0; // 경우의 수 카운트 + int max = 0; + // 윈도우 배열 초기화 + for(int i=0; i= max) { + if(checkArr[c]==0) { + max = count+1; + } + else { + max = count; + } + } + checkArr[rail[start]]--; + if(checkArr[rail[start]]==0) { + count--; + } + if(checkArr[rail[end]]==0) { + count++; + } + checkArr[rail[end]]++; + } + System.out.println(max); + } +} diff --git "a/BOJ/1000-10000\353\262\210/HW_3020.java" "b/BOJ/1000-10000\353\262\210/HW_3020.java" new file mode 100644 index 00000000..8bd4fbb2 --- /dev/null +++ "b/BOJ/1000-10000\353\262\210/HW_3020.java" @@ -0,0 +1,43 @@ +import java.util.Scanner; + +// 시간 복잡도 : 시간 제한 1초, 장애물의 크기 : O(NlogN) +public class HW_3020 { + public static void main(String[] args) { + // 개똥벌레가 파괴해야 하는 장애물의 최솟값과 그러한 구간의 수 출력 + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); // 동굴 크기 (장애물의 개수) + int H = sc.nextInt(); // 동굴 높이 + + int[] up = new int[H+1]; // 석순 장애물 + int[] down = new int[H+1]; // 종유석 장애물 + + for (int n = 0; n < N; n++) { + int height = sc.nextInt(); // 장애물의 높이를 입력 받음 + if (n % 2 == 0) { + up[height]++; + } else { + down[height]++; + } + } + + // 석순과 종유석 누적합 계산 + for(int i=H-1; i>=1; i--) { + up[i] += up[i + 1]; + down[i] += down[i + 1]; + } + + int min = N; + int minCount = 0; + + for(int i=1; i= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && grid[nx][ny] == 0) { // 왼쪽 방향으로 갈 수 있다면 -> 이동 + visited[nx][ny] = true; + x = nx; + y = ny; + count++; // 방문한 칸 증가 + moved = true; + break; // 왼쪽 방향으로 이동했으므로 탐색 종료 + } + } + + if (!moved) { // 4방향 모두 이동할 수 없는 경우, 현재 방향에서 후진 + int backX = x - dx[d]; + int backY = y - dy[d]; + + if (backX >= 0 && backX < n && backY >= 0 && backY < m && grid[backX][backY] == 0) { // 후진 가능 -> 후진 + x = backX; + y = backY; + } else { + break; + } + } + } + return count; // 방문한 칸의 총 개수 반환 + } +} \ No newline at end of file diff --git "a/CodeTree/2019-2020\353\205\204/HW_\353\266\210\354\225\210\355\225\234_\353\254\264\353\271\231\354\233\214\355\201\254.java" "b/CodeTree/2019-2020\353\205\204/HW_\353\266\210\354\225\210\355\225\234_\353\254\264\353\271\231\354\233\214\355\201\254.java" new file mode 100644 index 00000000..7af0fd43 --- /dev/null +++ "b/CodeTree/2019-2020\353\205\204/HW_\353\266\210\354\225\210\355\225\234_\353\254\264\353\271\231\354\233\214\355\201\254.java" @@ -0,0 +1,84 @@ +import java.util.Scanner; + +public class HW_불안한_무빙워크 { + public static int MAX_N = 100; + public static int n, k; + public static int[] top = new int[MAX_N * 2]; // 레일 길이가 + public static boolean[] check = new boolean[MAX_N]; // 위쪽 레일에서 사람이 있는지 여부 확인 + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); // 레일의 길이 + k = sc.nextInt(); // 안정성이 0인 칸이 k개 이상이면 과정 종료 + + for (int i = 0; i < 2 * n; i++) { // 위쪽과 아래쪽 레일을 한 배열로 입력 받음 + top[i] = sc.nextInt(); + } + + int tryCount = 0; + while (!done()) { + simulate(); + tryCount++; + } + System.out.println(tryCount); + } + + // 무빙워크를 시계 방향으로 한 칸 회전 + public static void shift() { + int tempStability = top[2 * n - 1]; + + // 위쪽 레일과 아래쪽 레일을 한 배열로 관리하기 때문에 전체적으로 한 칸씩 회전 + for (int i = 2 * n - 1; i >= 1; i--) { + top[i] = top[i - 1]; + } + top[0] = tempStability; + + // 사람의 위치도 함께 회전 + for (int i = n - 1; i >= 1; i--) { + check[i] = check[i - 1]; + } + check[0] = false; // 첫 번째 칸에 사람은 없음 + check[n - 1] = false; // 마지막 칸에 사람이 있으면 내림 + } + + // 사람이 현재 위치에서 다음 위치로 이동 가능한지 확인 + public static boolean canGo(int idx) { + return top[idx] > 0 && !check[idx]; // 안정성이 0보다 크고 사람이 없으면 이동 가능 + } + + // 뒤에서부터 사람이 이동할 수 있는지 확인하며 이동 + public static void movePerson() { + for (int i = n - 2; i >= 0; i--) { // 마지막 칸은 사람이 무조건 내려가기 때문에 n-2부터 시작 + if (check[i] && canGo(i + 1)) { + check[i] = false; // 현재 위치에서 사람이 이동 + check[i + 1] = true; // 다음 위치로 이동 + top[i + 1]--; // 이동한 칸의 안정성 감소 + } + } + check[n - 1] = false; // 마지막 칸에 도달한 사람은 내려감 + } + + public static void add() { + if (top[0] > 0 && !check[0]) { + check[0] = true; // 첫 번째 칸에 사람 추가 + top[0]--; // 안정성 1 감소 + } + } + + public static void simulate() { + shift(); // 1. 무빙워크 한 칸 회전 + movePerson(); // 2. 사람 이동 + add(); // 3. 새로운 사람 추가 + } + + // 안정성이 0인 칸이 k개 이상인지를 확인 + public static boolean done() { + int unstableCount = 0; + for (int i = 0; i < 2 * n; i++) { + if (top[i] == 0) { + unstableCount++; + } + } + return unstableCount >= k; + } +} diff --git a/Programmers/Level2/HW_132265.java b/Programmers/Level2/HW_132265.java new file mode 100644 index 00000000..dde53371 --- /dev/null +++ b/Programmers/Level2/HW_132265.java @@ -0,0 +1,38 @@ +import java.util.*; + +// 시간 복잡도 : topping길이 : 백만 O(NM) X , O(N), O(N logN) ... +// 일반 정렬을 사용하면 시간초과 날 것 +// 롤케이크를 공평하게 자르는 방법의 수 출력 + +// 항상 궁금했던거.. 자바에서는 다른 원소를 어떻게 확인할까? - 중복 제거 : HashSet! + +class Solution { + public int solution(int[] topping) { + int answer = 0; + int N = topping.length; + int left[] = new int[N]; + int right[] = new int[N]; + + HashSet leftSet = new HashSet<>(); + HashSet rightSet = new HashSet<>(); + for(int i=0; i0; i--){ + rightSet.add(topping[i]); + right[i] = rightSet.size(); + } + + + for(int i=0; i stack = new Stack<>(); + + int size = numbers.length; + stack.push(0); + + for(int i=1; i numbers[i] + } + + while(!stack.empty()){ + answer[stack.pop()] = -1; + } + + return answer; + } +} \ No newline at end of file diff --git a/Programmers/Level2/HW_49994.java b/Programmers/Level2/HW_49994.java new file mode 100644 index 00000000..b39a39a9 --- /dev/null +++ b/Programmers/Level2/HW_49994.java @@ -0,0 +1,39 @@ +// [PG] 49994_방문 길이 +// 게임 캐릭터가 지나간 길 중 캐릭터가 처음 걸어본 길의 길이 구하기 +// 좌표평면(-5, 5) 벗어나는 명령어는 무시 +import java.util.*; +class Solution { + private static boolean isValidMove(int nx, int ny){ + return 0 <= nx && nx < 11 && 0 <= ny && ny < 11; + } + private static final HashMap location = new HashMap<>(); + + private static void initLocation(){ + location.put('U', new int[]{0, 1}); + location.put('D', new int[]{0, -1}); + location.put('L', new int[]{-1, 0}); + location.put('R', new int[]{1, 0}); + } + public int solution(String dirs) { // U(위), D(아래), L, R + initLocation(); + int x = 5, y=5; + HashSet answer = new HashSet<>(); + + for(int i=0; i Queue = new PriorityQueue<>(Comparator.reverseOrder()); // 높은 숫자 우선순위 큐 정의 + + for(int i=0; i0){ + int work = Queue.poll(); + if(work==0) { + break; + } + work -= 1; + Queue.offer(work); + n -=1; + } + + int size = Queue.size(); // Queue size 줄어드는 것 방지 + for(int i=0; i