Skip to content

Commit 0f3ec41

Browse files
authored
Merge pull request #48 from KodaHye/main
[4주차] 고다혜
2 parents 98d6d76 + 32cc5c6 commit 0f3ec41

10 files changed

+398
-0
lines changed

BOJ/1000-10000번/DH_1477.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
휴게소 세우기
6+
*/
7+
8+
public class DH_1477 {
9+
static int N, M, L;
10+
static int[] arr;
11+
12+
static int binarySearch() {
13+
int s = 1, e = L;
14+
15+
// lowerIdx 구해야 됨
16+
while(s < e) {
17+
int m = (s + e) / 2;
18+
19+
// m 길이 간격으로 휴게소 추가하기
20+
// 휴게소 개수 구하기
21+
int restCnt = getRestCnt(m);
22+
23+
// 추가할 수 있는 개수가 설치해야되는 개수보다 많다면, 길이 늘리기
24+
if(restCnt > M) s = m + 1;
25+
else e = m;
26+
}
27+
28+
return s;
29+
}
30+
31+
static int getRestCnt(int dis) {
32+
int cnt = 0;
33+
for(int i = 0; i < N + 1; i++) {
34+
int current = i == N ? L: arr[i];
35+
int prev = i == 0 ? 0: arr[i - 1];
36+
37+
cnt += ((current - prev - 1) / dis);
38+
}
39+
40+
return cnt;
41+
}
42+
public static void main(String[] args) throws Exception {
43+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
44+
StringTokenizer st = new StringTokenizer(br.readLine());
45+
46+
N = Integer.parseInt(st.nextToken());
47+
M = Integer.parseInt(st.nextToken());
48+
L = Integer.parseInt(st.nextToken());
49+
50+
arr = new int[N];
51+
52+
st = new StringTokenizer(br.readLine());
53+
for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());
54+
Arrays.sort(arr);
55+
56+
System.out.println(binarySearch());
57+
}
58+
}

