Skip to content

Commit a098310

Browse files
authored
29주차
1 parent 700c085 commit a098310

File tree

6 files changed

+311
-2
lines changed

6 files changed

+311
-2
lines changed

src/main/java/org/example/_20week/PathSum3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private static TreeNode initialize() {
123123

124124
public static class TreeNode {
125125
int val;
126-
TreeNode left;
127-
TreeNode right;
126+
public TreeNode left;
127+
public TreeNode right;
128128

129129
TreeNode() {
130130
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example._29week;
2+
3+
class BinaryTreeMaximumPathSum {
4+
private static int max;
5+
6+
public int maxPathSum(TreeNode root) {
7+
max = Integer.MIN_VALUE;
8+
int lastValue = calculate(root);
9+
max = Math.max(max,lastValue);
10+
11+
return max;
12+
}
13+
14+
private int calculate(TreeNode root){
15+
if(root == null){
16+
return 0;
17+
}
18+
19+
int leftSum = calculate(root.left);
20+
leftSum = leftSum > 0 ? leftSum : 0;
21+
22+
int rightSum = calculate(root.right);
23+
rightSum = rightSum > 0 ? rightSum : 0;
24+
25+
int value = leftSum + rightSum + root.val;
26+
max = Math.max(max,value);
27+
28+
return Math.max(leftSum,rightSum) + root.val;
29+
}
30+
31+
public class TreeNode {
32+
int val;
33+
TreeNode left;
34+
TreeNode right;
35+
TreeNode() {}
36+
TreeNode(int val) { this.val = val; }
37+
TreeNode(int val, TreeNode left, TreeNode right) {
38+
this.val = val;
39+
this.left = left;
40+
this.right = right;
41+
}
42+
}
43+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.example._29week;
2+
3+
import java.util.*;
4+
5+
class ConnectingIland {
6+
public int solution(int n, int[][] costs) {
7+
int[] parents = new int[n];
8+
Arrays.setAll(parents, idx -> idx);
9+
10+
int edgeCount = 0;
11+
int cost = 0;
12+
13+
Arrays.sort(costs, (e1, e2) -> Integer.compare(e1[2], e2[2]));
14+
// for (int i = 0; i < costs.length; i++) {
15+
// System.out.println(costs[i][0] + ", " + costs[i][1] + " : " + costs[i][2]);
16+
// }
17+
18+
for (int i = 0; edgeCount < n - 1; i++) {
19+
boolean isSameGraph = isSameGraph(parents, costs[i][0], costs[i][1]);
20+
if (isSameGraph) {
21+
continue;
22+
}
23+
24+
union(parents, costs[i][0], costs[i][1]);
25+
cost += costs[i][2];
26+
edgeCount++;
27+
}
28+
29+
return cost;
30+
}
31+
32+
private static boolean isSameGraph(int[] parents, int x, int y) {
33+
int parentX = getParent(parents, x);
34+
int parentY = getParent(parents, y);
35+
36+
return parentX == parentY;
37+
}
38+
39+
private static int getParent(int[] parents, int x) {
40+
if (parents[x] == x) {
41+
return x;
42+
}
43+
44+
return parents[x] = getParent(parents, parents[x]);
45+
}
46+
47+
private static void union(int[] parents, int x, int y) {
48+
x = getParent(parents, x);
49+
y = getParent(parents, y);
50+
51+
if (x < y) {
52+
parents[y] = x;
53+
} else {
54+
parents[x] = y;
55+
}
56+
}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example._29week;
2+
3+
import org.example._20week.PathSum3;
4+
5+
public class CountCompleteTreeNodes {
6+
7+
public int countNodes(PathSum3.TreeNode root) {
8+
if(root==null){
9+
return 0;
10+
}
11+
12+
return countNodes(root.right) + countNodes(root.left) + 1;
13+
}
14+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.example._29week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
public class OperatorInserting {
10+
11+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
private static int max = Integer.MIN_VALUE;
13+
private static int min = Integer.MAX_VALUE;
14+
15+
private static int[] numbers;
16+
17+
public static void main(String[] args) throws IOException {
18+
int numberCount = Integer.parseInt(br.readLine());
19+
numbers = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
20+
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
int plusCount = Integer.parseInt(st.nextToken());
23+
int minusCount = Integer.parseInt(st.nextToken());
24+
int multiplyCount = Integer.parseInt(st.nextToken());
25+
int divisionCount = Integer.parseInt(st.nextToken());
26+
27+
dfs(numbers[0], 1, plusCount, minusCount, multiplyCount, divisionCount);
28+
29+
System.out.println(max);
30+
System.out.println(min);
31+
}
32+
33+
private static void dfs(int number, int index, int plusCount, int minusCount, int multiplyCount, int divisionCount) {
34+
if (plusCount == 0 && minusCount == 0 && multiplyCount == 0 && divisionCount == 0) {
35+
max = Math.max(max, number);
36+
min = Math.min(min, number);
37+
}
38+
39+
if (plusCount > 0) {
40+
dfs(number + numbers[index], index + 1, plusCount - 1, minusCount, multiplyCount, divisionCount);
41+
}
42+
43+
if (minusCount > 0) {
44+
dfs(number - numbers[index], index + 1, plusCount, minusCount - 1, multiplyCount, divisionCount);
45+
}
46+
47+
if (multiplyCount > 0) {
48+
dfs(number * numbers[index], index + 1, plusCount, minusCount, multiplyCount - 1, divisionCount);
49+
}
50+
51+
if (divisionCount > 0) {
52+
dfs(number / numbers[index], index + 1, plusCount, minusCount, multiplyCount, divisionCount - 1);
53+
}
54+
}
55+
}
56+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package org.example._29week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class SnakeMaze {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
private static final int[] dr = {0, 1, 0, -1};
13+
private static final int[] dc = {1, 0, -1, 0};
14+
15+
private static final int APPLE = -1;
16+
private static final int EXIT = -1;
17+
private static final int SNAKE = 1;
18+
private static final int EMPTY = 0;
19+
20+
private static int[][] map;
21+
private static Map<Integer, String> commands = new HashMap<>();
22+
private static int mapSize;
23+
24+
public static void main(String[] args) throws IOException {
25+
mapSize = Integer.parseInt(br.readLine());
26+
map = new int[mapSize + 1][mapSize + 1];
27+
28+
int appleCount = Integer.parseInt(br.readLine());
29+
for (int i = 0; i < appleCount; i++) {
30+
StringTokenizer st = new StringTokenizer(br.readLine());
31+
int row = Integer.parseInt(st.nextToken());
32+
int col = Integer.parseInt(st.nextToken());
33+
34+
map[row][col] = APPLE;
35+
}
36+
37+
int commandCount = Integer.parseInt(br.readLine());
38+
for (int i = 0; i < commandCount; i++) {
39+
StringTokenizer st = new StringTokenizer(br.readLine());
40+
int second = Integer.parseInt(st.nextToken());
41+
String command = st.nextToken();
42+
43+
commands.put(second, command);
44+
}
45+
46+
Snake snake = new Snake();
47+
for (int i = 1; ; i++) {
48+
if (snake.moveHead() == EXIT) {
49+
System.out.println(i);
50+
return;
51+
}
52+
53+
String command = commands.get(i);
54+
if (command != null) {
55+
snake.rotate(command);
56+
}
57+
}
58+
59+
}
60+
61+
private static class Snake {
62+
List<Position> snakes;
63+
int dir;
64+
65+
public Snake() {
66+
snakes = new ArrayList<>();
67+
snakes.add(new Position(1, 1));
68+
map[1][1] = SNAKE;
69+
70+
this.dir = 0;
71+
}
72+
73+
public int moveHead() {
74+
int headIndex = snakes.size() - 1;
75+
int nextRow = snakes.get(headIndex).row + dr[dir];
76+
int nextCol = snakes.get(headIndex).col + dc[dir];
77+
78+
if (nextRow <= 0 || nextRow > mapSize || nextCol <= 0 || nextCol > mapSize) {
79+
return EXIT;
80+
}
81+
82+
if (map[nextRow][nextCol] == SNAKE) {
83+
return EXIT;
84+
}
85+
86+
if (map[nextRow][nextCol] != APPLE) {
87+
moveTail();
88+
}
89+
90+
snakes.add(new Position(nextRow, nextCol));
91+
map[nextRow][nextCol] = SNAKE;
92+
return 0;
93+
}
94+
95+
private void moveTail() {
96+
Position tail = snakes.get(0);
97+
snakes.remove(0);
98+
map[tail.row][tail.col] = EMPTY;
99+
}
100+
101+
public void rotate(String command) {
102+
switch (command) {
103+
case "L":
104+
rotateLeft();
105+
break;
106+
case "D":
107+
rotateRight();
108+
break;
109+
default:
110+
break;
111+
}
112+
}
113+
114+
private void rotateLeft() {
115+
dir = dir == 0 ? 3 : dir - 1;
116+
}
117+
118+
private void rotateRight() {
119+
dir = dir == 3 ? 0 : dir + 1;
120+
}
121+
}
122+
123+
private static class Position {
124+
int row;
125+
int col;
126+
127+
public Position(int row, int col) {
128+
this.row = row;
129+
this.col = col;
130+
}
131+
132+
@Override
133+
public String toString() {
134+
return "Position{" +
135+
"row=" + row +
136+
", col=" + col +
137+
'}';
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)