Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[6주차] 이예진 #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jewan100 merged 11 commits into GreatAlgorithm-Study:main from yeahdy:main
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions BOJ/1000-5000번/YJ_2110.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.*;
import java.io.*;

public class YJ_2110 {
static int[] house;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] data = br.readLine().split("\\s");
int N = Integer.parseInt(data[0]);
int C = Integer.parseInt(data[1]);

house = new int[N];
for(int i=0; i<N; i++){
house[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(house);
System.out.println(binarySearch(C));
}

static int binarySearch(int C){
int left = 0;
int right = house[house.length-1] - house[0]; //최대간격

while(left <= right){
int mid = (left+right)/2; //★공유기를 설치할 최소거리 후보!

//공유기 설치하기
int builtCount = 1;
int current = house[0];
for(int i=1; i<house.length; i++){
if(house[i] - current >= mid){ //★거리 간격 >= mid 뜻은 house[i]가 mid 와 같은 위치에 있거나 mid 뒤에 있다는 뜻임
builtCount++;
current = house[i];
}
}

//간격 조정하면서 최적의 최대값 찾기
if(builtCount >= C){
left = mid + 1; //upperBound: mid 값을 높여서 간격을 넓힘
}else {
right = mid-1;
}
}
return right;
}
}
46 changes: 46 additions & 0 deletions BOJ/1000-5000번/YJ_2805.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.*;
import java.util.Arrays;

public class YJ_2805 {
static int[] trees;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] data = br.readLine().split("\\s");
int N = Integer.parseInt(data[0]);
int M = Integer.parseInt(data[1]);

trees = new int[N];
String[] t = br.readLine().split("\\s");
for(int i=0; i<t.length; i++){
trees[i] = Integer.parseInt(t[i]);
}

Arrays.sort(trees);
System.out.println(binarySearch(M));
}

static int binarySearch(int M){
int left = 0;
int right = trees[trees.length-1];

while(left <= right){
int mid = (left+right)/2;
long sum = 0;

for(int tree : trees){
int cutting = tree - mid;
if(cutting > 0){
sum += cutting;
}
}

if(sum >= M){
left = mid+1;
}else{
right = mid-1;
}
}

return right;
}
}
38 changes: 38 additions & 0 deletions BOJ/1000-5000번/YJ_4781.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.io.*;

public class YJ_4781 {
static int[] dp;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String line = br.readLine();
if("0 0.00".equals(line)){
break;
}
String[] data = line.split("\\s");
int n = Integer.parseInt(data[0]);
int money = stringToInt(data[1]);

dp = new int[money+1];
for(int i=1; i<n+1; i++){
String[] candy = br.readLine().split("\\s");
int calorie = Integer.parseInt(candy[0]);
int price = stringToInt(candy[1]);
knapsack(calorie,price,money);
}

System.out.println(dp[money]);
}
br.close();
}

static void knapsack(int calorie,int price,int money){
for (int m = price; m <= money; m++) {
dp[m] = Math.max(dp[m], calorie + dp[m - price]);
}
}

private static int stringToInt(String data) {
return Integer.parseInt(data.replace(".",""));
}
}
49 changes: 49 additions & 0 deletions BOJ/5001-10000번/YJ_7579.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.*;

public class YJ_7579 {
static int[] memories;
static int[] costs;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] data = br.readLine().split("\\s");
int N = Integer.parseInt(data[0]);
int M = Integer.parseInt(data[1]);

String[] nM = br.readLine().split("\\s");
String[] nC = br.readLine().split("\\s");
memories = new int[N];
costs = new int[N];
for(int i=0; i<N; i++){
memories[i] = Integer.parseInt(nM[i]);
costs[i] = Integer.parseInt(nC[i]);
}

System.out.println(getMinCostSum(N,M));
}

//내생각: dp배열의 값을 최소비용으로 두고 접근(X)
//1차원 dp배열을 최대비용으로 값을 메모리로 두고 접근(O)
static int getMinCostSum(int N, int M){
//dp[비용] = 메모리
int[] dp = new int[10001]; //최대 비용의 총합

for(int app=0; app<N; app++){
for(int c=10000; c>=costs[app]; c--){
//최소 비용으로 가질수있는 최대메모리(최대라면 항상 메모리를 충족함)
dp[c] = Math.max(dp[c], dp[c-costs[app]]+memories[app]);
//현재 메모리 vs (현재 메모리를 추가한다면) 이전 상태의 메모리★ + 현재메모리
}
}

int result = 0;
for(int i=0; i<dp.length; i++){
//최소 비용부터 탐색해서 적정메모리 이상이 있는지 조회
if(dp[i] >= M){
result = i;
break;
}
}

return result;
}
}
34 changes: 34 additions & 0 deletions BOJ/5001-10000번/YJ_9084.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.*;

