Skip to content

[2주차] 이지영 #17

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

Merged
merged 9 commits into from
Sep 22, 2024
86 changes: 86 additions & 0 deletions BOJ/1000-10000번/JY_1647.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package day0917;

import java.util.*;
import java.io.*;

public class JY_1647 {

static int N, M;
static int[] parents;
static List<Edge> eList;
static class Edge implements Comparable<Edge>{
int n1, n2, cost;
public Edge(int n1, int n2, int cost) {
this.n1 = n1;
this.n2 = n2;
this.cost = cost;
}
@Override
public int compareTo(Edge other) {
return this.cost - other.cost;
}
@Override
public String toString() {
return "("+this.n1+","+this.n2+"):"+this.cost;
}
}

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());
eList = new ArrayList<>();
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());

eList.add(new Edge(n1, n2, cost));
}
// 엣지 정렬
Collections.sort(eList);
// System.out.println(eList);

// 부모 초기화(자기자신으로)
parents = new int[N+1];
for(int i=1; i<N+1; i++) {
parents[i] = i;
}

List<Edge> g = new ArrayList<>();
// 엣지 반복
for(Edge e: eList) {
// 부모가 같지 않으면 union
if(find(e.n1) != find(e.n2)) {
union(e.n1, e.n2);
g.add(e);
}
}
int dist = 0;
// 마지막 엣지 제거
for(int i=0; i<g.size()-1; i++) {
dist += g.get(i).cost;
}
System.out.println(dist);



}
public static int find(int x) {
if(parents[x] != x) {
parents[x] = find(parents[x]);
}
return parents[x];
}
public static void union(int a, int b) {
int pa = find(a);
int pb = find(b);
if(pa != pb) {
parents[pb] = pa;
}
}

}
39 changes: 39 additions & 0 deletions BOJ/1000-10000번/JY_2631.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package day0918;

import java.util.*;
import java.io.*;

public class JY_2631 {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
int[] dp = new int[N];

for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(br.readLine());
dp[i] = 1; // 자기자신의 길이
}
// System.out.println(Arrays.toString(arr));

// 가장 증가하는 부분 수열을 제외한 나머지를 옮겨야 함
for(int i=0; i<N; i++) {
for(int j=0; j<i; j++) {
if(arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], dp[j]+1);
}
}
}
int mLen = 0;
for(int i=0; i<N; i++) {
mLen = Math.max(mLen, dp[i]);
}
// System.out.println(Arrays.toString(dp));
System.out.println(N-mLen);

}


}
205 changes: 205 additions & 0 deletions CodeTree/2021-2022년/JY_예술성.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import java.util.*;
import java.io.*;

public class Main {

static int N;
static int gNum;
static int[][] board;
static Map<Integer, Group> gMap;
static int[][] g;
static boolean[][] visited;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {-1, 1, 0, 0};
static int[][] nb;
static class Pos {
int x, y;
public Pos(int x, int y){
this.x=x;
this.y=y;
}
@Override
public String toString(){
return "("+this.x+","+this.y+")";
}
}
static class Group {
int val, size;
public Group(int val, int size){
this.val = val;
this.size = size;
}
@Override
public String toString(){
return "["+this.val+"]:"+this.size;
}
}

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());
board = new int[N][N];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++){
board[i][j] = Integer.parseInt(st.nextToken());
}
}

