Skip to content
45 changes: 45 additions & 0 deletions BOJ/1000-5000번/HW_1911.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

// 모든 물웅덩이들을 덮기 위해 필요한 널빤지들의 최소 개수를 출력
// 시작 위치로부터 순서대로(->정렬) 물웅덩이를 덮어 나가기 -> 그리디
public class Main {
public static void main(String[] args) throws IOException {
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());
int[][] puddles = new int [N][2];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
puddles[i][0] = s; // 물웅덩이 시작
puddles[i][1] = e; // 물웅덩이 끝
}
Arrays.sort(puddles, (a, b) -> Integer.compare(a[0], b[0])); // 시작 위치부터 순서대로 덮기 위해 오름차순 정렬

int current = 0; // 현재 덮은 위치의 끝
int cnt = 0; // 필요한 널빤지 개수

for(int i=0; i<puddles.length; i++){ // 물 웅덩이 덮기
int s = puddles[i][0];
int e = puddles[i][1];

if(current >= e){ // 이미 덮였다면
continue;
}
if(current < s){
current = s;
}

int result = (int) Math.ceil((e - current) / (double) L); // (덮어야할 길이 / 널빤지 길이)
cnt += result; // 널빤지 개수++
current += result * L; // 덮은 위치 갱신
}
Copy link
Contributor

@yeongleej yeongleej Oct 31, 2024

Choose a reason for hiding this comment

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

완전 정석적인 코드 같아여~~ 굿 👍

System.out.println(cnt);
}
}
51 changes: 51 additions & 0 deletions BOJ/1000-5000번/HW_2461.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import org.w3c.dom.Node;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException {
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());
int[][] students = new int[N][M];

for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<M; j++){
students[i][j] = Integer.parseInt(st.nextToken());
}
Arrays.sort(students[i]); // 각 반의 능력치 정렬
}

int[] indexs = new int[N]; // 학생들 인덱스를 저장할 배열
for(int i=0; i<N; i++){
indexs[i] = 0;
}
int min = Integer.MAX_VALUE;

while(true){
int curMin = students[0][indexs[0]];
int curMax= students[0][indexs[0]];
int minIdex = 0;
for(int i=1; i<N; i++){
if (curMin > students[i][indexs[i]]) { // 최솟값
curMin = students[i][indexs[i]];
minIdex = i;
}
if(curMax < students[i][indexs[i]]){ // 최댓값
curMax = students[i][indexs[i]];
}
}
if((curMax - curMin) < min){
min = curMax - curMin;
}
if(++indexs[minIdex] >= M)
break;
}
System.out.println(min);
}
}
94 changes: 94 additions & 0 deletions BOJ/20001-25000번/HW_20007.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import org.w3c.dom.Node;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

// 시간복잡도 : N<=1,000, M<=100,000 O(NM) 가능
// 이웃집 모두에 떡을 돌리기 위한 최소 일을 출력
// 만약 모두 방문할수 없으면 -1을 출력
public class Main {
static class Node implements Comparable<Node>{
int house;
int cost;
Node(int house, int cost){
this.house = house;
this.cost = cost;
}
public int compareTo(Node other){
return this.cost - other.cost;
}
}

static List<Node>[] graph;
static int[] distance;
public static void main(String[] args) throws IOException {
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()); // 도로 개수
int X = Integer.parseInt(st.nextToken()); // 도로 길이 <= 1000000000
int Y = Integer.parseInt(st.nextToken()); // 성현이 집

// 그래프 초기화
graph = new ArrayList[N];
for(int i=0; i<N; i++){
graph[i] = 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());
graph[A].add(new Node(B, C)); // A -> B 양방향
graph[B].add(new Node(A, C)); // B -> A
}
distance = new int[N];
Arrays.fill(distance, Integer.MAX_VALUE);
dijkstra(Y);

int days = 1; // 최소 일수(첫날 1일)
int totalDistance = 0;
Arrays.sort(distance);

for (int i = 0; i < N; i++) { // 거리 확인
if (i != Y) { // 성현이의 집은 제외
if (distance[i] * 2 > X) { // 왕복 거리 초과
System.out.println(-1); // 방문x
return;
}
if (totalDistance + distance[i] * 2 > X) { // 하루 이동 거리 초과
days++; // 하루 추가
totalDistance = 0; // 새로운 날로 초기화
}
totalDistance += distance[i] * 2; // 방문한 거리 추가
}
}
System.out.println(days); // 최소 일수
}
public static void dijkstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(start, 0));
distance[start] = 0; // 시작 위치는 거리 0

while (!pq.isEmpty()) {
Node cur = pq.poll();
int curHouse = cur.house;
int curCost = cur.cost;

if (curCost > distance[curHouse]) continue;

for (int i = 0; i < graph[curHouse].size(); i++) {
Node neighbor = graph[curHouse].get(i);
int newCost = distance[curHouse] + neighbor.cost;

if (newCost < distance[neighbor.house]) {
distance[neighbor.house] = newCost;
pq.add(new Node(neighbor.house, newCost));
}
}
}
}
}
17 changes: 17 additions & 0 deletions BOJ/5001-10000번/HW_9342.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
String str = "^[ABCDEF]?A+F+C+[ABCDEF]?$";

