-
Notifications
You must be signed in to change notification settings - Fork 4
[8주차] 이혜원 #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8주차] 이혜원 #104
Changes from all commits
88fd449
8932a6d
ca32f61
fb14a54
e2d4f01
11f5d32
2b2b1c2
aac01e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; // 덮은 위치 갱신 | ||
} | ||
System.out.println(cnt); | ||
} | ||
} |
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); | ||
} | ||
} |
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)); | ||
} | ||
} | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모든 영역에 불이 붙는 경우(빈 영역이 없는 경우) 밖에 없을수도 있으니깐 max값은 |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모든 |
||
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; | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
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; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
완전 정석적인 코드 같아여~~ 굿 👍