BOJ/1000-10000번/DH_4386.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
별자리 만들기
6+
*/
7+
8+
public class DH_4386 {
9+
static class Point {
10+
double r, c;
11+
public Point(double r, double c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
}
16+
17+
static class Edge {
18+
int s, e;
19+
double w;
20+
public Edge(int s, int e, double w) {
21+
this.s = s;
22+
this.e = e;
23+
this.w = w;
24+
}
25+
}
26+
27+
static int N;
28+
static int[] p, rank;
29+
static Point[] points;
30+
static PriorityQueue<Edge> pq;
31+
32+
static void mst() {
33+
int cnt = 0;
34+
double result = 0;
35+
36+
while(!pq.isEmpty() && cnt < N - 1) {
37+
Edge current = pq.poll();
38+
39+
if(find(current.e) == find(current.s)) continue;
40+
union(current.e, current.s);
41+
result += current.w;
42+
cnt++;
43+
}
44+
45+
System.out.println(((int) ((result * 100)) * 1.0) / 100);
46+
}
47+
48+
// rank를 사용한 union
49+
static void union(int a, int b) {
50+
a = find(a);
51+
b = find(b);
52+
53+
// rank가 작은 것을 큰 쪽으로 붙이기
54+
if(rank[a] < rank[b]) p[a] = b;
55+
else if(rank[a] > rank[b]) p[b] = a;
56+
// rank가 같다면, 한 쪽 rank 늘려주고 거기에 붙이기
57+
else {
58+
p[b] = a;
59+
rank[a]++;
60+
}
61+
}
62+
63+
// find: 경로 압축
64+
static int find(int a) {
65+
return p[a] = a == p[a] ? a: find(p[a]);
66+
}
67+
68+
// 입력받은 별의 위치에 대해 모든 경우의 수를 pq에 넣어줌
69+
static void setQ() {
70+
for(int i = 0; i < points.length; i++) {
71+
for(int j = i + 1; j < points.length; j++) {
72+
pq.add(new Edge(i, j, getDis(i, j)));
73+
}
74+
}
75+
}
76+
77+
// 두 별 사이 거리를 구하는 함수
78+
static double getDis(int i, int j) {
79+
double diffR = Math.pow(points[i].r - points[j].r, 2);
80+
double diffC = Math.pow(points[i].c - points[j].c, 2);
81+
82+
return Math.sqrt(diffR + diffC);
83+
}
84+
85+
public static void main(String[] args) throws Exception {
86+
initInput();
87+
setQ();
88+
mst();
89+
}
90+
91+
static void initInput() throws Exception {
92+
93+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
94+
StringTokenizer st;
95+
N = Integer.parseInt(br.readLine());
96+
97+
points = new Point[N];
98+
p = new int[N];
99+
rank = new int[N];
100+
101+
// pq = new PriorityQueue<>(Comparator.comparing(o -> o.w));
102+
// pq = new PriorityQueue<>((o1, o2) -> Double.compare(o1.w, o2.w));
103+
pq = new PriorityQueue<>(Comparator.comparingDouble(o -> o.w));
104+
105+
for(int i = 0; i < p.length; i++) p[i] = i;
106+
107+
for(int i = 0; i < N; i++) {
108+
st = new StringTokenizer(br.readLine());
109+
double r = Double.parseDouble(st.nextToken());
110+
double c = Double.parseDouble(st.nextToken());
111+
112+
points[i] = new Point(r, c);
113+
}
114+
}
115+
}

BOJ/1000-10000번/DH_7570.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
줄세우기
6+
*/
7+
8+
public class DH_7570 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st;
12+
13+
int N = Integer.parseInt(br.readLine());
14+
int[] dp = new int[N + 1];
15+
int max = 0;
16+
17+
st = new StringTokenizer(br.readLine());
18+
for(int i = 0; i < N; i++) {
19+
int num = Integer.parseInt(st.nextToken());
20+
// dp[num] = dp[num - 1] != 0 ? dp[num - 1] + 1 : 1;
21+
dp[num] = dp[num - 1] + 1;
22+
max = Math.max(dp[num], max);
23+
}
24+
25+
System.out.println(N - max);
26+
27+
}
28+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
토스트 계란틀
6+
*/
7+
8+
public class DH_토스트_계란틀 {
9+
static class Point {
10+
int r, c;
11+
public Point(int r, int c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
}
16+
static int N, L, R;
17+
static int[][] map;
18+
static boolean[][] v;
19+
static int[] dr = {1, 0, -1, 0}, dc = {0, 1, 0, -1};
20+
// tmp: 방문한 위치들을 저장할 임시 큐
21+
static ArrayDeque<Point> q, tmp;
22+
23+
static void solution() {
24+
int turn = 0;
25+
while(canMove()) turn++;
26+
System.out.println(turn);
27+
}
28+
29+
static boolean canMove() {
30+
v = new boolean[N][N];
31+
boolean flag = false;
32+
33+
for(int r = 0; r < N; r++) {
34+
35+
int sum, cnt;
36+
for(int c = 0; c < N; c++) {
37+
if(v[r][c]) continue;
38+
sum = map[r][c];
39+
cnt = 1;
40+
41+
q.add(new Point(r, c));
42+
tmp.add(new Point(r, c));
43+
v[r][c] = true;
44+
while(!q.isEmpty()) {
45+
Point current = q.poll();
46+
tmp.add(current);
47+
48+
for(int d = 0; d < 4; d++) {
49+
int nr = current.r + dr[d];
50+
int nc = current.c + dc[d];
51+
52+
if(!check(nr, nc) || v[nr][nc] || !isInRange(map[current.r][current.c], map[nr][nc])) continue;
53+
v[nr][nc] = true;
54+
q.add(new Point(nr, nc));
55+
sum += map[nr][nc];
56+
cnt++;
57+
flag = true;
58+
}
59+
}
60+
61+
while(!tmp.isEmpty()) {
62+
Point current = tmp.poll();
63+
map[current.r][current.c] = sum / cnt;
64+
}
65+
}
66+
67+
}
68+
return flag;
69+
}
70+
71+
static boolean isInRange(int a, int b) {
72+
return Math.abs(a - b) >= L && Math.abs(a - b) <= R;
73+
}
74+
static boolean check(int r, int c) {
75+
return r >= 0 && r < N && c >= 0 && c < N;
76+
}
77+
78+
public static void main(String[] args) throws Exception {
79+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
80+
StringTokenizer st = new StringTokenizer(br.readLine());
81+
82+
N = Integer.parseInt(st.nextToken());
83+
L = Integer.parseInt(st.nextToken());
84+
R = Integer.parseInt(st.nextToken());
85+
86+
map = new int[N][N];
87+
q = new ArrayDeque<>();
88+
tmp = new ArrayDeque<>();
89+
90+
for(int r = 0; r < N; r++) {
91+
st = new StringTokenizer(br.readLine());
92+
for(int c = 0; c < N; c++) {
93+
map[r][c] = Integer.parseInt(st.nextToken());
94+
}
95+
}
96+
97+
solution();
98+
}
99+
}

Programmers/Level2/DH_42885.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Programmers.Level2;
2+
3+
import java.util.*;
4+
5+
/*
6+
구명보트
7+
*/
8+
9+
public class DH_42885 {
10+
class Solution {
11+
public int solution(int[] people, int limit) {
12+
int answer = 0;
13+
14+
Deque<Integer> q = new ArrayDeque<>();
15+
16+
Arrays.sort(people); // people 배열을 정렬 후 deque에 담음
17+
for (int p : people) q.add(p);
18+
19+
while (!q.isEmpty()) {
20+
// pollLast를 하며 숫자가 큰 것부터 처리
21+
int current = q.pollLast();
22+
// 제한을 넘는다면 continue
23+
if (current > limit) continue;
24+
25+
answer++;
26+
27+
if (!q.isEmpty()) {
28+
// 큐에 있는 제일 큰 값과, 제일 작은 값을 비교하며
29+
// 구명보트 태우기
30+
if (current + q.peekLast() <= limit) q.pollLast();
31+
else if (current + q.peekFirst() <= limit) q.pollFirst();
32+
}
33+
34+
}
35+
return answer;
36+
}
37+
}
38+
}

Programmers/Level3/DH_42898.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Programmers.Level3;
2+
3+
/*
4+
등굣길
5+
*/
6+
7+
class DH_42898 {
8+
static final int MOD = 1_000_000_007;
9+
static int[] dr = {0, -1}, dc = {-1, 0};
10+
static int[][] dp; // 해당 지점까지 갈 수 있는 경우의 수를 저장하는 변수
11+
static boolean[][] existPuddle; // 물에 잠긴 지역 확이하는 변수
12+
13+
static boolean check(int r, int c) {
14+
return r >= 0 && r < dp.length && c >= 0 && c < dp[0].length;
15+
}
16+
17+
public int solution(int m, int n, int[][] puddles) {
18+
existPuddle = new boolean[n][m];
19+
20+
// 물에 잠긴 지역 체크
21+
for(int[] p: puddles) existPuddle[p[1] - 1][p[0] - 1] = true;
22+
23+
dp = new int[n][m];
24+
25+
dp[0][0] = 1;
26+
for(int r = 0; r < n; r++) {
27+
for(int c = 0; c < m; c++) {
28+
for(int d = 0; d < 2; d++) {
29+
int prevR = r + dr[d];
30+
int prevC = c + dc[d];
31+
32+
// 해당 지점 기준 가능한 이전 지점에서의 가지수 더해주기
33+
if(!check(prevR, prevC) || existPuddle[prevR][prevC]) continue;
34+
dp[r][c] = (dp[r][c] + dp[prevR][prevC]) % MOD;
35+
}
36+
}
37+
}
38+
39+
return dp[n - 1][m - 1];
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://school.programmers.co.kr/learn/courses/30/lessons/284531
2+
# ROUND(숫자, 1): 소숫점 아래 두 번째 자리에서 반올림해서 첫번째까지만 보이기
3+
# CONCAT(STR1, STR2): 두 개의 문자 붙이기
4+
SELECT ROUTE
5+
, CONCAT(ROUND(SUM(D_BETWEEN_DIST), 1), 'km') `TOTAL_DISTANCE`
6+
, CONCAT(ROUND(AVG(D_BETWEEN_DIST), 2), 'km') `AVERAGE_DISTANCE`
7+
FROM SUBWAY_DISTANCE
8+
GROUP BY ROUTE
9+
ORDER BY ROUND(SUM(D_BETWEEN_DIST), 1) DESC

0 commit comments

Comments
 (0)