Skip to content
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

김기찬 / 10월 5주차 / 월 #305

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

TearofCoding
Copy link
Contributor

🎈boj 22865 - 가장먼곳


🗨 해결방법 :


친구의 집을 시작점으로 다익스트라를 사용하여 각 지점까지의 최소를 구한다 이를 친구마다 반복한다 그 중 가장 먼 곳이 나의 집이다

📝메모 :


플로이드 워셜을 사용하면 터진다

✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

//22865번
public class 가장먼곳 {
	private static int n;
	private static ArrayList<ArrayList<Node>> dist;
	private static int[] minDist;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//땅 후보의 개수
		n = Integer.parseInt(br.readLine());
		// A, B, C가 사는 위치
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int[] friend = new int[3];
		for(int i = 0; i < 3; i++) {
			friend[i] = Integer.parseInt(st.nextToken());
		}
		//도로의 개수
		int m = Integer.parseInt(br.readLine());
		
		dist = new ArrayList<>();
		for(int i = 0; i <= n; i++) {
			dist.add(new ArrayList<>());
		}
		
		for(int i = 0; i < m; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int from = Integer.parseInt(st.nextToken());
			int to = Integer.parseInt(st.nextToken());
			int len = Integer.parseInt(st.nextToken());
			dist.get(from).add(new Node(to, len));
			dist.get(to).add(new Node(from, len));
		}
		
		minDist = new int[n+1];
		Arrays.fill(minDist, Integer.MAX_VALUE);
		
		//다익스트라
		for(int friendHouse : friend) {
			Dikjkstra(friendHouse);
		}
		
		int maxDist = 0;
		int newHome = 0;
		for(int i = 1; i <= n; i++) {
			if(maxDist < minDist[i]) {
				maxDist = minDist[i];
				newHome = i;
			}
		}
		System.out.println(newHome);
	}

	private static void Dikjkstra(int start) {
		int[] houseDist = new int[n+1];
		Arrays.fill(houseDist, Integer.MAX_VALUE);
		houseDist[start] = 0;
		
		PriorityQueue<Node> pq = new PriorityQueue<>((Node a, Node b) -> a.dist >= b.dist ? 1:-1);
		pq.offer(new Node(start, 0));
		
		while(!pq.isEmpty()) {
			Node curNode = pq.poll();
			if(houseDist[curNode.house] < curNode.dist) {
				continue;
			}
			for(Node nextHouse : dist.get(curNode.house)) {
				if(houseDist[nextHouse.house] > curNode.dist + nextHouse.dist) {
					houseDist[nextHouse.house] = curNode.dist + nextHouse.dist;
					pq.add(new Node(nextHouse.house, houseDist[nextHouse.house]));
				}
				
			}
			
		}
		
		for(int i = 1; i <= n; i++) {
			if(minDist[i] > houseDist[i]) {
				minDist[i] = houseDist[i];
			}
		}
		
	}
	
	private static class Node{
		int house;
		int dist;
		public Node(int house, int dist) {
			super();
			this.house = house;
			this.dist = dist;
		}
		
	}
}

🎈boj 17298- 오큰수


🗨 해결방법 :


스택을 사용

📝메모 :


자신의 오른쪽에 자신보다 작은 수는 존재할 필요가 없다

✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

//17298번
public class 오큰수 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//수열의 크기 n
		int n = Integer.parseInt(br.readLine());
		
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		
		int[] result = new int[n];
		Stack<Integer> stack = new Stack<>();
		for(int i = n-1; i >= 0; i--) {
			while(!stack.isEmpty() && arr[i] >= stack.peek()) {
				stack.pop();
			}
			
			result[i] = stack.isEmpty() ? -1 : stack.peek();
			stack.push(arr[i]);
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < n; i++) {
			sb.append(result[i]).append(" ");
		}
		System.out.println(sb.toString());
		
	}
}

🎈boj 25682- 체스판다시칠하기2


🗨 해결방법 :


누적합

📝메모 :


체스판은 흰색 혹은 검은색으로 시작한다 모든 점에서 새롭게 다시 칠해야 공간을 찾는 것은 비효율적이다

✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//25682
public class 체스판다시칠하기2 {
	private static int[][] bw;
	private static int[][] wb;
	private static int k;

	public static void main(String[] args) throws Exception {
		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());
		k = Integer.parseInt(st.nextToken());
		
		bw = new int[n][m];//bwbwbw로 시작하는 체스판
		wb = new int[n][m];//wbwbwb로 시작하는 체스판
		for(int i = 0; i < n; i++) {
			String str = br.readLine();
			for(int j = 0; j < m; j++) {
				char c = str.charAt(j);
				
				if((i+j)%2==0) {//i+j합이 짝수
					if(c == 'B') {
						wb[i][j] = 1;
					}else {
						bw[i][j] = 1;
					}
				}else {//i+j합이 홀수
					if(c == 'B') {
						bw[i][j] = 1;
					}else {
						wb[i][j] = 1;
					}
				}
				
			}
		}
				
		//k*k 보드, 다시 칠해야 하는 정사각형 개수의 최소값
		//각 줄 가로, 세로 k개 고를 시 다시 칠해야 하는 개수
		//누적합
		//가로 누적합
		for(int i = 0; i < n; i++) {
			for(int j = 1; j < m; j++) {
				bw[i][j] += bw[i][j-1];
				wb[i][j] += wb[i][j-1];
			}
		}
		//세로 누적합
		for(int j = 0; j < m; j++) {
			for(int i = 1; i < n; i++) {
				bw[i][j] += bw[i-1][j];
				wb[i][j] += wb[i-1][j];
			}
		}
		
		int min = Integer.MAX_VALUE;
		for(int i = k-1; i < n; i++) {
			for(int j = k-1; j < m; j++) {
				int sum = repaint(i, j);
				if(min > sum) {
					min = sum;
				}
			}
		}
		System.out.println(min);
		
	}

	private static int repaint(int x, int y) {
		int sumBW = bw[x][y];
		int sumWB = wb[x][y];
		if(x-k>=0) {
			sumBW -= bw[x-k][y];
			sumWB -= wb[x-k][y];
		}
		if(y-k>=0) {
			sumBW -= bw[x][y-k];
			sumWB -= wb[x][y-k];
		}
		if(x-k>=0 && y-k>=0) {
			sumBW += bw[x-k][y-k];
			sumWB += wb[x-k][y-k];
		}
		return Math.min(sumBW, sumWB);
	}
	
}

Copy link
Member

@leetaggg leetaggg left a comment

Choose a reason for hiding this comment

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

잘 알려주셔서 감사합니다! 고생하셨습니다!

@TearofCoding TearofCoding merged commit 9cf45eb into SSAFY-10th-Seoul17:main Nov 2, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants