Skip to content

Commit

Permalink
240117
Browse files Browse the repository at this point in the history
  • Loading branch information
NavyHubb committed Jan 17, 2024
1 parent 15673a8 commit ffd5ed1
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/graph/tree/BOJ_5639_이진검색트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package graph.tree;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* [문제 분석]
* 이진 검색 트리를 전위 순회한 결과가 주어졌을 때, 이 트리를 후위 순회한 결과를 구하라
*
* [문제 풀이]
*
*/
public class BOJ_5639_이진검색트리 {

static class Node {
int num;
Node left, right;

public Node(int num) {
this.num = num;
}

public Node(int num, Node left, Node right) {
this.num = num;
this.left = left;
this.right = right;
}

public void insert(int n) {
if (n < this.num) {
if (this.left == null) {
this.left = new Node(n);
} else {
this.left.insert(n);
}
} else {
if (this.right == null) {
this.right = new Node(n);
} else {
this.right.insert(n);
}
}
}
}

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

// 루트 노드 설정
Node root = new Node(Integer.parseInt(br.readLine()));

// 트리 구성
String input;
while (true) {
input = br.readLine();
if (input == null || input.equals("")) break;

root.insert(Integer.parseInt(input));
}

postOrder(root);
}

// 후위순회: 왼쪽자식-오른쪽자식-부모
private static void postOrder(Node node) {
if (node == null) return;

postOrder(node.left);
postOrder(node.right);
System.out.println(node.num);
}

}
130 changes: 130 additions & 0 deletions src/implementation/BOJ_17779_게리맨더링2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package implementation;

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

/**
* [문제 분석]
* (x, y), (x+1, y-1), ..., (x+d1, y-d1)
* (x, y), (x+1, y+1), ..., (x+d2, y+d2)
* (x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)
* (x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)
* 경계선과 경계선의 안에 포함되어있는 곳은 5번 선거구이다.
*
* 1번 선거구: 1 ≤ r < x+d1, 1 ≤ c ≤ y
* 2번 선거구: 1 ≤ r ≤ x+d2, y < c ≤ N
* 3번 선거구: x+d1 ≤ r ≤ N, 1 ≤ c < y-d1+d2
* 4번 선거구: x+d2 < r ≤ N, y-d1+d2 ≤ c ≤ N
*
* 조건)
* d1, d2 ≥ 1
* 1 ≤ x < x+d1+d2 ≤ N
* 1 ≤ y-d1 < y < y+d2 ≤ N
*
* [문제 풀이]
* x, y, d1, d2에 대해 모든 경우의 수를 탐색하여
* 최솟값을 찾는다
*/
public class BOJ_17779_게리맨더링2 {

static int N, total;
static int[][] map;

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

N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
total += map[i][j];
}
}

System.out.println(solution());
}

private static int solution() {
int min = Integer.MAX_VALUE;

for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++) {
for (int d1 = 1; d1 < N; d1++) {
for (int d2 = 1; d2 < N; d2++) {
if (x + d1 + d2 >= N) continue;
if (y - d1 < 0 || y + d2 >= N) continue;

min = Math.min(min, simulation(x, y, d1, d2));
}
}
}
}

return min;
}

private static int simulation(int x, int y, int d1, int d2) {
boolean[][] border = new boolean[N][N];

// 경계선 마킹
for (int i = 0; i <= d1; i++) {
border[x+i][y-i] = true;
border[x+d2+i][y+d2-i] = true;
}
for (int i = 0; i <= d2; i++) {
border[x+i][y+i] = true;
border[x+d1+i][y-d1+i] = true;
}

int[] people = new int[5]; // 구역별 인구수

// 1 선거구
for (int r = 0; r < x+d1; r++) {
for (int c = 0; c <= y; c++) {
if (border[r][c]) break;
people[0] += map[r][c];
}
}

// 2 선거구
for (int r = 0; r <= x+d2; r++) {
for (int c = N-1; c > y; c--) {
if (border[r][c]) break;
people[1] += map[r][c];
}
}

// 3 선거구
for (int r = x+d1; r < N; r++) {
for (int c = 0; c < y-d1+d2; c++) {
if (border[r][c]) break;
people[2] += map[r][c];
}
}

// 4 선거구
for (int r = x+d2+1; r < N; r++) {
for (int c = N-1; c >= y-d1+d2; c--) {
if (border[r][c]) break;
people[3] += map[r][c];
}
}

// 5 선거구
people[4] = total;
for (int i = 0; i <= 3; i++) {
people[4] -= people[i];
}

Arrays.sort(people);

return people[4] - people[0];
}

}
52 changes: 52 additions & 0 deletions src/twoPointer/BOJ_2470_두용액.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package twoPointer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class BOJ_2470_두용액 {

static int N;
static int[] nums;

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

N = Integer.parseInt(br.readLine());
nums = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();

solution();
}

private static void solution() {
Arrays.sort(nums);

int leftIdx = 0;
int rightIdx = N-1;
int leftValue = nums[leftIdx];
int rightValue = nums[rightIdx];
int gap = Integer.MAX_VALUE;

int sum;
int abs;
while (leftIdx < rightIdx) {
sum = nums[leftIdx] + nums[rightIdx];
abs = Math.abs(sum);
if (abs < gap) {
gap = abs;
leftValue = nums[leftIdx];
rightValue = nums[rightIdx];
}

if (sum > 0) {
rightIdx--;
} else {
leftIdx++;
}
}

System.out.println(leftValue + " " + rightValue);
}

}

0 comments on commit ffd5ed1

Please sign in to comment.