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

Commit 3a9bedc

Browse files
Merge pull request #196 from Jewan1120/main
[14주차] 백제완
2 parents 19a4d3f + 30eb21c commit 3a9bedc

File tree

8 files changed

+388
-0
lines changed

8 files changed

+388
-0
lines changed

‎BOJ/20001-25000번/JW_20181.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class JW_20181{
2+
3+
public static void main(String[] args) throws Exception {
4+
int n = read(), k = read();
5+
long[] arr = new long[n];
6+
for (int i = 0; i < n; i++)
7+
arr[i] = read();
8+
long[] dp = new long[n + 1];
9+
int l = 0, r = 0;
10+
long sum = 0;
11+
// 투 포인터 누적합
12+
while (r < n) {
13+
sum += arr[r];
14+
dp[r + 1] = dp[r];
15+
// sum이 k이상이면 r값을 결정
16+
// arr[l]을 빼도 계속 k이상일 수 있으므로 while로 반복 진행
17+
while (sum >= k) {
18+
// 이전 dp에서 현재 탈피 에너지로 최댓값 갱신
19+
dp[r + 1] = Math.max(dp[r + 1], dp[l] + sum - k);
20+
sum -= arr[l];
21+
l++;
22+
}
23+
r++;
24+
}
25+
System.out.println(dp[n]);
26+
}
27+
28+
private static int read() throws Exception {
29+
int c, n = System.in.read() & 15;
30+
while ((c = System.in.read()) >= 48)
31+
n = (n << 3) + (n << 1) + (c & 15);
32+
if (c == 13)
33+
System.in.read();
34+
return n;
35+
}
36+
}

‎BOJ/20001-25000번/JW_20291.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.TreeMap;
4+
5+
public class JW_20291 {
6+
7+
public static void main(String[] args) throws Exception {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int n = Integer.parseInt(br.readLine());
10+
TreeMap<String, Integer> tm = new TreeMap<>(); // 확장자를 정렬하고, 카운트하기 위한 트리맵
11+
for (int i = 0; i < n; i++) {
12+
String ext = br.readLine().split("\\.")[1]; // 확장자
13+
tm.put(ext, tm.getOrDefault(ext, 0) + 1); // 확장자 카운트
14+
}
15+
StringBuilder sb = new StringBuilder();
16+
// 조건에 맞게 출력
17+
for (String ext : tm.keySet())
18+
sb.append(ext).append(" ").append(tm.get(ext)).append("\n");
19+
System.out.println(sb);
20+
}
21+
}

