Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90d818c

Browse files
Merge pull request #156 from yeahdy/main
[11주차] 이예진
2 parents a221906 + b810971 commit 90d818c

File tree

9 files changed

+357
-11
lines changed

9 files changed

+357
-11
lines changed

‎BOJ/1000-5000번/YJ_1052.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_1052 {
5+
//반례 10 3?
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int K = Integer.parseInt(st.nextToken());
11+
12+
//K-1 개 만큼 물 합치기
13+
for(int i=0; i<K-1; i++){
14+
int count = 0;
15+
while(Math.pow(2,count) < N){ //N개의 물병을 넘지 않는 최대 제곱
16+
count++;
17+
}
18+
N -= (int) Math.pow(2,count-1); //물병 1개를 만들 수 있는 최대양
19+
if(N == 0) {
20+
break;
21+
}
22+
}
23+
24+
if(N == 0){
25+
System.out.println(-1);
26+
}else{
27+
//마지막 1개는 남은 양을 하나로 만들기
28+
int count = 0;
29+
while(Math.pow(2,count) < N){
30+
count++;
31+
}
32+
System.out.println((int) Math.pow(2,count) - N);
33+
}
34+
}
35+
}

‎BOJ/1000-5000번/YJ_1761.java‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
//LCA(최소 공통조상)+DFS
5+
//N(2 ≤ N ≤ 40,000)
6+
public class YJ_1761 {
7+
static class Node{
8+
int num;
9+
int distance;
10+
Node(int num, int distance){
11+
this.num = num;
12+
this.distance = distance;
13+
}
14+
}
15+
16+
static int n;
17+
static int m;
18+
static int[][] dp;
19+
static int[] depth;
20+
static int[] distances; //노드 1~N 까지의 합
21+
static boolean[] visited;
22+
static List<List<Node>> graph = new ArrayList<>();
23+
24+
static final int MAX = 16; //트리의 높이 log2^N 으로 최대 log2^40000 은 15.3
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+
n = Integer.parseInt(st.nextToken());
30+
31+
//초기화
32+
dp = new int[n+1][MAX+1];
33+
depth = new int[n+1];
34+
distances = new int[n+1];
35+
visited = new boolean[n+1];
36+
for(int i=0; i<n+1; i++){
37+
graph.add(new ArrayList<>());
38+
}
39+
//그래프 모델링
40+
for(int i=0; i<n-1; i++){
41+
st = new StringTokenizer(br.readLine());
42+
int to = Integer.parseInt(st.nextToken());
43+
int from = Integer.parseInt(st.nextToken());
44+
int distance = Integer.parseInt(st.nextToken());
45+
graph.get(to).add(new Node(from, distance));
46+
graph.get(from).add(new Node(to, distance));
47+
}
48+
dfs(1, 0);
49+
saveParents();
50+
//정답 출력
51+
m = Integer.parseInt(br.readLine());
52+
while(m-- > 0){
53+
st = new StringTokenizer(br.readLine());
54+
int to = Integer.parseInt(st.nextToken());
55+
int from = Integer.parseInt(st.nextToken());
56+
int common = lca(to, from); //루트와 가장 가까운 공통 조상 찾기
57+
System.out.println(distances[to] + distances[from] - (2*distances[common]));
58+
}
59+
}
60+
61+
static void dfs(int to, int from){
62+
visited[to] = true; //★현재 시작점에 방문체크
63+
depth[to] = depth[from] + 1;
64+
65+
for(Node next : graph.get(to)){
66+
if(!visited[next.num]){ //★현재와 이어진 다음 노드의 방문 유무 확인
67+
visited[next.num] = true;
68+
dp[next.num][0] = to; //각 노드 별 직속 조상 저장으로 초기화
69+
distances[next.num] = distances[to] + next.distance; //다음 노드에서 정점까지의 누적거리
70+
dfs(next.num,to);
71+
}
72+
}
73+
}
74+
75+
//전체 트리에서 각 노드별로 다중 차수 부모 노드들 저장
76+
static void saveParents(){
77+
for(int from=1; from<MAX; from++){
78+
for(int to=1; to<n+1; to++){
79+
int parent = dp[to][from-1];
80+
dp[to][from] = dp[parent][from-1];
81+
}
82+
}
83+
}
84+
85+
static int lca(int to, int from){
86+
if(depth[to] > depth[from]){ // from 이 항상 더 깊은 노드로 기준점 잡기
87+
int temp = to;
88+
to = from;
89+
from = temp; //더 깊은 노드로 교체
90+
}
91+
//각 노드 간 레벨이 다를 경우 깊이 맞추기
92+
for(int i = MAX; i>=0; i--){
93+
if(Math.pow(2,i) <= depth[from] - depth[to]){ //Math.pow 를 통해 깊이 레벨을 표현 (하나의 레벨에 있는 모든 가짓수)
94+
from = dp[from][i]; // 더 깊은 노드인 from 를 위로 올림
95+
}
96+
}
97+
//깊이 레벨이 같고, 부모가 같다면 공통 조상 반환(to 를 반환해도 됨)
98+
if(to == from){
99+
return from;
100+
}
101+
//깊이가 같은 상태지만, 부모가 다른 경우 루트와 가장 가까운 레벨의 공통 조상 찾기
102+
for(int i=MAX; i>=0; i--){
103+
if(dp[to][i] != dp[from][i]){
104+
//to와 from의 부모가 다르면 상위 레벨로 계속 이동
105+
to = dp[to][i];
106+
from = dp[from][i];
107+
}
108+
}
109+
return dp[from][0]; //to와 from의 부모가 같기 때문에 dp[to][0]를 반환해도 됨
110+
}
111+
112+
}

