Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ndb796 committed Aug 9, 2020
1 parent 9c9e1c1 commit 5acfc12
Show file tree
Hide file tree
Showing 16 changed files with 645 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 11/1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.*;

public class Main {

public static int n;
public static ArrayList<Integer> arrayList = new ArrayList<>();

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();

for (int i = 0; i < n; i++) {
arrayList.add(sc.nextInt());
}

Collections.sort(arrayList);

int result = 0; // 총 그룹의 수
int count = 0; // 현재 그룹에 포함된 모험가의 수

for (int i = 0; i < n; i++) { // 공포도를 낮은 것부터 하나씩 확인하며
count += 1; // 현재 그룹에 해당 모험가를 포함시키기
if (count >= arrayList.get(i)) { // 현재 그룹에 포함된 모험가의 수가 현재의 공포도 이상이라면, 그룹 결성
result += 1; // 총 그룹의 수 증가시키기
count = 0; // 현재 그룹에 포함된 모험가의 수 초기화
}
}

System.out.println(result);
}
}
25 changes: 25 additions & 0 deletions 11/2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();

// 첫 번째 문자를 숫자로 변경한 값을 대입
long result = str.charAt(0) - '0';

for (int i = 1; i < str.length(); i++) {
// 두 수 중에서 하나라도 '0' 혹은 '1'인 경우, 곱하기보다는 더하기 수행
int num = str.charAt(i) - '0';
if (num <= 1 || result <= 1) {
result += num;
}
else {
result *= num;
}
}

System.out.println(result);
}
}
33 changes: 33 additions & 0 deletions 11/3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.*;

public class Main {

public static String str;
public static int count0 = 0; // 전부 0으로 바꾸는 경우
public static int count1 = 0; // 전부 1로 바꾸는 경우

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
str = sc.next();

// 첫 번째 원소에 대해서 처리
if (str.charAt(0) == '1') {
count0 += 1;
}
else {
count1 += 1;
}

// 두 번째 원소부터 모든 원소를 확인하며
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) != str.charAt(i + 1)) {
// 다음 수에서 1로 바뀌는 경우
if (str.charAt(i + 1) == '1') count0 += 1;
// 다음 수에서 0으로 바뀌는 경우
else count1 += 1;
}
}

System.out.println(Math.min(count0, count1));
}
}
27 changes: 27 additions & 0 deletions 11/4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;

public class Main {

public static int n;
public static ArrayList<Integer> arrayList = new ArrayList<>();

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();

for (int i = 0; i < n; i++) {
arrayList.add(sc.nextInt());
}

Collections.sort(arrayList);

int target = 1;
for (int i = 0; i < n; i++) {
// 만들 수 없는 금액을 찾았을 때 반복 종료
if (target < arrayList.get(i)) break;
target += arrayList.get(i);
}

System.out.println(target);
}
}
29 changes: 29 additions & 0 deletions 11/5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;

public class Main {

public static int n, m;
// 1부터 10까지의 무게를 담을 수 있는 배열
public static int[] arr = new int[11];

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();

for (int i = 0; i < n; i++) {
int x = sc.nextInt();
arr[x] += 1;
}

int result = 0;

// 1부터 m까지의 각 무게에 대하여 처리
for (int i = 1; i <= m; i++) {
n -= arr[i]; // 무게가 i인 볼링공의 개수(A가 선택할 수 있는 개수) 제외
result += arr[i] * n; // B가 선택하는 경우의 수와 곱해주기
}

System.out.println(result);
}
}
70 changes: 70 additions & 0 deletions 11/6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.*;

class Food implements Comparable<Food> {

private int time;
private int index;

public Food(int time, int index) {
this.time = time;
this.index = index;
}

public int getTime() {
return this.time;
}

public int getIndex() {
return this.index;
}

// 시간이 짧은 것이 높은 우선순위를 가지도록 설정
@Override
public int compareTo(Food other) {
return Integer.compare(this.time, other.time);
}
}

class Solution {
public int solution(int[] food_times, long k) {
// 전체 음식을 먹는 시간보다 k가 크거나 같다면 -1
long summary = 0;
for (int i = 0; i < food_times.length; i++) {
summary += food_times[i];
}
if (summary <= k) return -1;

// 시간이 작은 음식부터 빼야 하므로 우선순위 큐를 이용
PriorityQueue<Food> pq = new PriorityQueue<>();
for (int i = 0; i < food_times.length; i++) {
// (음식 시간, 음식 번호) 형태로 우선순위 큐에 삽입
pq.offer(new Food(food_times[i], i + 1));
}

summary = 0; // 먹기 위해 사용한 시간
long previous = 0; // 직전에 다 먹은 음식 시간
long length = food_times.length; // 남은 음식의 개수

// summary + (현재의 음식 시간 - 이전 음식 시간) * 현재 음식 개수와 k 비교
while (summary + ((pq.peek().getTime() - previous) * length) <= k) {
int now = pq.poll().getTime();
summary += (now - previous) * length;
length -= 1; // 다 먹은 음식 제외
previous = now; // 이전 음식 시간 재설정
}

// 남은 음식 중에서 몇 번째 음식인지 확인하여 출력
ArrayList<Food> result = new ArrayList<>();
while (!pq.isEmpty()) {
result.add(pq.poll());
}
// 음식의 번호 기준으로 정렬
Collections.sort(result, new Comparator<Food>() {
@Override
public int compare(Food a, Food b) {
return Integer.compare(a.getIndex(), b.getIndex());
}
});
return result.get((int) ((k - summary) % length)).getIndex();
}
}
67 changes: 67 additions & 0 deletions 14/1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.*;

class Student implements Comparable<Student> {

private String name;
private int kor;
private int eng;
private int m;

public Student(String name, int kor, int eng, int m) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.m = m;
}

/*
[ 정렬 기준 ]
1) 두 번째 원소를 기준으로 내림차순 정렬
2) 두 번째 원소가 같은 경우, 세 번째 원소를 기준으로 오름차순 정렬
3) 세 번째 원소가 같은 경우, 네 번째 원소를 기준으로 내림차순 정렬
4) 네 번째 원소가 같은 경우, 첫 번째 원소를 기준으로 오름차순 정렬
*/

public String getName() {
return this.name;
}

// 정렬 기준은 '점수가 낮은 순서'
@Override
public int compareTo(Student other) {
if (this.kor == other.kor && this.eng == other.eng && this.m == other.m) {
return this.name.compareTo(other.name);
}
if (this.kor == other.kor && this.eng == other.eng) {
return Integer.compare(other.m, this.m);
}
if (this.kor == other.kor) {
return Integer.compare(this.eng, other.eng);
}
return Integer.compare(other.kor, this.kor);
}
}

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

ArrayList<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name = sc.next();
int kor = sc.nextInt();
int eng = sc.nextInt();
int m = sc.nextInt();
students.add(new Student(name, kor, eng, m));
}

Collections.sort(students);

// 정렬된 학생 정보에서 이름만 출력
for (int i = 0; i < n; i++) {
System.out.println(students.get(i).getName());
}
}
}
19 changes: 19 additions & 0 deletions 14/2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

ArrayList<Integer> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++) {
arrayList.add(sc.nextInt());
}

Collections.sort(students);

// 중간값(median)을 출력
System.out.println(v[(n - 1) / 2]);
}
}
62 changes: 62 additions & 0 deletions 14/3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.*;

class Node implements Comparable<Node> {

private int stage;
private double fail;

public Node(int stage, double fail) {
this.stage = stage;
this.fail = fail;
}

public int getStage() {
return this.stage;
}

@Override
public int compareTo(Node other) {
if (this.fail == other.fail) {
return Integer.compare(this.stage, other.stage);
}
return Double.compare(other.fail, this.fail);
}
}

class Solution {
public int[] solution(int N, int[] stages) {
int[] answer = new int[N];
ArrayList<Node> arrayList = new ArrayList<>();
int length = stages.length;

// 스테이지 번호를 1부터 N까지 증가시키며
for (int i = 1; i <= N; i++) {
// 해당 스테이지에 머물러 있는 사람의 수 계산
int cnt = 0;
for (int j = 0; j < stages.length; j++) {
if (stages[j] == i) {
cnt += 1;
}
}

// 실패율 계산
double fail = 0;
if (length >= 1) {
fail = (double) cnt / length;
}

// 리스트에 (스테이지 번호, 실패율) 원소 삽입
arrayList.add(new Node(i, fail));
length -= cnt;
}

// 실패율을 기준으로 각 스테이지를 내림차순 정렬
Collections.sort(arrayList);

// 정렬된 스테이지 번호 반환
for (int i = 0; i < N; i++) {
answer[i] = arrayList.get(i).getStage();
}
return answer;
}
}
Loading

0 comments on commit 5acfc12

Please sign in to comment.