Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/programmers/algorithm/bruteforce/_01_최소직사각형.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package programmers.algorithm.bruteforce;

import java.util.stream.IntStream;

public class _01_최소직사각형 {

public static void main(String[] args) {
new _01_최소직사각형();
}

public _01_최소직사각형() {
int[][] val1 = new int[][]{{60, 50}, {30, 70}, {60, 30}, {80, 40}};
int[][] val2 = new int[][]{{10, 7}, {12, 3}, {8, 15}, {14, 7}, {5, 15}};
int[][] val3 = new int[][]{{14, 4}, {19, 6}, {6, 16}, {18, 7}, {7, 11}};
int[][][] val = new int[][][]{val1, val2, val3};
IntStream.range(0, val.length).forEach(i -> System.out.println(solution(val[i])));
}

public int solution(int[][] sizes) {
int height = 0, width = 0;
for (int[] size : sizes) {
height = Math.max(Math.max(size[0], size[1]), height);
width = Math.max(Math.min(size[0], size[1]), width);
}
return height * width;
}
}
58 changes: 58 additions & 0 deletions src/programmers/algorithm/bruteforce/_02_모의고사.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package programmers.algorithm.bruteforce;

import java.util.ArrayList;
import java.util.List;

public class _02_모의고사 {

public static void main(String[] args) {
int[] solution = new _02_모의고사().solution(new int[]{1, 3, 2, 4, 2});
for (int i : solution) {
System.out.println(i);
}
}

private static final int[] FIRST_PATTERN = new int[]{1, 2, 3, 4, 5};
private static final int[] SECOND_PATTERN = new int[]{2, 1, 2, 3, 2, 4, 2, 5};
private static final int[] THIRD_PATTERN = new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

public int[] solution(int[] answers) {
int firstManScore = scoring(FIRST_PATTERN, answers);
int secondManScore = scoring(SECOND_PATTERN, answers);
int thirdManScore = scoring(THIRD_PATTERN, answers);

return createWinnerArr(firstManScore, secondManScore, thirdManScore);
}

private int scoring(int[] pattern, int[] answers) {
int score = 0, index = 0;
int length = pattern.length;

for (int answer : answers) {
if (answer == pattern[index++ % length]) {
score++;
}
}

return score;
}

private int[] createWinnerArr(int fst, int snd, int trd) {
List<Integer> winnerList = new ArrayList<>();

int max = Math.max(fst, Math.max(snd, trd));
if (max == fst) {
winnerList.add(1);
}

if (max == snd) {
winnerList.add(2);
}

if (max == trd) {
winnerList.add(3);
}

return winnerList.stream().mapToInt(i -> i).toArray();
}
}
79 changes: 79 additions & 0 deletions src/programmers/algorithm/bruteforce/_03_소수찾기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package programmers.algorithm.bruteforce;

import java.util.HashSet;
import java.util.Set;

public class _03_소수찾기 {

public static void main(String[] args) {
int solution = new _03_소수찾기().solution("17");
System.out.println(solution);
}

private Set<Integer> numSet = new HashSet<>();

private boolean[] visited;

private int nums[];

private int length;

boolean[] notPrimeNumbers;

public int solution(String numbers) {
length = numbers.length();
visited = new boolean[length];
nums = numbers.chars().map(i -> Character.getNumericValue(i)).sorted().toArray();


createNum(0, 0);

Integer max = numSet.stream().max(Comparable::compareTo).get();
notPrimeNumbers = getNotPrimeNumbers(max);

int answer = 0;
for (Integer integer : numSet) {
if (!notPrimeNumbers[integer]) {
answer++;
}
}

return answer;
}

private void createNum(int level, int num) {
if (level != 0) {
numSet.add(num);
}

if (level == length) {
return;
}

for (int i = 0; i < length; i++) {
int curr = nums[i];
if (visited[i]) {
continue;
}
visited[i] = true;
createNum(++level, num * 10 + curr);
visited[i] = false;
level--;
}
}

private boolean[] getNotPrimeNumbers(int n) {
boolean[] notPrimeNumbers = new boolean[n + 1];
notPrimeNumbers[0] = notPrimeNumbers[1] = true;

for (int i = 2; i * i <= n; i++) {
if (!notPrimeNumbers[i]) {
for (int j = i * i; j <= n; j += i) {
notPrimeNumbers[j] = true;
}
}
}

return notPrimeNumbers;
}
}
27 changes: 27 additions & 0 deletions src/programmers/algorithm/bruteforce/_04_카펫.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package programmers.algorithm.bruteforce;

public class _04_카펫 {

public static void main(String[] args) {
int[] solution = new _04_카펫().solution(10, 2);
for (int i : solution) {
System.out.println(i);
}
}

public int[] solution(int brown, int yellow) {
int[] answer = new int[2];
int plus = brown / 2 + 2;
int multiply = brown + yellow;
for (int height = 2; height <= plus / 2; height++) {
int width = plus - height;
if (width * height == multiply) {
answer[0] = width;
answer[1] = height;
break;
}
}

return answer;
}
}
61 changes: 61 additions & 0 deletions src/programmers/algorithm/bruteforce/_05_피로도.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package programmers.algorithm.bruteforce;

