Skip to content
49 changes: 49 additions & 0 deletions BOJ/1000-5000번/DH_1911.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.*;
import java.util.*;

/*
* 훍길 보수하기
*/

public class DH_1911 {
static class Node {
int s, e;
public Node(int s, int e) {
this.s = s;
this.e = e;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken()); // 폭우
int L = Integer.parseInt(st.nextToken()); // 널빤지

Node[] nodes = new Node[N];
for(int i = 0; i < nodes.length; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

nodes[i] = new Node(s, e);
}

// 시작점 기준 정렬
Arrays.sort(nodes, (o1, o2) -> Integer.compare(o1.s, o2.s));

int current = 0, cnt = 0;
for(Node n: nodes) {
if(current < n.s) {
current = n.s;
}

while(current < n.e) {
current += L;
cnt++;
}
}
Comment on lines +36 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 요상하게 구했는데 엄청 깔끔하시네여..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while문을 사용해서 널빤지의 개수를 구해줬는데, 나누기, 나머지 연산을 활용해서 구해주는게 효율적일 것 같아요!!


System.out.println(cnt);
}
}
58 changes: 58 additions & 0 deletions BOJ/1000-5000번/DH_2461.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.*;
import java.util.*;

/*
* 대표 선수
*/

public class DH_2461 {
static class Node {
int value, i, j;
public Node(int value, int i, int j) {
this.value = value;
this.i = i;
this.j = j;
}
}
static int[][] arr;

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

int N = Integer.parseInt(st.nextToken()); // 학급의 수
int M = Integer.parseInt(st.nextToken()); // 학생의 수

arr = new int[N][M];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) arr[i][j] = Integer.parseInt(st.nextToken());
// 한 줄이 다 입력될 때 마다 정렬
Arrays.sort(arr[i]);
}

int result = Integer.MAX_VALUE;
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.value));

int max = 0;
for(int i = 0 ; i < N; i++){
pq.add(new Node(arr[i][0], i, 0));
max = Math.max(arr[i][0],max);
}

while(true){
Node current = pq.poll();
result = Math.min(max - current.value, result);

// 제일 작은 값이 있는 원소의 idx가 열의 마지막 idx라면 반복문 나오기
if(current.j == M - 1) break;

// 최솟값이 있는 행의 열 idx를 + 1 한 뒤, pq에 넣어주고,
// max 값 업데이트 해주기
int value = arr[current.i][current.j + 1];
pq.add(new Node(value, current.i, current.j + 1));
max = Math.max(max,value);
}
System.out.println(result);
}
}
102 changes: 102 additions & 0 deletions BOJ/20001-25000번/DH_20007.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import java.io.*;
import java.util.*;

/*
* 떡 돌리기
*/

public class DH_20007 {
static final Long INF = Long.MAX_VALUE;
static class Node implements Comparable<Node> {
int e;
long w;
public Node(int e, long w) {
this.e = e;
this.w = w;
}

@Override
public int compareTo(Node o) {
return Long.compare(this.w, o.w);
}
}
static ArrayList<Node> adj[];
static int N, M, X, Y;
static long[] dis;

static void dijkstra() {
dis = new long[N];
Arrays.fill(dis, INF);


PriorityQueue<Node> pq = new PriorityQueue<Node>();
pq.add(new Node(Y, 0));
dis[Y] = 0;

while(!pq.isEmpty()) {
Node current = pq.poll();

for(Node next: adj[current.e]) {
if(dis[next.e] > dis[current.e] + next.w) {
dis[next.e] = dis[current.e] + next.w;
pq.add(new Node(next.e, dis[next.e]));
}
}
}
}

static int getCnt() {
// 가는 길이가 최소길이 순이 될 수 있도록 정렬
Arrays.sort(dis);

// 한 번에 갈 수 있는 경우의 수
int cnt = 1;

long tmpDis = 0; // 거리의 합계
for(int i = 0; i < N; i++) {
long doubleDis = dis[i] * 2; // 왕복 거리
// 도달할 수 없는 지점이 있거나, 왕복 거리가 X보다 멀다면 -1 반환
if(dis[i] == INF || doubleDis > X) return - 1;

// 왕복 거리로 갈 수 있다면 거리의 합계에 더해주기
if(tmpDis + doubleDis <= X) {
tmpDis += doubleDis;
}
// 갈 수 없다면 cnt 늘려주고 거리의 합계를 왕복 거리로 갱신
else {
cnt++;
tmpDis = doubleDis;
}
}

return cnt;
}
public static void main(String[] args) throws Exception {
initInput();
dijkstra();
System.out.println(getCnt());
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken()); // 집의 개수
M = Integer.parseInt(st.nextToken()); // 도로의 개수
X = Integer.parseInt(st.nextToken()); // 갈 수 있는 최대 거리
Y = Integer.parseInt(st.nextToken()); // 시작점

adj = new ArrayList[N];
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();

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[a].add(new Node(b, c));
adj[b].add(new Node(a, c));
}
}
}
22 changes: 22 additions & 0 deletions BOJ/5001-10000번/DH_9342.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.io.*;