StringBuilder sb = new StringBuilder();
while(T-->0){
sb.append(br.readLine().matches(str) ? "Infected!" : "Good").append('\n');
}
System.out.println(sb);
}
}
100 changes: 100 additions & 0 deletions CodeTree/2017-2018년/HW_방화벽_설치하기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.util.*;
import java.io.*;

// 방화벽 3개를 추가로 설치 했을 때 방화벽을 제외하고 불이 퍼지지 않는 영역 크기의 최댓값을 출력
public class HW_방화벽_설치하기 {
static int n, m;
static int[][] board;
static int[] dx = {-1, 1, 0, 0}; // 상하좌우
static int[] dy = {0, 0, -1, 1};
static int max = Integer.MIN_VALUE;
Copy link
Contributor

@jewan100 jewan100 Oct 28, 2024

Choose a reason for hiding this comment

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

모든 영역에 불이 붙는 경우(빈 영역이 없는 경우) 밖에 없을수도 있으니깐 max값은 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());
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());
}
}
dfs(0);
System.out.println(max);
}

static void dfs(int wall) { // 방화벽 설치
if (wall == 3) { // 방화벽 3개까지
bfs();
return;
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == 0) {
board[i][j] = 1;
dfs(wall + 1);
board[i][j] = 0;
}
}
}
}
Comment on lines +35 to +44
Copy link
Contributor

@jewan100 jewan100 Oct 28, 2024

Choose a reason for hiding this comment

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

이렇게 DFS의 시작 범위를 잡으면 중복 연산이 발생하니 중복 처리를 위한 비트 마스킹이나 다른 방법을 찾아보는 것도 좋을 것 같아요!


static void bfs() { // 불 번지는 영역 확인
Queue<Node> queue = new LinkedList<>();
int[][] temp = new int[n][m];

for (int i = 0; i < n; i++) { // 배열 복사
for (int j = 0; j < m; j++) {
temp[i][j] = board[i][j];
if (board[i][j] == 2) { // 불이 있을 경우
queue.add(new Node(i, j)); // 불 번짐
}
}
}
Comment on lines +50 to +57
Copy link
Contributor

@jewan100 jewan100 Oct 28, 2024

Choose a reason for hiding this comment

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

모든 DFS마다 불의 위치를 다시 찾아주는 것은 비효율적이에요!
미리 불 위치를 입력 받을 때 리스트에 담아두고 큐에 옮겨 담는 것이 더 좋을 것 같아요

while (!queue.isEmpty()) {
Node node = queue.poll();
int x = node.x;
int y = node.y;

for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (isRange(nx, ny) && temp[nx][ny] == 0) {
queue.add(new Node(nx, ny));
temp[nx][ny] = 2;
}
}
}
check(temp);
}

static void check(int[][] temp) { // 불이 번지지 않은 영역 확인
int safe = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (temp[i][j] == 0) {
safe++;
}
}
}
max = Math.max(max, safe);
}

static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < n && ny >= 0 && ny < m;
}

static class Node {
int x;
int y;

Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}
20 changes: 20 additions & 0 deletions Programmers/Level2/HW_150369.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 시간복잡도 : N<=100,000 완전 탐색 불가
// 트럭 하나로 모든 배달과 수거를 마치고 물류창고까지 돌아올 수 있는 최소 이동 거리
// 모든 배달과 수거 -> 각 집 방문 -> 전체거리 최소화 -> 멀리있는 집부터 방문해서 돌아오기
class Solution {
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
long answer = 0;
int delivery = 0;
int pickup = 0;
for(int i=n-1; i>=0; i--){
delivery += deliveries[i];
pickup += pickups[i];
while(delivery>0 || pickup>0){
delivery-= cap;
pickup -= cap;
answer += (i+1) * 2;
}
}
return answer;
}
}
7 changes: 7 additions & 0 deletions SQL/08주차/입양 시각 구하기(1).SQL
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- https://school.programmers.co.kr/learn/courses/30/lessons/59412
-- 09:00부터 19:59까지, 각 시간대별로 입양이 몇 건이나 발생했는지 조회하는 SQL문을 작성
SELECT HOUR(DATETIME) AS HOUR, COUNT(ANIMAL_ID) AS COUNT
FROM ANIMAL_OUTS
GROUP BY HOUR(DATETIME)
HAVING HOUR >= 9 AND HOUR <= 19
ORDER BY HOUR
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://school.programmers.co.kr/learn/courses/30/lessons/164671
SELECT CONCAT('/home/grep/src/', A.BOARD_ID,'/',B.FILE_ID,B.FILE_NAME,B.FILE_EXT) AS FILE_PATH
FROM USED_GOODS_BOARD A JOIN USED_GOODS_FILE B
ON A.BOARD_ID = B.BOARD_ID
WHERE A.VIEWS IN (SELECT MAX(VIEWS) FROM USED_GOODS_BOARD)
ORDER BY FILE_ID DESC;