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 62e00a7

Browse files
authored
Merge pull request #302 from yeongleej/main
[22주차] 이지영
2 parents 49ec972 + 2a48112 commit 62e00a7

File tree

6 files changed

+367
-0
lines changed

6 files changed

+367
-0
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_1707 {
4+
5+
static int T, N, M;
6+
static List<Integer>[] g;
7+
static boolean[] visited;
8+
static int[] crr;
9+
static boolean isOk;
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+
15+
T = Integer.parseInt(st.nextToken());
16+
StringBuilder sb = new StringBuilder();
17+
for(int t=0; t<T; t++) {
18+
st = new StringTokenizer(br.readLine());
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
g = new ArrayList[N+1];
23+
for(int i=1; i<N+1; i++) {
24+
g[i] = new ArrayList<>();
25+
}
26+
27+
for(int i=0; i<M; i++) {
28+
st = new StringTokenizer(br.readLine());
29+
int a = Integer.parseInt(st.nextToken());
30+
int b = Integer.parseInt(st.nextToken());
31+
g[a].add(b);
32+
g[b].add(a);
33+
}
34+
35+
visited = new boolean[N+1];
36+
crr = new int[N+1]; // 집합 번호 저장 배열
37+
isOk = true;
38+
for(int i=1; i<N+1; i++) {
39+
if(visited[i]) continue;
40+
dfs(i, 1);
41+
if(!isOk) break;
42+
43+
}
44+
45+
if(isOk) sb.append("YES\n");
46+
else sb.append("NO\n");
47+
48+
}
49+
50+
System.out.println(sb.toString());
51+
52+
}
53+
public static void dfs(int now, int group) {
54+
crr[now] = group;
55+
visited[now] = true;
56+
57+
for(int next: g[now]) {
58+
// 인접 노드가 현재 노드의 그룹과 같다면 fail
59+
if(crr[next] == group) {
60+
isOk = false;
61+
return;
62+
}
63+
64+
// 방문 X
65+
if(!visited[next]) {
66+
dfs(next, -group);
67+
}
68+
69+
}
70+
}
71+
72+
73+
}

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_16724 {
4+
5+
static int N, M;
6+
static char[][] g;
7+
static int[] parent;
8+
// 상 하 좌 우
9+
static int[] dx = {-1, 1, 0, 0};
10+
static int[] dy = {0, 0, -1, 1};
11+
static Map<Character, Integer> cMap;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
M = Integer.parseInt(st.nextToken());
19+
g = new char[N][M];
20+
21+
parent = new int[N*M];
22+
for(int i=0; i<N; i++) {
23+
String line = br.readLine();
24+
for(int j=0; j<M; j++) {
25+
g[i][j] = line.charAt(j);
26+
parent[i*M+j] = i*M+j;
27+
}
28+
}
29+
30+
cMap = new HashMap<>();
31+
cMap.put('U', 0);
32+
cMap.put('D', 1);
33+
cMap.put('L', 2);
34+
cMap.put('R', 3);
35+
36+
for(int i=0; i<N; i++) {
37+
for(int j=0; j<M; j++) {
38+
int dir = cMap.get(g[i][j]);
39+
int nx = i + dx[dir];
40+
int ny = j + dy[dir];
41+
if(find(i*M+j) != find(nx*M+ny)) {
42+
union(i*M+j, nx*M+ny);
43+
}
44+
}
45+
}
46+
47+
Set<Integer> cSet = new HashSet<>();
48+
for(int i=0; i<N*M; i++) {
49+
// a <- b <- c 관계에서 모두 parent 값은 a인데
50+
// a의 parent 값이 변경되면 b, c는 변경되지 않으므로 경로압축을 통한 최종 부모를 찾아줘야 함
51+
// ** 최종 부모를 찾아주기 위해서 find()후 삽입
52+
cSet.add(find(parent[i]));
53+
}
54+
System.out.println(cSet.size());
55+
56+
57+
}
58+
public static int find(int x) {
59+
if(x != parent[x]) {
60+
parent[x] = find(parent[x]);
61+
}
62+
return parent[x];
63+
}
64+
public static void union(int a, int b) {
65+
int pa = find(a);
66+
int pb = find(b);
67+
68+
if(pa < pb) {
69+
parent[pb] = pa;
70+
} else {
71+
parent[pa] = pb;
72+
}
73+
}
74+
75+
}

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class JY_18430 {
5+
6+
static int N, M;
7+
static int[][] g;
8+
// 상 좌 하 우
9+
static int[] dx = {-1, 0, 1, 0};
10+
static int[] dy = {0, -1, 0, 1};
11+
static boolean[][] visited;
12+
static int ans;
13+
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
21+
g = new int[N][M];
22+
for(int i=0; i<N; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
for(int j=0; j<M; j++) {
25+
g[i][j] = Integer.parseInt(st.nextToken());
26+
}
27+
}
28+
29+
visited = new boolean[N][M];
30+
ans = 0;
31+
dfs(0, 0, 0);
32+
33+
System.out.println(ans);
34+
}
35+
public static boolean inRange(int x, int y) {
36+
return x>=0 && x<N && y>=0 && y<M;
37+
}
38+
public static void dfs(int x, int y, int score) {
39+
// 다음 행 진행
40+
if(y == M) {
41+
x += 1;
42+
y = 0;
43+
}
44+
if(x == N) {
45+
ans = Math.max(ans, score);
46+
return;
47+
}
48+
// (x, y) 방문 O
49+
if(!visited[x][y]) {
50+
// 부메랑 반복
51+
for(int d=0; d<4; d++) {
52+
int ax = x + dx[d];
53+
int ay = y + dy[d];
54+
int bx = x + dx[(d+1)%4];
55+
int by = y + dy[(d+1)%4];
56+
if(!inRange(ax, ay) || !inRange(bx, by)) continue;
57+
if(visited[ax][ay] || visited[bx][by]) continue;
58+
visited[x][y] = true;
59+
visited[ax][ay] = true;
60+
visited[bx][by] = true;
61+
dfs(x,y+1, score + (g[x][y]*2 +g[ax][ay] + g[bx][by]));
62+
visited[x][y] = false;
63+
visited[ax][ay] = false;
64+
visited[bx][by] = false;
65+
}
66+
}
67+
// (x, y) 방문 X
68+
dfs(x, y+1, score);
69+
70+
}
71+
72+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_병원_거리_최소화하기 {
4+
5+
static int N, M;
6+
static int[][] g;
7+
static Map<Integer, Hospital> hMap;
8+
static List<Integer> tList;
9+
static int ans;
10+
static class Hospital {
11+
int num, x, y;
12+
13+
public Hospital(int num, int x, int y) {
14+
super();
15+
this.num = num;
16+
this.x = x;
17+
this.y = y;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Hospital [num=" + num + ", x=" + x + ", y=" + y + "]";
23+
}
24+
25+
}
26+
27+
public static void main(String[] args) throws IOException{
28+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
StringTokenizer st = new StringTokenizer(br.readLine());
30+
31+
N = Integer.parseInt(st.nextToken());
32+
M = Integer.parseInt(st.nextToken());
33+
34+
g = new int[N][N];
35+
hMap = new HashMap<>();
36+
int n = 0;
37+
for(int i=0; i<N; i++) {
38+
st = new StringTokenizer(br.readLine());
39+
for(int j=0; j<N; j++) {
40+
g[i][j] = Integer.parseInt(st.nextToken());
41+
if(g[i][j] == 2) {
42+
hMap.put(n, new Hospital(n, i, j));
43+
n++;
44+
}
45+
}
46+
}
47+
48+
tList = new ArrayList<>();
49+
ans = Integer.MAX_VALUE;
50+
// 최대 경우의 수 : 100(사람수) * 13C7 * 7
51+
comb(0, 0);
52+
53+
System.out.println(ans);
54+
}
55+
public static void comb(int depth, int start) {
56+
if(depth == M) {
57+
int score = find(tList);
58+
ans = Math.min(ans, score);
59+
return;
60+
}
61+
for(int i=start; i<hMap.size(); i++) {
62+
tList.add(i);
63+
comb(depth+1, i+1);
64+
tList.remove(tList.size()-1);
65+
}
66+
}
67+
public static int cal(int x1, int y1, int x2, int y2) {
68+
return Math.abs(x1-x2) + Math.abs(y1-y2);
69+
}
70+
public static int find(List<Integer> hList) {
71+
int total = 0;
72+
// 사람 반복
73+
for(int i=0; i<N; i++) {
74+
for(int j=0; j<N; j++) {
75+
if(g[i][j] != 1) continue;
76+
int tmp = Integer.MAX_VALUE;
77+
// 병원 반복
78+
for(int h : hList) {
79+
Hospital now = hMap.get(h);
80+
int dist = cal(i, j, now.x, now.y);
81+
tmp = Math.min(tmp, dist);
82+
}
83+
total += tmp;
84+
}
85+
}
86+
87+
return total;
88+
}
89+
90+
}

