Skip to content

Commit 5177339

Browse files
committed
48주차
1 parent 17e775b commit 5177339

File tree

5 files changed

+333
-0
lines changed

5 files changed

+333
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.example._48week;
2+
3+
public class CrossStones {
4+
5+
public static void main(String[] args) {
6+
int[] stones = {2, 4, 5, 3, 2, 1, 4, 2, 5, 1};
7+
int k = 3;
8+
9+
System.out.println(solution(stones, k));
10+
}
11+
12+
public static int solution(int[] stones, int k) {
13+
int s = 0;
14+
int e = 200_000_000;// 2억
15+
16+
// O(NlogN)
17+
while (s < e) {
18+
int mid = s + (e - s) / 2;
19+
20+
if (isPassable(stones, mid,k)) {
21+
s = mid + 1;
22+
} else {
23+
e = mid;
24+
}
25+
}
26+
27+
return s;
28+
}
29+
30+
// O(N)
31+
private static boolean isPassable(int[] stones, int peopleCnt, int k) {
32+
33+
int cnt = 0;
34+
for (int i = 0; i < stones.length; i++) {
35+
if (stones[i] > peopleCnt) {
36+
cnt=0;
37+
continue;
38+
}
39+
40+
cnt++;
41+
if (cnt == k) {
42+
return false;
43+
}
44+
}
45+
46+
return true;
47+
}
48+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.example._48week;
2+
3+
class LockAndKey {
4+
private static int N = 0;
5+
private static int M = 0;
6+
7+
public static void main(String[] args) {
8+
int[][] key = {{0, 0, 0}, {1, 0, 0}, {0, 1, 1}};
9+
int[][] lock = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}};
10+
11+
boolean solution = solution(key, lock);
12+
System.out.println(solution);
13+
}
14+
public static boolean solution(int[][] key, int[][] lock) {
15+
N = lock.length;
16+
M = key.length;
17+
18+
int[][][] keys = makeRotationKeys(key);
19+
int[][] newLock = makeExtensionLock(lock);
20+
21+
for (int row = 0; row < newLock.length; row++) {
22+
for (int col = 0; col < newLock.length; col++) {
23+
for (int rotation = 0; rotation < 4; rotation++) {
24+
boolean isMatch = isMatch(keys[rotation], newLock, row, col);
25+
if (isMatch) {
26+
return true;
27+
}
28+
}
29+
}
30+
}
31+
32+
return false;
33+
}
34+
35+
public static int[][][] makeRotationKeys(int[][] key) {
36+
int[][][] keys = new int[4][M][M];
37+
keys[0] = key;
38+
39+
for (int i = 1; i < 4; i++) {
40+
for (int row = 0; row < key.length; row++) {
41+
for (int col = 0; col < key[0].length; col++) {
42+
keys[i][col][key.length-1-row] = keys[i - 1][row][col];
43+
}
44+
}
45+
}
46+
47+
return keys;
48+
}
49+
50+
private static int[][] makeExtensionLock(int[][] lock) {
51+
int newLength = N + 2 * M - 2;
52+
int[][] newLock = new int[newLength][newLength];
53+
54+
for (int row = 0; row < newLength; row++) {
55+
for (int col = 0; col < newLength; col++) {
56+
newLock[row][col] = 3;
57+
}
58+
}
59+
60+
for (int row = M - 1, originalRow = 0; row < M - 1 + N; row++, originalRow++) {
61+
for (int col = M - 1, originalCol = 0; col < M - 1 + N; col++, originalCol++) {
62+
newLock[row][col] = lock[originalRow][originalCol];
63+
}
64+
}
65+
66+
return newLock;
67+
}
68+
69+
public static boolean isMatch(int[][] key, int[][] lock, int rowIndex, int colIndex) {
70+
for (int row = M - 1; row < M - 1 + N; row++) {
71+
for (int col = M - 1; col < M - 1 + N; col++) {
72+
if (((row - rowIndex) < 0 || (row - rowIndex) >= M) || ((col - colIndex) < 0 || (col - colIndex) >= M)){
73+
if(lock[row][col]==0){
74+
return false;
75+
}
76+
77+
continue;
78+
}
79+
80+
if (lock[row][col] == 0 && key[row - rowIndex][col - colIndex] == 0) {
81+
return false;
82+
}
83+
84+
if (lock[row][col] == 1 && key[row - rowIndex][col - colIndex] == 1) {
85+
return false;
86+
}
87+
}
88+
}
89+
90+
return true;
91+
}
92+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.example._48week;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
public class MergeIntervals {
9+
10+
private static final int s = 0;
11+
private static final int e = 1;
12+
13+
public static void main(String[] args) {
14+
// int[][] intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
15+
int[][] intervals = {{1, 4}, {2, 3}};
16+
int[][] answer = merge(intervals);
17+
System.out.println("end");
18+
}
19+
20+
public static int[][] merge(int[][] intervals) {
21+
Arrays.sort(intervals, Comparator.comparingInt((int[] a) -> a[s]));
22+
List<int[]> newIntervals = new ArrayList<>();
23+
24+
int lastStart = intervals[0][s];
25+
int lastEnd = intervals[0][e];
26+
27+
for (int i = 1; i < intervals.length; i++) {
28+
if (lastEnd >= intervals[i][s]) {
29+
lastEnd = Math.max(lastEnd, intervals[i][e]);
30+
continue;
31+
}
32+
33+
newIntervals.add(new int[]{lastStart, lastEnd});
34+
lastStart = intervals[i][s];
35+
lastEnd = intervals[i][e];
36+
}
37+
38+
newIntervals.add(new int[]{lastStart, lastEnd});
39+
return newIntervals.stream()
40+
.toArray(int[][]::new);
41+
}
42+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.example._48week;
2+
3+
public class SharedTaxiFare {
4+
5+
private static final int INF = 100_000_000; // 1억
6+
7+
public static void main(String[] args) {
8+
// int n = 6;
9+
// int s = 4;
10+
// int a = 6;
11+
// int b = 2;
12+
// int[][] fares = {{4, 1, 10}, {3, 5, 24}, {5, 6, 2}, {3, 1, 41}, {5, 1, 24}, {4, 6, 50}, {2, 4, 66}, {2, 3, 22}, {1, 6, 25}};
13+
14+
int n = 7;
15+
int s = 3;
16+
int a = 4;
17+
int b = 1;
18+
int[][] fares = {{5, 7, 9}, {4, 6, 4}, {3, 6, 1}, {3, 2, 3}, {2, 1, 6}};
19+
20+
int solution = solution(n, s, a, b, fares);
21+
System.out.println(solution);
22+
}
23+
24+
// n: node개수
25+
// s: 시작 지점
26+
// a: A의 도착 지점
27+
// b: B의 도착 지점
28+
// fares: 지점 사이의 예상 택시 요금
29+
public static int solution(int n, int s, int a, int b, int[][] fares) {
30+
// make dp
31+
int[][] dp = initMap(n, fares);
32+
33+
// floyd warshall O(N^3)
34+
for (int k = 1; k < n + 1; k++) {
35+
for (int row = 1; row < n + 1; row++) {
36+
for (int col = 1; col < n + 1; col++) {
37+
dp[row][col] = Math.min(dp[row][col], dp[row][k] + dp[k][col]);
38+
}
39+
}
40+
}
41+
42+
int answer = Integer.MAX_VALUE;
43+
for (int i = 1; i < n + 1; i++) {
44+
answer = Math.min(answer, dp[s][i] + dp[i][a] + dp[i][b]);
45+
}
46+
47+
return answer;
48+
}
49+
50+
private static int[][] initMap(int n, int[][] fares) {
51+
int[][] dp = new int[n + 1][n + 1];
52+
53+
for (int row = 1; row < n + 1; row++) {
54+
for (int col = 1; col < n + 1; col++) {
55+
if (row == col) {
56+
dp[row][col] = 0;
57+
} else {
58+
dp[row][col] = INF;
59+
}
60+
}
61+
}
62+
63+
for (int[] fare : fares) {
64+
int node1 = fare[0];
65+
int node2 = fare[1];
66+
int cost = fare[2];
67+
68+
dp[node1][node2] = cost;
69+
dp[node2][node1] = cost;
70+
}
71+
72+
return dp;
73+
}
74+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.example._48week;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.LocalTime;
5+
import java.time.format.DateTimeFormatter;
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
public class ShuttleBus {
10+
11+
public static void main(String[] args) {
12+
String[] arr = {"09:10", "09:09", "08:00"};
13+
String answer = solution(2,10,2, arr);
14+
15+
System.out.println(answer);
16+
}
17+
18+
// n: 셔틀 운행 횟수
19+
// t: 셔틀 운행 간격
20+
// m: 한 셔틀에 탈 수 있는 최대 크루원 수
21+
public static String solution(int n, int t, int m, String[] timetable) {
22+
Bus[] busses = new Bus[n];
23+
boolean[] full = new boolean[n];
24+
25+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
26+
LocalDateTime currentTime = LocalDateTime.now()
27+
.withHour(9)
28+
.withMinute(0)
29+
.withSecond(0)
30+
.minusMinutes(t);
31+
32+
Arrays.sort(timetable);
33+
34+
// busses 초기화
35+
for (int busIdx = 0; busIdx < n; busIdx++) {
36+
currentTime = currentTime.plusMinutes(t);
37+
busses[busIdx] = new Bus(currentTime.format(formatter));
38+
}
39+
40+
for (int timeIdx = 0, busIdx = 0; timeIdx < timetable.length && busIdx < n; timeIdx++) {
41+
if (timetable[timeIdx].compareTo(busses[busIdx].time) > 0) {
42+
busIdx++;
43+
timeIdx--;
44+
continue;
45+
}
46+
47+
busses[busIdx].cnt++;
48+
if (busses[busIdx].cnt >= m) {
49+
full[busIdx] = true;
50+
busIdx++;
51+
}
52+
}
53+
54+
for (int busIdx = busses.length - 1; busIdx >= 0; busIdx--) {
55+
if (!full[busIdx]) {
56+
return busses[busIdx].time;
57+
}
58+
}
59+
60+
LocalDateTime dateTime = LocalDateTime.of(
61+
LocalDateTime.now().toLocalDate(), // 오늘 날짜 가져오기
62+
LocalTime.parse(timetable[m-2], formatter) // 문자열을 LocalTime으로 변환
63+
);
64+
65+
return dateTime.minusMinutes(1).format(formatter);
66+
}
67+
68+
private static class Bus {
69+
String time;
70+
int cnt;
71+
72+
public Bus(String time) {
73+
this.time = time;
74+
this.cnt = 0;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)