봄 황사와 함께 미세먼지가 기승을 부리는 1주일이었다. 코 아프고, 목 아프고, 손도 끈적거리고,,, 🤧 가만히 있어도 수명이 깎여나가는 1주일...💀 24년 3월의 마지막 주를 회고한다.
// 1. descendingSet() 옵션 사용
for (Integer i : deck.descendingSet()) {
if(idx++ + 1 == targetIdx) return i;
}
// 2. 생성시 내림차순 옵션 추가
new TreeSet<>(Collections.reverseOrder());
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Input ip = getInput(br);
Solution s = new Solution();
System.out.println(s.solution(ip.arr1, ip.arr2));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Input getInput(BufferedReader br) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int len1 = Integer.parseInt(st.nextToken());
int len2 = Integer.parseInt(st.nextToken());
int[] arr1 = getArray(br, len1);
int[] arr2 = getArray(br, len2);
return new Input(arr1, arr2);
}
private static int[] getArray(BufferedReader br, int len) throws IOException {
int[] arr = new int[len];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
return arr;
}
static class Input{
int[] arr1;
int[] arr2;
public Input(int[] arr1, int[] arr2) {
this.arr1 = arr1;
this.arr2 = arr2;
}
}
}
class Solution {
public String solution(int[] arr1, int[] arr2) {
return getAnswer(getDiffer(arr1, getSet(arr2)));
}
private String getAnswer(List<Integer> list) {
StringBuilder answer = new StringBuilder();
if(list.isEmpty()) return "0";
answer.append(list.size()).append("\n");
for (int i : list) {
answer.append(i).append(" ");
}
return answer.toString();
}
private List<Integer> getDiffer(int[] arr1, Set<Integer> set) {
List<Integer> result = new ArrayList<>();
for (int i : arr1) {
if(!set.contains(i)) result.add(i);
}
Collections.sort(result);
return result;
}
private Set<Integer> getSet(int[] arr) {
Set<Integer> set = new HashSet<>();
for (int i : arr) {
set.add(i);
}
return set;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int[] getInput(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[] arr = new int[len];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
return arr;
}
}
class Solution {
public String solution(int[] arr) {
return getAnswer(arr);
}
private String getAnswer(int[] arr) {
StringBuilder answer = new StringBuilder();
for (int i : arr) {
Sieve sieve = new Sieve(i);
ThreePrimeProblemSolver tpps = new ThreePrimeProblemSolver(i, sieve.getPrimeNums());
int[] result = tpps.solveThreePrimeProblem();
if (isSolved(result)) {
for (int primeNum : result) {
answer.append(primeNum).append(" ");
}
answer.append("\n");
}
}
return answer.toString();
}
private boolean isSolved(int[] result) {
int sum = 0;
for (int i : result) {
sum += i;
}
return sum != 0;
}
}
class ThreePrimeProblemSolver {
int target;
List<Integer> sievedNums;
public ThreePrimeProblemSolver(int target, List<Integer> sievedNums) {
this.target = target;
this.sievedNums = sievedNums;
}
public int[] solveThreePrimeProblem() {
for (Integer sievedNum1 : this.sievedNums) {
for (Integer sievedNum2 : this.sievedNums) {
for (Integer sievedNum3 : this.sievedNums) {
if(sievedNum1 + sievedNum2 + sievedNum3 == this.target)
return new int[]{sievedNum1, sievedNum2, sievedNum3};
}
}
}
return new int[]{0, 0, 0};
}
}
class Sieve {
boolean[] sieve;
List<Integer> primeNums;
public Sieve(int limit) {
this.sieve = new boolean[limit + 1];
Arrays.fill(this.sieve, true);
this.primeNums = getSievedNums();
}
private List<Integer> getSievedNums() {
List<Integer> sievedNums = new ArrayList<>();
for (int i = 2; i < this.sieve.length; i++) {
if (this.sieve[i]) {
sievedNums.add(i);
sieveNums(i);
}
}
return sievedNums;
}
private void sieveNums(int num) {
for (int i = num * 2; i < this.sieve.length; i += num) {
this.sieve[i] = false;
}
}
public List<Integer> getPrimeNums() {
return this.primeNums;
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int len = Integer.parseInt(br.readLine());
Solution s = new Solution();
System.out.println(s.solution(getInput(br, len), getInput(br, len)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String[][] getInput(BufferedReader br, int len) throws IOException {
String[][] board = new String[len][len];
for (int i = 0; i < board.length; i++) {
board[i] = br.readLine().split("");
}
return board;
}
}
class Solution {
public String solution(String[][] board, String[][] input) {
MineSearchPlayer msp = new MineSearchPlayer(board, input);
return getAnswer(msp.getResult());
}
private String getAnswer(String[][] result) {
StringBuilder answer = new StringBuilder();
for (String[] row : result) {
for (String i : row) {
answer.append(i);
}
answer.append("\n");
}
return answer.toString();
}
private class MineSearchPlayer {
String[][] mineField;
String[][] inputs;
boolean[][] isVisited;
int[][] result;
String[][] processedField;
boolean isAlreadyFail = false;
int[][] directions = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
public MineSearchPlayer(String[][] mineField, String[][] inputs) {
this.mineField = mineField;
this.inputs = inputs;
this.isVisited = new boolean[mineField.length][mineField[0].length];
this.result = new int[mineField.length][mineField[0].length];
this.processedField = new String[mineField.length][mineField[0].length];
for (String[] strings : this.processedField) {
Arrays.fill(strings, ".");
}
}
public String[][] getResult() {
playMineSearch();
return afterPlay();
}
private String[][] afterPlay() {
for (int x = 0; x < this.result.length; x++) {
for (int y = 0; y < this.result[x].length; y++) {
if (this.isVisited[x][y])this.processedField[x][y] = String.valueOf(this.result[x][y]);
}
}
if(this.isAlreadyFail) failProcess();
return this.processedField;
}
private void playMineSearch() {
for (int x = 0; x < this.inputs.length; x++) {
for (int y = 0; y < this.inputs[x].length; y++) {
if("x".equals(this.inputs[x][y])){
search(x, y);
}
}
}
}
private void search(int x, int y) {
this.isVisited[x][y] = true;
if (isMineLocated(x, y) && !this.isAlreadyFail) {
this.isAlreadyFail = true;
}
for (int[] direction : this.directions) {
int nx = x + direction[0];
int ny = y + direction[1];
if (isWithinMineField(nx, ny) && isMineLocated(nx, ny)) this.result[x][y]++;
}
}
private boolean isWithinMineField(int x, int y) {
return 0 <= x && x < this.inputs.length && 0 <= y && y < this.inputs[x].length;
}
private boolean isMineLocated(int x, int y) {
return "*".equals(this.mineField[x][y]);
}
private void failProcess() {
for (int x = 0; x < this.processedField.length; x++) {
for (int y = 0; y < this.processedField[x].length; y++) {
if("*".equals(this.mineField[x][y])) this.processedField[x][y] = "*";
}
}
}
}
}