동전교환 (냅색 알고리즘) import java.util.Arrays; import java.util.Scanner; public class Main { //냅색 알고리즘 static int n, m; static int[] coins; //동전 종류 목록 static int[] dynamic; //최소 동전 개수 public int solution() { dynamic[0] = 0; for(int i=0; i [Solved]/코딩테스트 2023.04.06
최대점수 구하기 (냅색 알고리즘) import java.util.Scanner; public class Main06 { //냅색 알고리즘 public static void main(String[] args) { Scanner kb = new Scanner(System.in); int n = kb.nextInt(); int m = kb.nextInt(); int[] dynamic = new int[m+1]; for(int i=0; i=time; j--) { //앞에서부터 돌면 중복 //뒤에서부터 돌아야 중복 회피 dynamic[j] = Math.max(dynamic[j], dynamic[j-time]+score); } } System.out.println(dynamic[m]); } } [Solved]/코딩테스트 2023.04.05
뮤직비디오(결정알고리즘) import java.util.Arrays; import java.util.Scanner; public class Main { public int count(int[] arr, int capacity) { int cnt = 1; int sum = 0; for(int x : arr) { if(sum+x > capacity) { cnt++; sum = x; } else { sum += x; } } return cnt; } public int solution(int n, int cd, int[] arr) { int ans = 0; int lt = Arrays.stream(arr).max().getAsInt(); int rt = Arrays.stream(arr).sum(); while(lt [Solved]/코딩테스트 2023.04.03
섬나라 아일랜드 (BFS, DFS) BFS import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Node{ int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } } public class Main { static int answer = 0, n; static int[] dx = {1, 1, 1, -1, -1, -1, 0, 0}; static int[] dy = {1, 0, -1, 1, 0, -1, 1, -1}; static int[][] board; static Queue queue = new LinkedList(); public void BFS(int x, int y) { .. [Solved]/코딩테스트 2023.03.30
토마토(BFS 활용) import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Tomato{ int x, y; public Tomato(int x, int y) { this.x = x; this.y = y; } } public class Main { static int[][] box; static int[][] day; static int[] dx = {1, -1, 0, 0}; static int[] dy = {0, 0, 1, -1}; static int n, m; static Queue queue = new LinkedList(); public void BFS() { while(!queue.isEmpty()) { Tomato to.. [Solved]/코딩테스트 2023.03.29
후위연산식 문제 (Stack) import java.util.Scanner; import java.util.Stack; public class Main { public int solution(String str) { int ans = 0; Stack stk = new Stack(); for(char x : str.toCharArray()) { if(Character.isDigit(x)) stk.push(x-48); else { int rt = stk.pop(); int lt = stk.pop(); if(x == '+') stk.push((lt) + (rt)); else if(x == '-') stk.push((lt) - (rt)); else if(x == '*') stk.push((lt) * (rt)); else if(x == '/'.. [Solved]/코딩테스트 2023.03.18
임시반장 정하기 문제 import java.util.Scanner; public class Main { public int solution(int std, int[][] stdArr) { int ans = 0, max = Integer.MIN_VALUE; int[] score = new int[std+1]; for(int i = 1; i [Solved]/코딩테스트 2023.03.17
Least Recently Used public class Main { public int[] solution(int c, int n, int[] input) { int[] ans = new int[c]; for(int i = 0; i = 1; i--) { ans[i] = ans[i-1]; } } else { for(int i = idx; i >= 1; i--) { ans[i] = ans[i-1]; } } ans[0] = x; } return ans; } publ.. [Solved]/코딩테스트 2023.03.14
쇠막대기 절단 문제 (Stack) import java.util.Scanner; import java.util.Stack; public class Main { public int solution(String str) { int ans = 0; Stack stk = new Stack(); for(int i = 0; i < str.length(); i++) { char temp = str.charAt(i); if(temp == ')'){ if(str.charAt(i-1) == '(') { stk.pop(); ans += stk.size(); } else if(str.charAt(i-1) == ')') { ans++; stk.pop(); } } else if(temp == '(') stk.push(temp); } return ans; } pu.. [Solved]/코딩테스트 2023.03.13
크레인 인형뽑기 (Stack) import java.util.Scanner; import java.util.Stack; public class Main { public int solution(int l, int n, int[][] board, int[] moves) { int ans = 0; Stack stk = new Stack(); for(int x : moves) { for(int i = 0; i < l; i++) { if(board[i][x-1] != 0) { int temp = board[i][x-1]; board[i][x-1] = 0; if(stk.isEmpty()) { stk.push(temp); } else if(!stk.isEmpty() && stk.peek()==temp) { stk.pop(); ans += 2; }.. [Solved]/코딩테스트 2023.03.11