‎BOJ/20001-25000번/JW_22944.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.StringTokenizer;
6+
7+
public class JW_22944 {
8+
9+
static int[] dy = { 1, -1, 0, 0 };
10+
static int[] dx = { 0, 0, 1, -1 };
11+
static int n;
12+
13+
public static void main(String[] args) throws Exception {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
n = Integer.parseInt(st.nextToken());
17+
int h = Integer.parseInt(st.nextToken());
18+
int d = Integer.parseInt(st.nextToken());
19+
char[][] board = new char[n][n];
20+
Deque<int[]> dq = new ArrayDeque<>();
21+
int[][] visited = new int[n][n];
22+
for (int i = 0; i < n; i++) {
23+
String line = br.readLine();
24+
for (int j = 0; j < n; j++) {
25+
board[i][j] = line.charAt(j);
26+
if (board[i][j] == 'S') {
27+
dq.offer(new int[] { i, j, 0, h, 0 });
28+
visited[i][j] = h;
29+
}
30+
}
31+
}
32+
while (!dq.isEmpty()) {
33+
int[] cur = dq.poll();
34+
for (int i = 0; i < 4; i++) {
35+
int ny = cur[0] + dy[i], nx = cur[1] + dx[i];
36+
int nh = cur[3], nd = cur[4];
37+
if (isValid(ny, nx)) {
38+
if (board[ny][nx] == 'E') {
39+
System.out.println(cur[2] + 1);
40+
return;
41+
}
42+
if (board[ny][nx] == 'U') {
43+
board[ny][nx] = '.';
44+
nd = d;
45+
}
46+
if (nd > 0) {
47+
nd--;
48+
} else {
49+
nh--;
50+
}
51+
if (visited[ny][nx] < nh + nd) {
52+
visited[ny][nx] = nh + nd;
53+
dq.offer(new int[] { ny, nx, cur[2] + 1, nh, nd });
54+
}
55+
}
56+
}
57+
}
58+
System.out.println(-1);
59+
}
60+
61+
private static boolean isValid(int y, int x) {
62+
return 0 <= y && y < n && 0 <= x && x < n;
63+
}
64+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.PriorityQueue;
7+
import java.util.StringTokenizer;
8+
9+
public class JW_색깔_폭탄 {
10+
11+
static class Group implements Comparable<Group> {
12+
int sy, sx;
13+
ArrayList<int[]> list = new ArrayList<>(); // 그룹에 해당하는 모든 폭탄의 좌표들
14+
int redCnt = 0;
15+
16+
// 처음 시작하는 요소 = 기준
17+
Group(int sy, int sx) {
18+
this.sy = sy;
19+
this.sx = sx;
20+
}
21+
22+
@Override
23+
// 우선순위 결정
24+
public int compareTo(Group o) {
25+
if (this.list.size() != o.list.size())
26+
return o.list.size() - this.list.size();
27+
else if (this.redCnt != o.redCnt)
28+
return this.redCnt - o.redCnt;
29+
else if (this.sy != o.sy)
30+
return o.sy - this.sy;
31+
return this.sx - o.sx;
32+
}
33+
}
34+
35+
static int n, m;
36+
static int[][] board;
37+
static int[] dy = { -1, 1, 0, 0 };
38+
static int[] dx = { 0, 0, -1, 1 };
39+
40+
public static void main(String[] args) throws Exception {
41+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
42+
StringTokenizer st = new StringTokenizer(br.readLine());
43+
n = Integer.parseInt(st.nextToken());
44+
m = Integer.parseInt(st.nextToken());
45+
board = new int[n][n];
46+
for (int i = 0; i < n; i++) {
47+
st = new StringTokenizer(br.readLine());
48+
for (int j = 0; j < n; j++)
49+
board[i][j] = Integer.parseInt(st.nextToken());
50+
}
51+
int score = 0; // 최종 점수
52+
while (true) {
53+
PriorityQueue<Group> groups = makeGroups(); // 우선순위 큐를 이용해서 우선순위 부여
54+
// 터트릴 수 없을 경우에 종료
55+
if (groups.isEmpty() || groups.peek().list.size() == 1)
56+
break;
57+
score += Math.pow(groups.peek().list.size(), 2); // 터트리기 전에 점수 계산
58+
explode(groups.poll()); // 폭발
59+
pullToDown(); // 중력 작용
60+
rotate(); // 반시계 회전
61+
pullToDown(); // 중력 작용
62+
}
63+
System.out.println(score);
64+
}
65+
66+
private static PriorityQueue<Group> makeGroups() {
67+
PriorityQueue<Group> groups = new PriorityQueue<>();
68+
boolean[][] visited = new boolean[n][n]; // 방문 처리
69+
for (int y = n - 1; y >= 0; y--) {
70+
for (int x = 0; x < n; x++) {
71+
// 방문했거나 빨간 폭탄일 경우 건너뜀
72+
if (visited[y][x] || board[y][x] <= 0)
73+
continue;
74+
boolean[][] redVisited = new boolean[n][n]; // 빨간 폭탄은 매 그룹마다 포함될 수 있으므로 따로 방문 처리
75+
Group group = new Group(y, x);
76+
Deque<int[]> dq = new ArrayDeque<>(); // BFS를 위한 덱
77+
group.list.add(new int[] { y, x });
78+
dq.offer(new int[] { y, x });
79+
visited[y][x] = true;
80+
81+
// BFS
82+
while (!dq.isEmpty()) {
83+
int[] cur = dq.poll();
84+
for (int k = 0; k < 4; k++) {
85+
int ny = cur[0] + dy[k], nx = cur[1] + dx[k];
86+
if (isValid(ny, nx) && !visited[ny][nx]) {
87+
// 빨간 폭탄일 경우
88+
if (board[ny][nx] == 0) {
89+
// 방문하지 않은 빨간 폭탄일 경우
90+
if (!redVisited[ny][nx]) {
91+
group.list.add(new int[] { ny, nx });
92+
group.redCnt++;
93+
dq.offer(new int[] { ny, nx });
94+
redVisited[ny][nx] = true;
95+
}
96+
// 같은 색깔의 폭탄일 경우만
97+
} else if (board[ny][nx] == board[y][x]) {
98+
group.list.add(new int[] { ny, nx });
99+
dq.offer(new int[] { ny, nx });
100+
visited[ny][nx] = true;
101+
}
102+
}
103+
}
104+
}
105+
groups.offer(group);
106+
}
107+
}
108+
return groups;
109+
}
110+
111+
// 폭발 함수
112+
private static void explode(Group group) {
113+
ArrayList<int[]> list = group.list;
114+
for (int[] bomb : list) {
115+
board[bomb[0]][bomb[1]] = -2; // 빈 공간으로 변경
116+
}
117+
}
118+
119+
// 빈 공간이 있을 경우 아래로 당김
120+
private static void pullToDown() {
121+
for (int y = n - 1; y > 0; y--)
122+
next: for (int x = 0; x < n; x++)
123+
if (board[y][x] == -2) {
124+
for (int ny = y - 1; ny >= 0; ny--) {
125+
// 빈 공간이 아니고 돌이 아닌 다른 수가 나올때까지
126+
if (board[ny][x] != -2) {
127+
if (board[ny][x] != -1)
128+
swap(y, x, ny, x); // 스왑
129+
// 스왑을 했다면 다음 빈 공간 탐색
130+
continue next;
131+
}
132+
}
133+
}
134+
}
135+
136+
// 반 시계 방향 회전
137+
private static void rotate() {
138+
int[][] next = new int[n][n];
139+
for (int i = 0; i < n; i++)
140+
for (int j = 0; j < n; j++)
141+
next[n - 1 - j][i] = board[i][j];
142+
board = next;
143+
}
144+
145+
// 배열의 요소를 바꾸는 스왑
146+
private static void swap(int y, int x, int ny, int nx) {
147+
int temp = board[y][x];
148+
board[y][x] = board[ny][nx];
149+
board[ny][nx] = temp;
150+
}
151+
152+
// 경계 체크
153+
private static boolean isValid(int y, int x) {
154+
return 0 <= y && y < n && 0 <= x && x < n;
155+
}
156+
}

‎Programmers/Level2/JW_340211.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.HashMap;
2+
class JW_340211 {
3+
HashMap<Integer, HashMap<Integer, Integer>> moves = new HashMap<>();
4+
int cnt = 0;
5+
6+
public int solution(int[][] points, int[][] routes) {
7+
for (int i = 0; i < routes.length; i++) {
8+
int[] route = routes[i];
9+
int[] now = points[route[0] - 1].clone();
10+
int turn = 0;
11+
moves.putIfAbsent(turn, new HashMap<>());
12+
int pos = now[0] * 1000 + now[1];
13+
moves.get(turn).put(pos, moves.get(turn).getOrDefault(pos, 0) + 1);
14+
for (int j = 1; j < route.length; j++) {
15+
int[] to = points[route[j] - 1];
16+
while (now[0] != to[0] || now[1] != to[1]) {
17+
if (now[0] < to[0])
18+
now[0]++;
19+
else if (now[0] > to[0])
20+
now[0]--;
21+
else if (now[1] < to[1])
22+
now[1]++;
23+
else if (now[1] > to[1])
24+
now[1]--;
25+
turn++;
26+
moves.putIfAbsent(turn, new HashMap<>());
27+
pos = now[0] * 1000 + now[1];
28+
moves.get(turn).put(pos, moves.get(turn).getOrDefault(pos, 0) + 1);
29+
}
30+
}
31+
}
32+
int cnt = 0;
33+
for (int turn : moves.keySet())
34+
for (int move : moves.get(turn).keySet())
35+
if (moves.get(turn).get(move) > 1)
36+
cnt++;
37+
return cnt;
38+
}
39+
}

‎Programmers/Level3/JW_118669.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashSet;
4+
import java.util.PriorityQueue;
5+
class JW_118669 {
6+
public int[] solution(int n, int[][] paths, int[] gates, int[] summits) {
7+
ArrayList<ArrayList<int[]>> graph = new ArrayList<>();
8+
for (int i = 0; i < n + 1; i++)
9+
graph.add(new ArrayList<>());
10+
for (int[] path : paths) {
11+
graph.get(path[0]).add(new int[] { path[1], path[2] });
12+
graph.get(path[1]).add(new int[] { path[0], path[2] });
13+
}
14+
15+
HashSet<Integer> gateSet = new HashSet<>();
16+
HashSet<Integer> summitSet = new HashSet<>();
17+
for (int gate : gates)
18+
gateSet.add(gate);
19+
for (int summit : summits)
20+
summitSet.add(summit);
21+
22+
int[] result = { 0, Integer.MAX_VALUE };
23+
Arrays.sort(summits);
24+
for (int summit : summits) {
25+
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
26+
boolean[] visited = new boolean[n + 1];
27+
pq.offer(new int[] { summit, 0 });
28+
while (!pq.isEmpty()) {
29+
int[] u = pq.poll();
30+
if (gateSet.contains(u[0])) {
31+
if (result[1] > u[1]) {
32+
result = new int[] { summit, u[1] };
33+
}
34+
break;
35+
}
36+
visited[u[0]] = true;
37+
for (int[] v : graph.get(u[0])) {
38+
if (!visited[v[0]] && !summitSet.contains(v[0])) {
39+
pq.offer(new int[] { v[0], Math.max(u[1], v[1]) });
40+
}
41+
}
42+
}
43+
}
44+
return result;
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
SELECT
2+
B.name `Department`
3+
, A.name `Employee`
4+
, A.salary `Salary`
5+
FROM
6+
Employee A
7+
INNER JOIN Department B
8+
ON A.departmentId = B.id
9+
WHERE
10+
(
11+
SELECT COUNT(DISTINCT salary)
12+
FROM Employee C
13+
WHERE departmentId = A.departmentId AND salary >= A.salary
14+
) <= 3
15+
ORDER BY
16+
Department
17+
, Salary DESC

0 commit comments

Comments
(0)

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