import java.util.ArrayList;
import java.util.List;

public class _05_피로도 {

public static void main(String[] args) {
// int solution = new _05_피로도().solution(80, new int[][]{{80, 20}, {50, 40}, {30, 10}});
// int solution = new _05_피로도().solution(10, new int[][]{{9, 2}, {10, 3}, {7, 3}, {5, 4}, {1, 1}});
// System.out.println(solution);
new _05_피로도().scoring();
}

public void scoring() {
List<int[][]> testList = new ArrayList<>();
testList.add(new int[][]{{80, 20}, {50, 40}, {30, 10}});
testList.add(new int[][]{{7, 3}, {5, 4}, {1, 1}});
testList.add(new int[][]{{9, 2}, {10, 3}, {7, 3}, {5, 4}, {1, 1}});
int ks[] = new int[]{80, 8, 10};
for (int i = 0; i < 3; i++) {
int solution = solution(ks[i], testList.get(i));
System.out.println(solution);
answer = 0;
}
}

private boolean[] visited;

private int answer;

public int solution(int k, int[][] dungeons) {
visited = new boolean[dungeons.length];
goDungeon(dungeons, 0, k);
return answer;
}

private void goDungeon(int[][] dungeons, int count, int k) {
int length = dungeons.length;
if (answer == length) {
return;
}
if (count == length) {
answer = count;
return;
}
for (int i = 0; i < length; i++) {
if (visited[i]) {
continue;
}
int[] dungeon = dungeons[i];
if (dungeon[0] > k) {
continue;
}
visited[i] = true;
goDungeon(dungeons, count + 1, k - dungeon[1]);
answer = Math.max(count + 1, answer);
visited[i] = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package programmers.algorithm.bruteforce;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _06_전력망을둘로나누기 {

public static void main(String[] args) {
new _06_전력망을둘로나누기().scoring();
}

public void scoring() {
int[] ns = new int[]{9, 4, 7};
List<int[][]> wiresList = new ArrayList<>();
wiresList.add(new int[][]{{1, 3}, {2, 3}, {3, 4}, {4, 5}, {4, 6}, {4, 7}, {7, 8}, {7, 9}});
wiresList.add(new int[][]{{1, 2}, {2, 3}, {3, 4}});
wiresList.add(new int[][]{{1, 2}, {2, 7}, {3, 7}, {3, 4}, {4, 5}, {6, 7}});
for (int i = 0; i < 3; i++) {
System.out.println(solution(ns[i], wiresList.get(i)));
}
}

public int solution(int n, int[][] wires) {
int answer = n;

List<Integer>[] tree = createTree(n, wires);
for (int[] wire : wires) {
answer = Math.min(answer, cutAndCalculateNode(wire, tree));
}

return answer;
}

private int cutAndCalculateNode(int[] wire, List<Integer>[] tree) {
int i = wire[0];
int j = wire[1];
int x = bfs(i, j, tree);
int y = bfs(j, i, tree);

return Math.abs(x - y);
}

private int bfs(int start, int cut, List<Integer>[] tree) {
int answer = 0;

boolean[] visited = new boolean[tree.length];
visited[start] = true;
visited[cut] = true;

Queue<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int poll = queue.poll();
answer++;
List<Integer> nodes = tree[poll];
for (int node : nodes) {
if (visited[node]) {
continue;
}
queue.add(node);
visited[node] = true;
}
}

return answer;
}

private List<Integer>[] createTree(int n, int[][] wires) {
List<Integer>[] tree = new List[n + 1];
for (int i = 1; i < n + 1; i++) {
tree[i] = new ArrayList<>();
}

for (int[] wire : wires) {
int i = wire[0];
int j = wire[1];
tree[i].add(j);
tree[j].add(i);
}

return tree;
}
}
46 changes: 46 additions & 0 deletions src/programmers/algorithm/bruteforce/_07_모음사전.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package programmers.algorithm.bruteforce;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class _07_모음사전 {

public static void main(String[] args) {
new _07_모음사전().scoring();
}

public void scoring() {
String[] words = new String[]{"AAAAE", "AAAE", "I", "EIO"};
for (int i = 0; i < 4; i++) {
System.out.println(solution(words[i]));
}
}

public int solution(String word) {
List<String> dictionary = createDictionary();

return dictionary.indexOf(word);
}

private List<String> createDictionary() {
List<String> dictionary = new ArrayList<>();
addDictionary(dictionary, new StringBuilder());
Collections.sort(dictionary);

return dictionary;
}

private void addDictionary(List<String> dictionary, StringBuilder word) {
dictionary.add(word.toString());
if (word.length() == 5) {
return;
}
for (int i = 0; i < 5; i++) {
char c = "AEIOU".charAt(i);
word.append(c);
addDictionary(dictionary, word);
word.deleteCharAt(word.length() - 1);
}
}
}