Skip to content

Commit badf63e

Browse files
committed
231003
1 parent b382ed9 commit badf63e

11 files changed

+604
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dateStructure.priorityQueue;
2+
3+
import java.util.Collections;
4+
import java.util.PriorityQueue;
5+
import java.util.Scanner;
6+
7+
/**
8+
* [조건]
9+
* 지금까지 말한 수들 중에서의 중간값을 말해야 한다
10+
* 외친 수가 짝수인 경우에는 중간에 있는 두 수 중 작은 값을 말한다
11+
*
12+
* [풀이]
13+
* 우선순위 큐를 활용하여 최대힙과 최소힙을 각각 하나씩 마련한다
14+
* 최소힙과 최대힙의 길이를 같게 유지하면서 입력값을 받는다
15+
*/
16+
public class BOJ_1655_가운데를말해요 {
17+
18+
static PriorityQueue<Integer> left;
19+
static PriorityQueue<Integer> right;
20+
static StringBuilder sb = new StringBuilder();
21+
22+
public static void main(String[] args) {
23+
Scanner sc = new Scanner(System.in);
24+
25+
int N = sc.nextInt();
26+
27+
left = new PriorityQueue<>(Collections.reverseOrder());
28+
right = new PriorityQueue<>();
29+
for (int i = 0; i < N; i++) {
30+
int n = sc.nextInt();
31+
32+
if (left.size() == right.size()) left.add(n);
33+
else right.add(n); // 두 힙의 길이가 다른 경우는 right의 길이가 더 짧은 경우밖에 없으므로 right에 채워준다
34+
35+
if (!left.isEmpty() && !right.isEmpty()) {
36+
if (left.peek() > right.peek()) {
37+
int tmp = left.poll();
38+
left.add(right.poll());
39+
right.add(tmp);
40+
}
41+
}
42+
43+
sb.append(left.peek()).append('\n');
44+
}
45+
46+
System.out.println(sb);
47+
}
48+
49+
}

