Skip to content

Commit 8f54eff

Browse files
authored
30주차 (#26)
1 parent a098310 commit 8f54eff

File tree

8 files changed

+667
-0
lines changed

8 files changed

+667
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package org.example._30week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.StringTokenizer;
9+
10+
public class CctvProblem {
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
private static int[][] map;
15+
private static List<CCTV> cctvs;
16+
private static int rowSize;
17+
private static int colSize;
18+
private static int emptySize;
19+
20+
private static final int WALL = 6;
21+
22+
private static int[] dr = {-1, 0, 1, 0};
23+
private static int[] dc = {0, 1, 0, -1};
24+
25+
private static int blindSpotSize = Integer.MAX_VALUE;
26+
27+
public static void main(String[] args) throws IOException {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
rowSize = Integer.parseInt(st.nextToken());
30+
colSize = Integer.parseInt(st.nextToken());
31+
emptySize = 0;
32+
33+
map = new int[rowSize][colSize];
34+
cctvs = new ArrayList<>();
35+
36+
for (int row = 0; row < rowSize; row++) {
37+
st = new StringTokenizer(br.readLine());
38+
for (int col = 0; col < colSize; col++) {
39+
map[row][col] = Integer.parseInt(st.nextToken());
40+
if (map[row][col] == 0) {
41+
emptySize++;
42+
} else if (map[row][col] >= 1 && map[row][col] <= 5) {
43+
cctvs.add(new CCTV(row, col, map[row][col]));
44+
}
45+
}
46+
}
47+
48+
boolean[][] flags = new boolean[rowSize][colSize];
49+
dfs(0, 0, flags);
50+
51+
System.out.println(blindSpotSize);
52+
}
53+
54+
private static void dfs(int index, int count, boolean[][] flags) {
55+
if (cctvs.size() == index) {
56+
blindSpotSize = Math.min(blindSpotSize, emptySize - count);
57+
return;
58+
}
59+
60+
CCTV cctv = cctvs.get(index);
61+
for (int[] direction : cctv.directions()) {
62+
boolean[][] newFlags = copyFlags(flags);
63+
int tempCount = count;
64+
for (int dir : direction) {
65+
int row = cctv.row;
66+
int col = cctv.col;
67+
68+
while (true) {
69+
row = row + dr[dir];
70+
col = col + dc[dir];
71+
72+
if (row < 0 || row >= rowSize || col < 0 || col >= colSize) {
73+
break;
74+
}
75+
76+
if (map[row][col] == WALL) {
77+
break;
78+
}
79+
80+
if (map[row][col] >= 1 && map[row][col] <= 5) {
81+
continue;
82+
}
83+
84+
if (newFlags[row][col]) {
85+
continue;
86+
}
87+
88+
newFlags[row][col] = true;
89+
tempCount++;
90+
}
91+
}
92+
93+
dfs(index + 1, tempCount, newFlags);
94+
}
95+
}
96+
97+
private static boolean[][] copyFlags(boolean[][] flags) {
98+
boolean[][] newFlags = new boolean[rowSize][colSize];
99+
100+
for (int row = 0; row < rowSize; row++) {
101+
for (int col = 0; col < colSize; col++) {
102+
newFlags[row][col] = flags[row][col];
103+
}
104+
}
105+
106+
return newFlags;
107+
}
108+
109+
private static class CCTV {
110+
private static final int[][] one = {{0}, {1}, {2}, {3}};
111+
private static final int[][] two = {{0, 2}, {1, 3}};
112+
private static final int[][] three = {{0, 1}, {1, 2}, {2, 3}, {3, 0}};
113+
private static final int[][] four = {{3, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 0}};
114+
private static final int[][] five = {{0, 1, 2, 3}};
115+
116+
int row;
117+
int col;
118+
int no;
119+
120+
public CCTV(int row, int col, int no) {
121+
this.row = row;
122+
this.col = col;
123+
this.no = no;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "CCTV{" +
129+
"row=" + row +
130+
", col=" + col +
131+
", no=" + no +
132+
'}';
133+
}
134+
135+
public int[][] directions() {
136+
switch (no) {
137+
case 1:
138+
return one;
139+
case 2:
140+
return two;
141+
case 3:
142+
return three;
143+
case 4:
144+
return four;
145+
case 5:
146+
return five;
147+
default:
148+
return null;
149+
}
150+
}
151+
152+
public int directionSize() {
153+
switch (no) {
154+
case 1:
155+
return one.length;
156+
case 2:
157+
return two.length;
158+
case 3:
159+
return three.length;
160+
case 4:
161+
return four.length;
162+
case 5:
163+
return five.length;
164+
default:
165+
return 0;
166+
}
167+
}
168+
}
169+
170+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.example._30week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class DoYouWannaBuildASnowman {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static int[] numbers;
12+
13+
private static int answer = Integer.MAX_VALUE;
14+
15+
public static void main(String[] args) throws IOException {
16+
int N = Integer.parseInt(br.readLine());
17+
numbers = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
18+
19+
List<Snowman> snowmen = new ArrayList<>(180_000);
20+
21+
for (int i = 0; i < N; i++) {
22+
for (int j = i + 1; j < N; j++) {
23+
snowmen.add(new Snowman(i, j));
24+
}
25+
}
26+
27+
Collections.sort(snowmen);
28+
29+
int snowmanSize = snowmen.size();
30+
for (int target = 0; target < snowmanSize; target++) {
31+
Snowman targetSnowman = snowmen.get(target);
32+
33+
for (int i = target + 1; i < snowmanSize; i++) {
34+
Snowman snowman = snowmen.get(i);
35+
36+
boolean hasDuplication = targetSnowman.hasDuplication(snowman);
37+
if (!hasDuplication) {
38+
answer = Math.min(answer, Math.abs(targetSnowman.getSize() - snowman.getSize()));
39+
break;
40+
}
41+
}
42+
}
43+
44+
System.out.println(answer);
45+
}
46+
47+
private static class Snowman implements Comparable<Snowman> {
48+
int x;
49+
int y;
50+
int sum;
51+
52+
public Snowman(int x, int y) {
53+
this.x = x;
54+
this.y = y;
55+
this.sum = numbers[x] + numbers[y];
56+
}
57+
58+
public boolean hasDuplication(Snowman other) {
59+
return other.x == this.x || other.x == this.y || other.y == this.x || other.y == this.y;
60+
}
61+
62+
public int getSize() {
63+
return numbers[x] + numbers[y];
64+
}
65+
66+
@Override
67+
public int compareTo(Snowman o) {
68+
return Integer.compare(this.sum, o.sum);
69+
}
70+
}
71+
72+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.example._30week;
2+
3+
import java.util.Arrays;
4+
5+
public class LongestCommonPrefix {
6+
7+
public static void main(String[] args) {
8+
// String[] strs = {"flower", "flow", "flight"};
9+
String[] strs = {"dog", "racecar", "car"};
10+
11+
String prefix = longestCommonPrefix(strs);
12+
System.out.println(prefix);
13+
}
14+
15+
public static String longestCommonPrefix(String[] strs) {
16+
int minLength = Arrays.stream(strs).mapToInt(String::length).min().getAsInt();
17+
18+
StringBuilder prefix = new StringBuilder();
19+
for (int index = 0; index < minLength; index++) {
20+
char ch = strs[0].charAt(index);
21+
22+
for (int i = 0; i < strs.length; i++) {
23+
if (strs[i].charAt(index) != ch) {
24+
return prefix.toString();
25+
}
26+
}
27+
28+
prefix.append(ch);
29+
}
30+
31+
return prefix.toString();
32+
}
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.example._30week;
2+
3+
import java.util.*;
4+
5+
class MergeIntervals {
6+
private static Set<Integer> zeroIntervals;
7+
8+
public int[][] merge(int[][] intervals) {
9+
List<int[]> answers = new ArrayList<>();
10+
zeroIntervals = new HashSet<>();
11+
int[] checkPoints = new int[10_001];
12+
13+
for (int[] interval : intervals) {
14+
if (interval[0] == interval[1]) {
15+
zeroIntervals.add(interval[0]);
16+
continue;
17+
}
18+
19+
checkPoints[interval[0]]++;
20+
checkPoints[interval[1]]--;
21+
}
22+
23+
// for (int i = 0; i < 19; i++) {
24+
// System.out.print(checkPoints[i]+" ");
25+
// }
26+
// System.out.println();
27+
28+
int startCount = 0;
29+
int startIndex = 0;
30+
for (int i = 0; i < 10_001; i++) {
31+
int value = checkPoints[i];
32+
33+
if (value > 0) {
34+
if (startCount == 0) {
35+
startIndex = i;
36+
}
37+
startCount += value;
38+
} else if (value < 0) {
39+
startCount += value;
40+
if (startCount == 0) {
41+
answers.add(new int[]{startIndex, i});
42+
}
43+
} else {
44+
if (startCount == 0) {
45+
boolean contains = zeroIntervals.contains(i);
46+
47+
if (contains) {
48+
answers.add(new int[]{i, i});
49+
}
50+
}
51+
}
52+
}
53+
54+
return answers.stream().toArray(int[][]::new);
55+
}
56+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.example._30week;
2+
3+
import java.util.Arrays;
4+
5+
public class RemoveDuplicatesFromSortedArray {
6+
7+
public static void main(String[] args) {
8+
// int[] nums = {1, 1, 2};
9+
int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
10+
11+
int answer = removeDuplicates(nums);
12+
System.out.println("size = " + answer);
13+
Arrays.stream(nums).forEach(e -> System.out.print(e + ", "));
14+
}
15+
16+
public static int removeDuplicates(int[] nums) {
17+
int index = 0;
18+
int prev = 1000;
19+
20+
for (int i = 0; i < nums.length; i++) {
21+
if (nums[i] != prev) {
22+
nums[index] = nums[i];
23+
index++;
24+
prev = nums[i];
25+
}
26+
}
27+
28+
return index;
29+
}
30+
}

0 commit comments

Comments
 (0)