‎BOJ/15001-20000번/YJ_15831.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_15831 {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringTokenizer st = new StringTokenizer(br.readLine());
8+
int N = Integer.parseInt(st.nextToken());
9+
int B = Integer.parseInt(st.nextToken());
10+
int W = Integer.parseInt(st.nextToken());
11+
String[] road = br.readLine().split("");
12+
13+
int result = 0;
14+
int bCount = 0;
15+
int wCount = 0;
16+
int start = 0;
17+
int end = 0;
18+
while(end < N){
19+
if(bCount > B){ //b 갯수를 초과한 경우 start 움직이기
20+
if("W".equals(road[start])){
21+
--wCount;
22+
}else{
23+
--bCount;
24+
}
25+
start++;
26+
} else { //b 갯수보다 작거나 같은 경우 end 움직이기
27+
if("W".equals(road[end])){
28+
wCount++;
29+
}else{
30+
bCount++;
31+
}
32+
end++;
33+
}
34+
35+
if(wCount >= W && bCount <= B) { //조건이 부합하는 경우 산책길 길이 갱신
36+
result = Math.max(result, end - start);
37+
}
38+
}
39+
40+
System.out.println(result);
41+
}
42+
}

‎BOJ/15001-20000번/YJ_19939.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//2 <= N <= 10^5
5+
//누적합
6+
public class YJ_19939 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
int N = Integer.parseInt(st.nextToken());
11+
int K = Integer.parseInt(st.nextToken());
12+
13+
int[] dp = new int[K+1];
14+
for(int i=1; i<K+1; i++){
15+
dp[i] = dp[i-1] + i;
16+
}
17+
18+
int minCount = dp[K];
19+
if(N < minCount){
20+
System.out.println(-1);
21+
}else{
22+
int result = (N-minCount) % K;
23+
if(result == 0){
24+
System.out.println(K-1);
25+
}else{
26+
System.out.println(K); //K+result - (result*1)
27+
}
28+
}
29+
}
30+
}

‎BOJ/5001-10000번/YJ_9466.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//2 ≤ n ≤ 10^5
5+
public class YJ_9466 {
6+
static int[] graph;
7+
static boolean[] visited;
8+
static boolean[] isFinished;
9+
static boolean[] isGrouping;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
int T = Integer.parseInt(st.nextToken());
15+
16+
//테스트케이스 실행
17+
while(T-- > 0){
18+
//초기화
19+
int n = Integer.parseInt(br.readLine());
20+
graph = new int[n+1];
21+
isFinished = new boolean[n+1];
22+
visited = new boolean[n+1];
23+
isGrouping = new boolean[n+1];
24+
st = new StringTokenizer(br.readLine());
25+
for(int i=1; i<n+1; i++){
26+
graph[i] = Integer.parseInt(st.nextToken());
27+
}
28+
//dfs 탐색
29+
for(int i=1; i<n+1; i++){
30+
if(!visited[i]){
31+
dfs(i);
32+
}
33+
}
34+
//프로젝트 팀에 속하지 못한 학생 수
35+
int count = 0;
36+
for(int i=1; i<n+1; i++){
37+
if(!isGrouping[i]){
38+
count++;
39+
}
40+
}
41+
System.out.println(count);
42+
}
43+
44+
}
45+
46+
public static void dfs(int current){
47+
visited[current] = true;
48+
int next= graph[current];
49+
50+
if(!visited[next]){
51+
dfs(next);
52+
} else if(!isFinished[next]) { //★재방문이지만 이전에 사이클을 이미 만들었거나, 사이클 형성을 못한 경우
53+
//싸이클 형성 가능 또는 자기자신
54+
while(!isGrouping[next]){
55+
isGrouping[next] = true;
56+
next = graph[next];
57+
}
58+
}
59+
isFinished[current] = true;
60+
}
61+
62+
}