int ans = 0;
for(int t=0; t<4; t++){
// System.out.println(t);
// print(board);

// 그룹 생성
generateGroup();
// print(g);
// System.out.println(gMap);

// 이웃 그룹 계산 & 예술 점수 계산
ans += calScore();

nb = new int[N][N];
// 십자가 회전
turnTen();

// 정사각형 회전
turnBox(0, 0);
turnBox(N/2+1, 0);
turnBox(0, N/2+1);
turnBox(N/2+1, N/2+1);

// 그래프 변경
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
board[i][j] = nb[i][j];
}
}

}
System.out.println(ans);

}
public static void print(int[][] arr){
for(int i=0; i<N; i++){
System.out.println(Arrays.toString(arr[i]));
}
System.out.println();
}
// generateGroup(): 그룹 확인 및 생성
public static void generateGroup() {
visited = new boolean[N][N];
gMap = new HashMap<>();
g = new int[N][N];
gNum = 0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(!visited[i][j]){
int s = bfs(i, j, board[i][j], gNum);
gMap.put(gNum, new Group(board[i][j], s));
gNum++;
}
}
}
}
public static boolean inRange(int x, int y) {
return x>=0 && x<N && y>=0 && y<N;
}
public static int bfs(int x, int y, int num, int gNum){
visited[x][y] = true;
g[x][y] = gNum;
Queue<Pos> q = new LinkedList<>();
q.add(new Pos(x, y));
int size = 1;
while(!q.isEmpty()){
Pos now = q.poll();
for(int i=0; i<4; i++){
int nx = now.x+dx[i];
int ny = now.y+dy[i];
if(inRange(nx, ny) && !visited[nx][ny] && num == board[nx][ny]){
visited[nx][ny] = true;
g[nx][ny] = gNum;
size++;
q.add(new Pos(nx, ny));
}
}
}
return size;
}
// 이웃 그룹 계산
public static int calScore(){
visited = new boolean[N][N];
int score = 0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(!visited[i][j]){
score += find(i, j, g[i][j]);
}
}
}
// System.out.println("score: "+score);
return score / 2;
}
public static int find(int x, int y, int num){
visited[x][y] = true;
int[] neighbors = new int[gNum];
Queue<Pos> q = new LinkedList<>();
q.add(new Pos(x, y));
while(!q.isEmpty()){
Pos now= q.poll();
for(int i=0; i<4; i++){
int nx = now.x+dx[i];
int ny = now.y+dy[i];
if(inRange(nx, ny)){
if(g[nx][ny] == num && !visited[nx][ny]){
visited[nx][ny] = true;
q.add(new Pos(nx, ny));
}
else if(g[nx][ny] != num){
neighbors[g[nx][ny]]++;
}
}
}
}
// 예술 점수 계산
int ans = 0;
Group g1 = gMap.get(num);
for(int i=0; i<gNum; i++){
if(neighbors[i] != 0){
Group g2 = gMap.get(i);
ans += cal(g1, g2, neighbors[i]);
}
}
return ans;
}
public static int cal(Group g1, Group g2, int sides){
return (g1.size+g2.size)*g1.val*g2.val*sides;
}
// 십자가 회전
public static void turnTen(){
// 세로 줄 회전
for(int i=0; i<N; i++){
nb[N/2][i] = board[i][N/2];
}
// 가로 줄 회전
for(int j=0; j<N; j++){
nb[N-j-1][N/2] = board[N/2][j];
}
}
public static void turnBox(int sx, int sy) {
Copy link
Contributor

Choose a reason for hiding this comment

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

전 회전에서 어려움이 많았는데 깔끔하게 푸셨네요!!
코드리뷰때 해설 기대하겠습니다...!

Copy link
Contributor

Choose a reason for hiding this comment

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

회전 구현 너무 어려웠는데 저도 기대하겠습니다..🫥

Copy link
Contributor

Choose a reason for hiding this comment

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

222222,,,,

int size = N / 2;
for(int i=sx; i<sx+size; i++){
for(int j=sy; j<sy+size; j++){
// (0, 0)으로 옮겨주기
int ox = i-sx;
int oy = j-sy;
// x, y -> y, size-1-x
int rx = oy;
int ry = size-1-ox;
nb[rx+sx][ry+sy] = board[i][j];
}
}
}
}
31 changes: 31 additions & 0 deletions Programmers/Level2/JY_12913.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.*;

class Solution {
int solution(int[][] land) {
int answer = 0;
int N = land.length;
int[][] dp = new int[N][4];

// 초기화
for(int j=0; j<4; j++){
dp[0][j] = land[0][j];
}

for(int i=1; i<N; i++){
for(int j=0; j<4; j++) {
for(int k = 0; k<4; k++){
if(j != k){
dp[i][j] = Math.max(dp[i][j], dp[i-1][k]+land[i][j]);
Copy link
Contributor

Choose a reason for hiding this comment

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

직관적이라서 보기 좋아요!

}
}
}
}


for(int j=0; j<4; j++){
answer = Math.max(answer, dp[N-1][j]);
}

return answer;
}
}
29 changes: 29 additions & 0 deletions Programmers/Level2/JY_42584.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;
class Solution {
static class State {
int price, time;
public State(int price, int time){
this.price = price;
this.time = time;
}
}
public int[] solution(int[] prices) {
int N = prices.length;
int[] answer = new int[N];
Stack<State> stack = new Stack<>();
for(int i=0; i<N; i++){
// top보다 작은 가격이 나올때까지 반복
while(!stack.isEmpty() && stack.peek().price > prices[i]){
State now = stack.pop();
answer[now.time] = i-now.time;
}
stack.add(new State(prices[i], i));
}
// 마지막까지 떨어지지 않은 가격들 처리
while(!stack.isEmpty()){
State now = stack.pop();
answer[now.time] = (N-1)-now.time;
}
return answer;
}
}
Loading