/*
* 염색체
*/

public class DH_9342 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

int t = Integer.parseInt(br.readLine());
for(int i = 0; i < t; i++) {
String s = br.readLine();
if(s.matches("^[ABCEDF]?A+F+C+[ABCDEF]?$")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 String에 직접 matches를 쓰면 되는군요🤔
문자열 메서드 다 까먹어버렸네요..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 저도 import java.util.regex.Pattern로 정규표현식 사용해야하는줄 알았는데 matches를 사용할 수 있군요! 관련해서 한번 공부해봐야겠어요👍

sb.append("Infected!\n");
} else sb.append("Good\n");
}

System.out.println(sb);
}
}
105 changes: 105 additions & 0 deletions CodeTree/2017-2018년/DH_방화벽_설치하기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import java.io.*;
import java.util.*;

public class DH_방화벽_설치하기 {
static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}
static int N, M, emptyArea, result;
static int[][] map;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};

static void solution() {
// 방화벽 3개 추가 설치 (조합)
func(0, 0, new boolean[N][M]);
System.out.println(result);
}

static int bfs(boolean[][] wall) {
Deque<Point> q = new ArrayDeque<Point>();

int cnt = 0; // 불에 타는 영역 수
boolean[][] v = new boolean[N][M];

for(int r = 0; r < N; r++) {
for(int c = 0; c < M; c++) {
// 이미 방문한 곳이거나, 불이 아니거나, 벽일 때
if(v[r][c] || map[r][c] != 2 || wall[r][c]) continue;
q.add(new Point(r, c));
v[r][c] = true;
}
}

while(!q.isEmpty()) {
Point current = q.poll();

for(int d = 0; d < 4; d++) {
int nr = current.r + dr[d];
int nc = current.c + dc[d];

// 범위에 벗어나거나, 이미 간 곳이거나, 벽이면 못감
if(!check(nr, nc) || v[nr][nc] || map[nr][nc] == 1 || wall[nr][nc]) continue;
q.add(new Point(nr, nc));
v[nr][nc]= true;
cnt++;
}
}

return cnt;
}

static boolean check(int r, int c) {
return r >= 0 && r < N && c >= 0 && c < M;
}
static void func(int idx, int depth, boolean[][] wall) {
if(depth == 3) {

int fireArea = bfs(wall);
result = Math.max(result, emptyArea - 3 - fireArea);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미리 센 빈칸으로 불 안난 곳 깔끔하게 구하는것도 같이 배워갑니닷😎


return;
}

for(int i = idx; i < N * M; i++) {
int r = i / M;
int c = i % M;

// 이미 방화벽이 있거나, 불이 있는 곳에는 방화벽을 세울 수 없음
if(map[r][c] != 0) continue;

wall[r][c] = true;
func(i + 1, depth + 1, wall);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열 위치를 하나의 인덱스로 깔끔하게 넘겨주는거...!!! 배우고갑니다!!

wall[r][c] = false;
}
}

public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("./input/방화벽설치하기.txt"));
initInput();
solution();
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

result = Integer.MIN_VALUE;

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

map = new int[N][M];

for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for(int c = 0; c < M; c++) {
map[r][c] = Integer.parseInt(st.nextToken());
if(map[r][c] == 0) emptyArea++;
}
}
}
}
25 changes: 25 additions & 0 deletions Programmers/Level2/DH_150369.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
택배 배달과 수거하기
*/

class DH_150369 {
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
long answer = 0;

int d = 0, p = 0;

for(int i = n - 1; i >= 0; i--) {
d -= deliveries[i];
p -= pickups[i];

while(d < 0 || p < 0) {
d += cap;
p += cap;

// 왕복 왔다갔다 하는 거리
answer += (i + 1) * 2;
}
}
return answer;
}
}
Loading