‎Programmers/Level2/YJ_142085.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Collections;
2+
import java.util.PriorityQueue;
3+
4+
public class YJ_142085 {
5+
public int solution(int n, int k, int[] enemy) {
6+
int answer = 0;
7+
8+
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
9+
for (int e : enemy) {
10+
if (n < e && k == 0) {
11+
break;
12+
}
13+
14+
pq.offer(e);
15+
if (n < e) {
16+
n += pq.poll();
17+
k--;
18+
}
19+
n -= e;
20+
answer++;
21+
}
22+
23+
return answer;
24+
}
25+
}

‎Programmers/Level3/YJ_150366.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public String[] solution(String[] commands) throws IOException {
1212
for(int i=0; i<MAX; i++){
1313
parents[i] = i;
1414
}
15-
Arrays.fill(table,"");
1615

1716
for(String command : commands){
1817
StringTokenizer st = new StringTokenizer(command);
@@ -21,13 +20,14 @@ public String[] solution(String[] commands) throws IOException {
2120
switch(message){
2221
case "UPDATE":
2322
String data = st.nextToken();
24-
boolean isNumber = data!= null && data.chars().allMatch(Character::isDigit);
25-
if(isNumber){
23+
//알파벳 소문자와 숫자
24+
String data1 = st.nextToken();
25+
if(st.hasMoreTokens()){
2626
update(Integer.parseInt(data),
27-
Integer.parseInt(st.nextToken()),
27+
Integer.parseInt(data1),
2828
st.nextToken());
2929
}else{
30-
update(data,st.nextToken());
30+
update(data,data1);
3131
}
3232
break;
3333

@@ -90,7 +90,7 @@ public void unmerge(int r, int c){
9090
for(int i=0; i<table.length; i++){
9191
if(parents[i] == parent){
9292
parents[i] = i; //부모에서 벗어나기
93-
table[i] = ""; //병합 해제 후 셀 초기화
93+
table[i] = null; //병합 해제 후 셀 초기화
9494
}
9595
}
9696
table[index] = value;
@@ -99,7 +99,7 @@ public void unmerge(int r, int c){
9999
//PRINT r c
100100
public void print(int r, int c){
101101
int parent = find(getIndex(r, c));
102-
String result = table[parent].isBlank()? "EMPTY" : table[parent];
102+
String result = Objects.isNull(table[parent])? "EMPTY" : table[parent];
103103
prints.add(result);
104104
}
105105

@@ -113,16 +113,16 @@ public int find(int num){
113113

114114
//셀 병합하기
115115
public void union(int num1, int num2){
116-
num1 = find(num1);
117-
num2 = find(num2);
116+
num1 = find(num1);//A
117+
num2 = find(num2);//A
118118

119119
if(num1 == num2){ //이미 병합된 경우
120120
return;
121121
}
122122

123-
table[num1] = table[num1].isBlank()? table[num2] : table[num1];
123+
table[num1] = Objects.isNull(table[num1])? table[num2] : table[num1];
124124
parents[num2] = num1;
125-
table[num2] = "";
125+
table[num2] = null;
126126
}
127127

128128
private int getIndex(int r, int c){
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SELECT ROUND(SUM(tiv_2016),2) tiv_2016
2+
FROM (
3+
SELECT
4+
i.pid, i.tiv_2016
5+
FROM Insurance i
6+
INNER JOIN Insurance sub
7+
ON i.tiv_2015 = sub.tiv_2015
8+
AND i.pid != sub.pid
9+
LEFT JOIN (
10+
SELECT pid, lat, lon
11+
FROM Insurance
12+
) exclude
13+
ON i.lat = exclude.lat AND i.lon = exclude.lon AND i.pid != exclude.pid
14+
WHERE exclude.pid IS NULL
15+
GROUP BY i.pid, i.tiv_2016
16+
) SUB;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /