Skip to content

Commit 67246b8

Browse files
authored
Merge pull request #17 from yeongleej/main
[2주차] 이지영
2 parents bc23554 + 64f7cc5 commit 67246b8

File tree

9 files changed

+491
-0
lines changed

9 files changed

+491
-0
lines changed

BOJ/1000-10000번/JY_1647.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package day0917;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class JY_1647 {
7+
8+
static int N, M;
9+
static int[] parents;
10+
static List<Edge> eList;
11+
static class Edge implements Comparable<Edge>{
12+
int n1, n2, cost;
13+
public Edge(int n1, int n2, int cost) {
14+
this.n1 = n1;
15+
this.n2 = n2;
16+
this.cost = cost;
17+
}
18+
@Override
19+
public int compareTo(Edge other) {
20+
return this.cost - other.cost;
21+
}
22+
@Override
23+
public String toString() {
24+
return "("+this.n1+","+this.n2+"):"+this.cost;
25+
}
26+
}
27+
28+
public static void main(String[] args) throws IOException {
29+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
30+
StringTokenizer st = new StringTokenizer(br.readLine());
31+
32+
N = Integer.parseInt(st.nextToken());
33+
M = Integer.parseInt(st.nextToken());
34+
eList = new ArrayList<>();
35+
for(int i=0; i<M; i++) {
36+
st = new StringTokenizer(br.readLine());
37+
int n1 = Integer.parseInt(st.nextToken());
38+
int n2 = Integer.parseInt(st.nextToken());
39+
int cost = Integer.parseInt(st.nextToken());
40+
41+
eList.add(new Edge(n1, n2, cost));
42+
}
43+
// 엣지 정렬
44+
Collections.sort(eList);
45+
// System.out.println(eList);
46+
47+
// 부모 초기화(자기자신으로)
48+
parents = new int[N+1];
49+
for(int i=1; i<N+1; i++) {
50+
parents[i] = i;
51+
}
52+
53+
List<Edge> g = new ArrayList<>();
54+
// 엣지 반복
55+
for(Edge e: eList) {
56+
// 부모가 같지 않으면 union
57+
if(find(e.n1) != find(e.n2)) {
58+
union(e.n1, e.n2);
59+
g.add(e);
60+
}
61+
}
62+
int dist = 0;
63+
// 마지막 엣지 제거
64+
for(int i=0; i<g.size()-1; i++) {
65+
dist += g.get(i).cost;
66+
}
67+
System.out.println(dist);
68+
69+
70+
71+
}
72+
public static int find(int x) {
73+
if(parents[x] != x) {
74+
parents[x] = find(parents[x]);
75+
}
76+
return parents[x];
77+
}
78+
public static void union(int a, int b) {
79+
int pa = find(a);
80+
int pb = find(b);
81+
if(pa != pb) {
82+
parents[pb] = pa;
83+
}
84+
}
85+
86+
}

BOJ/1000-10000번/JY_2631.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package day0918;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class JY_2631 {
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
int N = Integer.parseInt(br.readLine());
12+
int[] arr = new int[N];
13+
int[] dp = new int[N];
14+
15+
for(int i=0; i<N; i++) {
16+
arr[i] = Integer.parseInt(br.readLine());
17+
dp[i] = 1; // 자기자신의 길이
18+
}
19+
// System.out.println(Arrays.toString(arr));
20+
21+
// 가장 증가하는 부분 수열을 제외한 나머지를 옮겨야 함
22+
for(int i=0; i<N; i++) {
23+
for(int j=0; j<i; j++) {
24+
if(arr[j] < arr[i]) {
25+
dp[i] = Math.max(dp[i], dp[j]+1);
26+
}
27+
}
28+
}
29+
int mLen = 0;
30+
for(int i=0; i<N; i++) {
31+
mLen = Math.max(mLen, dp[i]);
32+
}
33+
// System.out.println(Arrays.toString(dp));
34+
System.out.println(N-mLen);
35+
36+
}
37+
38+
39+
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
static int N;
7+
static int gNum;
8+
static int[][] board;
9+
static Map<Integer, Group> gMap;
10+
static int[][] g;
11+
static boolean[][] visited;
12+
static int[] dx = {0, 0, -1, 1};
13+
static int[] dy = {-1, 1, 0, 0};
14+
static int[][] nb;
15+
static class Pos {
16+
int x, y;
17+
public Pos(int x, int y){
18+
this.x=x;
19+
this.y=y;
20+
}
21+
@Override
22+
public String toString(){
23+
return "("+this.x+","+this.y+")";
24+
}
25+
}
26+
static class Group {
27+
int val, size;
28+
public Group(int val, int size){
29+
this.val = val;
30+
this.size = size;
31+
}
32+
@Override
33+
public String toString(){
34+
return "["+this.val+"]:"+this.size;
35+
}
36+
}
37+
38+
public static void main(String[] args) throws IOException {
39+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
40+
StringTokenizer st = new StringTokenizer(br.readLine());
41+
42+
N = Integer.parseInt(st.nextToken());
43+
board = new int[N][N];
44+
for(int i=0; i<N; i++){
45+
st = new StringTokenizer(br.readLine());
46+
for(int j=0; j<N; j++){
47+
board[i][j] = Integer.parseInt(st.nextToken());
48+
}
49+
}
50+
51+
int ans = 0;
52+
for(int t=0; t<4; t++){
53+
// System.out.println(t);
54+
// print(board);
55+
56+
// 그룹 생성
57+
generateGroup();
58+
// print(g);
59+
// System.out.println(gMap);
60+
61+
// 이웃 그룹 계산 & 예술 점수 계산
62+
ans += calScore();
63+
64+
nb = new int[N][N];
65+
// 십자가 회전
66+
turnTen();
67+
68+
// 정사각형 회전
69+
turnBox(0, 0);
70+
turnBox(N/2+1, 0);
71+
turnBox(0, N/2+1);
72+
turnBox(N/2+1, N/2+1);
73+
74+
// 그래프 변경
75+
for(int i=0; i<N; i++){
76+
for(int j=0; j<N; j++){
77+
board[i][j] = nb[i][j];
78+
}
79+
}
80+
81+
}
82+
System.out.println(ans);
83+
84+
}
85+
public static void print(int[][] arr){
86+
for(int i=0; i<N; i++){
87+
System.out.println(Arrays.toString(arr[i]));
88+
}
89+
System.out.println();
90+
}
91+
// generateGroup(): 그룹 확인 및 생성
92+
public static void generateGroup() {
93+
visited = new boolean[N][N];
94+
gMap = new HashMap<>();
95+
g = new int[N][N];
96+
gNum = 0;
97+
for(int i=0; i<N; i++){
98+
for(int j=0; j<N; j++){
99+
if(!visited[i][j]){
100+
int s = bfs(i, j, board[i][j], gNum);
101+
gMap.put(gNum, new Group(board[i][j], s));
102+
gNum++;
103+
}
104+
}
105+
}
106+
}
107+
public static boolean inRange(int x, int y) {
108+
return x>=0 && x<N && y>=0 && y<N;
109+
}
110+
public static int bfs(int x, int y, int num, int gNum){
111+
visited[x][y] = true;
112+
g[x][y] = gNum;
113+
Queue<Pos> q = new LinkedList<>();
114+
q.add(new Pos(x, y));
115+
int size = 1;
116+
while(!q.isEmpty()){
117+
Pos now = q.poll();
118+
for(int i=0; i<4; i++){
119+
int nx = now.x+dx[i];
120+
int ny = now.y+dy[i];
121+
if(inRange(nx, ny) && !visited[nx][ny] && num == board[nx][ny]){
122+
visited[nx][ny] = true;
123+
g[nx][ny] = gNum;
124+
size++;
125+
q.add(new Pos(nx, ny));
126+
}
127+
}
128+
}
129+
return size;
130+
}
131+
// 이웃 그룹 계산
132+
public static int calScore(){
133+
visited = new boolean[N][N];
134+
int score = 0;
135+
for(int i=0; i<N; i++){
136+
for(int j=0; j<N; j++){
137+
if(!visited[i][j]){
138+
score += find(i, j, g[i][j]);
139+
}
140+
}
141+
}
142+
// System.out.println("score: "+score);
143+
return score / 2;
144+
}
145+
public static int find(int x, int y, int num){
146+
visited[x][y] = true;
147+
int[] neighbors = new int[gNum];
148+
Queue<Pos> q = new LinkedList<>();
149+
q.add(new Pos(x, y));
150+
while(!q.isEmpty()){
151+
Pos now= q.poll();
152+
for(int i=0; i<4; i++){
153+
int nx = now.x+dx[i];
154+
int ny = now.y+dy[i];
155+
if(inRange(nx, ny)){
156+
if(g[nx][ny] == num && !visited[nx][ny]){
157+
visited[nx][ny] = true;
158+
q.add(new Pos(nx, ny));
159+
}
160+
else if(g[nx][ny] != num){
161+
neighbors[g[nx][ny]]++;
162+
}
163+
}
164+
}
165+
}
166+
// 예술 점수 계산
167+
int ans = 0;
168+
Group g1 = gMap.get(num);
169+
for(int i=0; i<gNum; i++){
170+
if(neighbors[i] != 0){
171+
Group g2 = gMap.get(i);
172+
ans += cal(g1, g2, neighbors[i]);
173+
}
174+
}
175+
return ans;
176+
}
177+
public static int cal(Group g1, Group g2, int sides){
178+
return (g1.size+g2.size)*g1.val*g2.val*sides;
179+
}
180+
// 십자가 회전
181+
public static void turnTen(){
182+
// 세로 줄 회전
183+
for(int i=0; i<N; i++){
184+
nb[N/2][i] = board[i][N/2];
185+
}
186+
// 가로 줄 회전
187+
for(int j=0; j<N; j++){
188+
nb[N-j-1][N/2] = board[N/2][j];
189+
}
190+
}
191+
public static void turnBox(int sx, int sy) {
192+
int size = N / 2;
193+
for(int i=sx; i<sx+size; i++){
194+
for(int j=sy; j<sy+size; j++){
195+
// (0, 0)으로 옮겨주기
196+
int ox = i-sx;
197+
int oy = j-sy;
198+
// x, y -> y, size-1-x
199+
int rx = oy;
200+
int ry = size-1-ox;
201+
nb[rx+sx][ry+sy] = board[i][j];
202+
}
203+
}
204+
}
205+
}