‎Programmers/Level3/JY_67258.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
class JY_67258 {
3+
public int[] solution(String[] gems) {
4+
int[] answer = new int[2];
5+
6+
// 총 보석 종류구하기
7+
Set<String> gSet = new HashSet<>();
8+
for(String g: gems) {
9+
gSet.add(g);
10+
}
11+
int total = gSet.size();
12+
13+
14+
int minR = Integer.MAX_VALUE;
15+
// 투포인터 활용
16+
int s = 0;
17+
int e = s;
18+
Map<String, Integer> gMap = new HashMap<>();
19+
while(e < gems.length) {
20+
// System.out.println(">> now: "+gMap+", s:"+s+",e:"+e);
21+
// 보석 추가
22+
gMap.put(gems[e], gMap.getOrDefault(gems[e], 0)+1);
23+
24+
// 현재 구간(gMap의 보석들)이 모든 보석을 포함함
25+
// 총 보석의 개수가 total보다 작을 떄까지 s증가
26+
while(gMap.keySet().size() == total) {
27+
// 더 짧은 구간
28+
if(minR > (e-s)) {
29+
minR = (e-s);
30+
answer[0] = s+1;
31+
answer[1] = e+1;
32+
}
33+
// s지점의 보석 감소
34+
gMap.put(gems[s], gMap.get(gems[s])-1);
35+
// 감소 후, gems[s]의 개수가 0이된다면 gMap에서 삭제
36+
if(gMap.get(gems[s]) == 0) gMap.remove(gems[s]);
37+
s++;
38+
}
39+
40+
e++;
41+
42+
}
43+
44+
return answer;
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- 식품분류별 가장 비싼 식품의 정보 조회하기
2+
-- https://school.programmers.co.kr/learn/courses/30/lessons/131116
3+
SELECT CATEGORY, PRICE AS MAX_PRICE, PRODUCT_NAME
4+
FROM FOOD_PRODUCT
5+
WHERE (CATEGORY, PRICE) IN (
6+
SELECT CATEGORY, MAX(PRICE)
7+
FROM FOOD_PRODUCT
8+
WHERE CATEGORY IN('과자', '', '김치', '식용유')
9+
GROUP BY CATEGORY
10+
)
11+
ORDER BY MAX_PRICE DESC

0 commit comments

Comments
(0)

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