public class YJ_9084 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int t=0; t<T; t++){
int N = Integer.parseInt(br.readLine());
int[] coins = new int[N];
String[] c = br.readLine().split("\\s");
for(int i=0; i<N; i++){
coins[i] = Integer.parseInt(c[i]);
}
int M = Integer.parseInt(br.readLine());

System.out.println(dp(coins,M));
}
}

static int dp(int[] coins, int M){
int[] dp = new int[M+1];
dp[0] = 1;

for(int coin : coins){
for(int i=0; i<M+1; i++){
if(coin>i){
continue;
}
dp[i] += dp[i-coin];
}
}
return dp[M];
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.io.*;

public class YJ_테트리스_블럭_안의_합_최대화_하기 {
static int n = 0;
static int m = 0;

static int[][] block;
static boolean[][] visited;
static int maxSum = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split("\\s");
n = Integer.parseInt(line[0]);
m = Integer.parseInt(line[1]);

block = new int[n][m];
for(int i=0; i<n; i++){
String[] t = br.readLine().split("\\s");
for(int j=0; j<m; j++){
block[i][j] = Integer.parseInt(t[j]);
}
}

visited = new boolean[n][m];
//블럭의 한칸마다 전체를 DFS 로 탐색 * 전체블럭 수
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
visited[i][j] = true;
dfs(1,i,j,block[i][j]);
visited[i][j] = false;
//ᅡᅩᅮᅥ 도형

}
}

System.out.println(maxSum);
}

//좌우상하
static int[] dx = {0,0,-1,1};
static int[] dy = {-1,1,0,0};
static void dfs(int depth, int x, int y, int sum){
if(depth == 4){
maxSum = Math.max(maxSum, sum);
return;
}

//상하좌우 모두 탐색
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];

if(stop(nx,ny)){
continue;
}
visited[nx][ny] = true;
if(depth == 2){
dfs(depth+1, x,y,sum+block[nx][ny]);
}
dfs(depth+1, nx, ny, sum + block[nx][ny]);
visited[nx][ny] = false;
}

}

private static boolean stop(int nx, int ny){
return nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny];
}
}
72 changes: 72 additions & 0 deletions Programmers/Level3/YJ_42861.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Arrays;
import java.util.Comparator;

public class YJ_42861 {
static class Bridge {
int x;
int y;
int cost;
Bridge(int x, int y, int cost){
this.x = x;
this.y = y;
this.cost = cost;
}
}

public static void main(String[] args) {
int n = 4;
int[][] costs = {{0,1,1},{0,2,2},{1,2,5},{1,3,1},{2,3,8}};
System.out.println(solution(n,costs));
}

static int solution(int n, int[][] costs) {
int length = costs.length;
Bridge[] bridgeArr = new Bridge[length];
for(int i=0; i<length; i++){
if(costs[i][2] == 0){
continue;
}
bridgeArr[i] = new Bridge(costs[i][0],costs[i][1],costs[i][2]);
}

Arrays.sort(bridgeArr, Comparator.comparingInt(o -> o.cost));

int[] parent = new int[length+1];
for(int i=1; i<length+1; i++){
parent[i] = i;
}

int minCost = 0;
int line = 0;
for(Bridge bridge : bridgeArr){
if(line == n-1){ //간선수는 n-1개
break;
}

if(find(parent, bridge.x) != find(parent, bridge.y)){
union(parent,bridge.x,bridge.y);
minCost += bridge.cost;
line++;
}
}
return minCost;
}

static int find(int[] parent, int num){
if(parent[num] == num){
return parent[num];
}
return parent[num] = find(parent,parent[num]);
}

static void union(int[] parent,int num1, int num2){
if(num1 == num2){
return;
}
if(num1 > num2){
parent[num1] = num2;
}else{
parent[num2] = num1;
}
}
}
29 changes: 29 additions & 0 deletions Programmers/Level3/YJ_43238.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class YJ_43238 {
public static void main(String[] args) {
int n = 6;
int[] times = {7, 10};
System.out.println(binarySearch(n,times));
}

static long binarySearch(int n, int[] times) {
int length = times.length;
long left = 0;
long right = (long) times[length-1]*n;

while(left<right){
long mid = (left+right)/2;

long count = 0;
for(int time : times){
count += mid/time;
}

if(count>=n){
right = mid;
}else{
left = mid+1;
}
}
return right;
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /