Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions BOJ/1000-5000번/SB_1939.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_1939 {
static int N,M, start, end;
static List<List<Node>> adj = new ArrayList<>();
static int[] dist;

private static int dijskra() {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, Integer.MAX_VALUE)); // 최소값(최소값 중 최대)을 찾는것이기 때문에 맥스값으로 시작
dist[start] = Integer.MAX_VALUE;

while (!pq.isEmpty()) {
Node cur = pq.poll();
if (cur.idx==end) return cur.c; // 도착점 도달 시 최대 중량 반환

if (cur.c < dist[cur.idx]) continue;
for (Node nxt : adj.get(cur.idx)) {
int weight = Math.min(cur.c, nxt.c); // 현재까지 중량과 다음 간선의 중량 중 최소값
if (dist[nxt.idx] < weight) {
dist[nxt.idx] = weight;
pq.offer(new Node(nxt.idx, weight));
}
}
}
return 0; // 도달할 수 없는 경우
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

dist = new int[N+1];
Arrays.fill(dist, -1);
for (int i = 0; i <= N; i++) {
adj.add(new ArrayList<>());
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
adj.get(a).add(new Node(b, c));
adj.get(b).add(new Node(a, c));
}
st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());

System.out.println(dijskra());
}

private static class Node implements Comparable<Node>{
int idx, c;

public Node(int u, int c) {
this.idx = u;
this.c = c;
}

@Override
public int compareTo(Node o) {
return o.c - this.c;
}
}
}
55 changes: 55 additions & 0 deletions BOJ/1000-5000번/SB_2169.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SB_2169 {
static int N, M;
static int[][] board;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

board = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}

int[][] dp = new int[N][M];

// 첫번째 행 초기화
dp[0][0] = board[0][0];
for (int j = 1; j < M; j++) { // 오른쪽으로만 탐색 가능(이미 지나온 길 탐색 불가)
dp[0][j] = board[0][j] + dp[0][j - 1];
}

for (int i = 1; i < N; i++) {
int[] left = new int[M];
int[] right = new int[M];

left[0] = dp[i - 1][0] + board[i][0]; // 0번째열은 일단 이전 행에서 아래로 내려와야함 (위에서 내려오는 한가지 경우)
for (int j = 1; j < M; j++) { // 왼쪽에서 오는거, 위에서 내려오는 두가지 경우
left[j] = Math.max(left[j - 1], dp[i - 1][j]) + board[i][j];
}

right[M-1] = dp[i- 1][M-1] + board[i][M-1];
for (int j = M - 2; j >= 0; j--) {
right[j] = Math.max(right[j+1], dp[i-1][j]) + board[i][j];
}

// 왼쪽 오른쪽 값 합침
for (int j = 0; j < M; j++) {
dp[i][j] = Math.max(left[j], right[j]);
}
}

System.out.println(dp[N-1][M-1]);
}
}
56 changes: 56 additions & 0 deletions BOJ/1000-5000번/SB_2170.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class SB_2170 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

PriorityQueue<Node> pq = new PriorityQueue<>();

int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
pq.offer(new Node(s, e));
}

Node first = pq.poll();
int start = first.s;
int end = first.e;
int cnt = 0;

while (!pq.isEmpty()) {
Node cur = pq.poll();
if (start <= cur.s && cur.s <= end) { // 현재 선분이랑 겹칠 경우
end = Math.max(end, cur.e); // 더 큰 끝값으로 업데이트
continue;
}

// 겹치지 않음 (== 새로운 선분 시작)
cnt += end - start; // 지금까지의 선분 길이 더해주기
start = cur.s;
end = cur.e;
}
cnt += end-start; // 마지막 선분 길이 업데이트

System.out.println(cnt);
}
static class Node implements Comparable<Node>{
int s, e;

public Node(int s, int e) {
this.s = s;
this.e = e;
}

@Override
public int compareTo(Node o) {
return this.s - o.s;
}
}
}
52 changes: 52 additions & 0 deletions BOJ/1000-5000번/SB_2504.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

public class SB_2504 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();

Deque<Character> stack = new ArrayDeque<>();
int ans = 0;
int tmp = 1;