src/dp/BOJ_7579_앱.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dp;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class BOJ_7579_앱 {
9+
10+
static int N, M;
11+
static int[][] apps;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken()); // 앱의 갯수
18+
M = Integer.parseInt(st.nextToken()); // 확보해야 하는 메모리
19+
apps = new int[N+1][2];
20+
21+
st = new StringTokenizer(br.readLine());
22+
for (int i = 0; i < N; i++) {
23+
apps[i+1][0] = Integer.parseInt(st.nextToken());
24+
}
25+
26+
st = new StringTokenizer(br.readLine());
27+
for (int i = 0; i < N; i++) {
28+
apps[i+1][1] = Integer.parseInt(st.nextToken());
29+
}
30+
31+
System.out.println(solution());
32+
}
33+
34+
static int solution() {
35+
int result = Integer.MAX_VALUE;
36+
int[][] dp = new int[2][10001]; // dp[i][c]: 1~i번째 앱을 고려하였을 때 비용 c로 확보할 수 있는 최대 메모리
37+
38+
for (int i = 1; i <= N; i++) {
39+
int memory = apps[i][0];
40+
int cost = apps[i][1];
41+
42+
for (int c = 0; c <= 10000; c++) {
43+
if (c >= cost) {
44+
dp[i%2][c] = Math.max(memory + dp[(i-1)%2][c-cost], dp[(i-1)%2][c]);
45+
} else {
46+
dp[i%2][c] = dp[(i-1)%2][c];
47+
}
48+
49+
if (dp[i%2][c] >= M) {
50+
result = Math.min(result, c);
51+
}
52+
}
53+
}
54+
55+
return result;
56+
}
57+
58+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package graph;
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+
/**
10+
* [조건]
11+
* 예은이는 임의의 노드에 떨어진다
12+
* 떨어진 노드에서 주어진 수색 범위 내(현재 위치 포함)의 아이템을 모두 얻게 된다
13+
* 이때 얻을 수 있는 아이템 갯수의 최댓값을 구하라
14+
*
15+
* [풀이]
16+
* 플로이드워셜을 통해 모든 노드에 대한 모든 노드로의 거리를 구하고
17+
* 각 노드별로 얻을 수 있는 아이템 수를 구하여 그 중 최댓값을 구한다
18+
*/
19+
public class BOJ_14938_서강그라운드 {
20+
21+
static int V, E, R;
22+
static int[] items;
23+
static int[][] dist;
24+
static final int INF = 1_000_000_000;
25+
26+
public static void main(String[] args) throws IOException {
27+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
30+
V = Integer.parseInt(st.nextToken()); // 정점의 갯수
31+
R = Integer.parseInt(st.nextToken()); // 범위의 크기
32+
E = Integer.parseInt(st.nextToken()); // 간선의 갯수
33+
34+
st = new StringTokenizer(br.readLine());
35+
items = new int[V+1];
36+
for (int i = 1; i <= V; i++) {
37+
items[i] = Integer.parseInt(st.nextToken()); // 노드 별 아이템의 갯수
38+
}
39+
40+
// 거리 배열 초기화
41+
dist = new int[V+1][V+1];
42+
for (int i = 1; i <= V; i++) {
43+
Arrays.fill(dist[i], INF);
44+
dist[i][i] = 0;
45+
}
46+
47+
for (int i = 0; i < E; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
50+
int from = Integer.parseInt(st.nextToken());
51+
int to = Integer.parseInt(st.nextToken());
52+
int weight = Integer.parseInt(st.nextToken());
53+
54+
dist[from][to] = weight;
55+
dist[to][from] = weight;
56+
}
57+
58+
System.out.println(solution());
59+
}
60+
61+
static int solution() {
62+
for (int k = 1; k <= V; k++) {
63+
for (int i = 1; i <= V; i++) {
64+
for (int j = 1; j <= V; j++) {
65+
if (dist[i][k] != INF && dist[k][j] != INF) {
66+
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
67+
}
68+
}
69+
}
70+
}
71+
72+
int[] result = new int[V+1]; // 노드 별 획득할 수 있는 아이템 갯수
73+
for (int i = 1; i <= V; i++) {
74+
for (int j = 1; j <= V; j++) {
75+
if (dist[i][j] <= R) { // 수색 범위 내에 있는 아이템 갯수 카운트
76+
result[i] += items[j];
77+
}
78+
}
79+
}
80+
81+
return Arrays.stream(result).max().getAsInt();
82+
}
83+
84+
}

src/graph/BOJ_2589_보물섬.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package graph;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
import java.util.StringTokenizer;
9+
10+
/**
11+
* [조건]
12+
* 육지(L), 바다(W)
13+
* 인접 상하좌우의 육지로만 이동 가능
14+
* 보물은 서로 간에 최단 거리로 이동하는 데 있어 가장 긴 시간이 걸리는 육지 두 곳에 나뉘어 묻혀있다
15+
*
16+
* 지도가 주어질 때, 보물이 묻혀 있는 두 곳간의최단 거리로 이동하는 시간을 구하라
17+
*
18+
* [풀이]
19+
* 모든 육지인 칸에 대해서 탐색하며
20+
*/
21+
public class BOJ_2589_보물섬 {
22+
23+
static int N, M;
24+
static char[][] map;
25+
static int[] di = {-1, 1, 0, 0};
26+
static int[] dj = {0, 0, -1, 1};
27+
static int result = 0;
28+
29+
public static void main(String[] args) throws IOException {
30+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
31+
StringTokenizer st = new StringTokenizer(br.readLine());
32+
33+
N = Integer.parseInt(st.nextToken());
34+
M = Integer.parseInt(st.nextToken());
35+
36+
map = new char[N][M];
37+
for (int i = 0; i < N; i++) {
38+
String str = br.readLine();
39+
for (int j = 0; j < M; j++) {
40+
map[i][j] = str.charAt(j);
41+
}
42+
}
43+
44+
solution();
45+
System.out.println(result);
46+
}
47+
48+
static void solution() {
49+
for (int i = 0; i < N; i++) {
50+
for (int j = 0; j < M; j++) {
51+
if (map[i][j] == 'L') { // 육지
52+
bfs(i, j);
53+
}
54+
}
55+
}
56+
}
57+
58+
static void bfs(int startI, int startJ) {
59+
int max = 0;
60+
61+
Queue<int[]> que = new LinkedList<>();
62+
int[][] visited = new int[N][M];
63+
64+
que.add(new int[]{startI, startJ});
65+
visited[startI][startJ] = 1; // 방문하지 않은 곳과의 구분을 위해 0이 아닌 1로 초기화
66+
67+
while (!que.isEmpty()) {
68+
int[] cur = que.poll();
69+
int i = cur[0], j = cur[1];
70+
71+
for (int d = 0; d < 4; d++) {
72+
int nextI = i + di[d];
73+
int nextJ = j + dj[d];
74+
75+
if (nextI < 0 || nextJ < 0 || nextI > N-1 || nextJ > M-1) continue;
76+
if (map[nextI][nextJ] != 'L') continue;
77+
if (visited[nextI][nextJ] != 0) continue;
78+
79+
visited[nextI][nextJ] = visited[i][j] + 1;
80+
que.add(new int[]{nextI, nextJ});
81+
82+
max = Math.max(max, visited[nextI][nextJ]);
83+
}
84+
}
85+
86+
// 최댓값 갱신
87+
result = Math.max(result, max-1);
88+
}
89+
90+
}