Programmers/Level2/JY_12913.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int solution(int[][] land) {
5+
int answer = 0;
6+
int N = land.length;
7+
int[][] dp = new int[N][4];
8+
9+
// 초기화
10+
for(int j=0; j<4; j++){
11+
dp[0][j] = land[0][j];
12+
}
13+
14+
for(int i=1; i<N; i++){
15+
for(int j=0; j<4; j++) {
16+
for(int k = 0; k<4; k++){
17+
if(j != k){
18+
dp[i][j] = Math.max(dp[i][j], dp[i-1][k]+land[i][j]);
19+
}
20+
}
21+
}
22+
}
23+
24+
25+
for(int j=0; j<4; j++){
26+
answer = Math.max(answer, dp[N-1][j]);
27+
}
28+
29+
return answer;
30+
}
31+
}

Programmers/Level2/JY_42584.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
class Solution {
3+
static class State {
4+
int price, time;
5+
public State(int price, int time){
6+
this.price = price;
7+
this.time = time;
8+
}
9+
}
10+
public int[] solution(int[] prices) {
11+
int N = prices.length;
12+
int[] answer = new int[N];
13+
Stack<State> stack = new Stack<>();
14+
for(int i=0; i<N; i++){
15+
// top보다 작은 가격이 나올때까지 반복
16+
while(!stack.isEmpty() && stack.peek().price > prices[i]){
17+
State now = stack.pop();
18+
answer[now.time] = i-now.time;
19+
}
20+
stack.add(new State(prices[i], i));
21+
}
22+
// 마지막까지 떨어지지 않은 가격들 처리
23+
while(!stack.isEmpty()){
24+
State now = stack.pop();
25+
answer[now.time] = (N-1)-now.time;
26+
}
27+
return answer;
28+
}
29+
}

0 commit comments

Comments
 (0)