for (int i = 0; i < line.length(); i++) {
char cur = line.charAt(i);
switch (cur) {
case '(':
stack.addFirst(cur);
tmp*=2;
break;
case '[':
stack.addFirst(cur);
tmp*=3;
break;
case ')':
if (stack.isEmpty() || stack.peekFirst() != '('){
System.out.println(0);
return;
}
if (line.charAt(i - 1) == '(') ans+=tmp;
tmp/=2;
stack.pollFirst();
break;
case ']':
if (stack.isEmpty() || stack.peekFirst() != '['){
System.out.println(0);
return;
}
if (line.charAt(i-1)=='[') ans+=tmp;
tmp/=3;
stack.pollFirst();
break;
}
}

// 남아있는 괄호 여부 체크
if (stack.isEmpty()) System.out.println(ans);
else System.out.println(0);
}
}
111 changes: 111 additions & 0 deletions CodeTree/2015-2016년/SB_두개의사탕.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_두개의사탕 {
static int N, M;
static char[][] board;
static int[] blue = new int[2];
static int[] red = new int[2];
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};

private static Res tilt(int x, int y, int i) { // 방향에 따라 기울이기
int move = 0;
while (board[x + dx[i]][y + dy[i]] != '#' && board[x][y] != 'O') { // 다음에 위치할 값이 #이 아니고 현재 값이 도착점이 아니면 계속 움직임
x += dx[i];
y += dy[i]; // 좌표값 갱신 (i방향으로 이동)
move++;
}
return new Res(x, y, move, board[x][y] == 'O');
}

private static int bfs() {
Deque<Node> que = new ArrayDeque<>();
Set<String> visited = new HashSet<>();

que.offer(new Node(red[0], red[1], blue[0], blue[1], 0));
visited.add("" + red[0] + red[1] + blue[0] + blue[1]);

while (!que.isEmpty()) {
Node cur = que.poll();
if (cur.cnt >= 10) return -1;

for (int i = 0; i < 4; i++) {
// 구슬 각각 기울인 값
Res nR = tilt(cur.rx, cur.ry, i);
Res nB = tilt(cur.bx, cur.by, i);

if (nB.isGoal) continue;
if (nR.isGoal) return cur.cnt + 1;
if (nB.x == nR.x && nB.y == nR.y) { // 구슬이 같은 위치에 있다고 나왔을땐, 거리를 통해 실제 위치 파악
if (nR.mv > nB.mv){ // 이동거리가 많은 것이 늦게 온것, 따라서 기존 위치보다 한 칸 뒤에 존재
nR.x -= dx[i];
nR.y -= dy[i];
} else{
nB.x -= dx[i];
nB.y -= dy[i];
}
}

String state = "" + nR.x + nR.y + nB.x + nB.y;
if (!visited.contains(state)) {
visited.add(state);
que.offer(new Node(nR.x, nR.y, nB.x, nB.y, cur.cnt + 1));
}
}
}
return -1;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

board = new char[N][M];

for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
board[i][j] = line.charAt(j);
if (board[i][j]=='B') {
blue[0] = i;
blue[1] = j;
}
else if (board[i][j]=='R') {
red[0] = i;
red[1] = j;
}
}
}

System.out.println(bfs());
}

private static class Node{
int rx, ry, bx, by, cnt;

public Node(int rx, int ry, int bx, int by, int cnt) {
this.rx = rx;
this.ry = ry;
this.bx = bx;
this.by = by;
this.cnt = cnt;
}
}

private static class Res{
int x, y, mv;
boolean isGoal;

public Res(int x, int y, int mv, boolean isGoal) {
this.x = x;
this.y = y;
this.mv = mv;
this.isGoal = isGoal;
}
}
}
20 changes: 20 additions & 0 deletions SQL/21주차/SB_연도별 대장균 크기의 편차 구하기.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
WITH ColonyData AS (
SELECT
YEAR(DIFFERENTIATION_DATE) AS YEAR,
SIZE_OF_COLONY,
ID
FROM ECOLI_DATA
),
MaxColony AS (
SELECT YEAR(DIFFERENTIATION_DATE) AS YEAR,
MAX(SIZE_OF_COLONY) AS MX
FROM ECOLI_DATA
GROUP BY YEAR(DIFFERENTIATION_DATE)
)

SELECT c.YEAR,
(m.MX - c.SIZE_OF_COLONY) AS YEAR_DEV,
c.ID
FROM ColonyData c
JOIN MaxColony m ON C.YEAR = M.YEAR
ORDER BY c.YEAR, YEAR_DEV;