src/greedy/BOJ_12904_A와B.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package greedy;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
/**
8+
* 두 가지 연산
9+
* - 문자열의 뒤에 A를 추가한다
10+
* - 문자열을 뒤집고 뒤에 B를 추가한다
11+
*/
12+
public class BOJ_12904_A와B {
13+
14+
static StringBuffer S, T;
15+
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
19+
S = new StringBuffer(br.readLine());
20+
T = new StringBuffer(br.readLine());
21+
22+
System.out.println(solution());
23+
}
24+
25+
static int solution() {
26+
while (S.length() < T.length()) {
27+
if (T.charAt(T.length()-1) == 'A') {
28+
T.deleteCharAt(T.length()-1);
29+
} else if (T.charAt(T.length()-1) == 'B') {
30+
T.deleteCharAt(T.length()-1);
31+
T.reverse();
32+
}
33+
}
34+
35+
if (S.toString().equals(T.toString())) { // StringBuffer는 equals() 메서드를 override하지 않기 때문에 == 비교를 한 것과 같은 결과를 반환한다.
36+
return 1;
37+
}
38+
return 0;
39+
}
40+
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package greedy;
2+
3+
import java.util.Scanner;
4+
5+
public class BOJ_1343_폴리노미오 {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
String s = sc.next();
10+
11+
StringBuilder sb = new StringBuilder();
12+
13+
String A = "AAAA";
14+
String B = "BB";
15+
16+
int cnt = 0;
17+
for (int i = 0; i < s.length(); i++) {
18+
if (s.charAt(i) == 'X') {
19+
cnt++;
20+
} else {
21+
if (cnt%2 != 0) {
22+
System.out.println(-1);
23+
System.exit(0);
24+
}
25+
26+
if (cnt > 0) {
27+
while (cnt >= 4) {
28+
sb.append(A);
29+
cnt -= 4;
30+
}
31+
32+
while (cnt > 0 && cnt%2 == 0) {
33+
sb.append(B);
34+
cnt -= 2;
35+
}
36+
}
37+
38+
sb.append('.');
39+
cnt = 0;
40+
}
41+
}
42+
43+
if (cnt%2 != 0) {
44+
System.out.println(-1);
45+
System.exit(0);
46+
}
47+
48+
if (cnt > 0) {
49+
while (cnt >= 4) {
50+
sb.append(A);
51+
cnt -= 4;
52+
}
53+
54+
while (cnt > 0 && cnt%2 == 0) {
55+
sb.append(B);
56+
cnt -= 2;
57+
}
58+
}
59+
60+
System.out.println(sb);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)