Skip to content

Commit 5acfc12

Browse files
committed
Update
1 parent 9c9e1c1 commit 5acfc12

File tree

16 files changed

+645
-0
lines changed

16 files changed

+645
-0
lines changed

11/1.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n;
6+
public static ArrayList<Integer> arrayList = new ArrayList<>();
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
n = sc.nextInt();
11+
12+
for (int i = 0; i < n; i++) {
13+
arrayList.add(sc.nextInt());
14+
}
15+
16+
Collections.sort(arrayList);
17+
18+
int result = 0; // 총 그룹의 수
19+
int count = 0; // 현재 그룹에 포함된 모험가의 수
20+
21+
for (int i = 0; i < n; i++) { // 공포도를 낮은 것부터 하나씩 확인하며
22+
count += 1; // 현재 그룹에 해당 모험가를 포함시키기
23+
if (count >= arrayList.get(i)) { // 현재 그룹에 포함된 모험가의 수가 현재의 공포도 이상이라면, 그룹 결성
24+
result += 1; // 총 그룹의 수 증가시키기
25+
count = 0; // 현재 그룹에 포함된 모험가의 수 초기화
26+
}
27+
}
28+
29+
System.out.println(result);
30+
}
31+
}

11/2.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
String str = sc.next();
8+
9+
// 첫 번째 문자를 숫자로 변경한 값을 대입
10+
long result = str.charAt(0) - '0';
11+
12+
for (int i = 1; i < str.length(); i++) {
13+
// 두 수 중에서 하나라도 '0' 혹은 '1'인 경우, 곱하기보다는 더하기 수행
14+
int num = str.charAt(i) - '0';
15+
if (num <= 1 || result <= 1) {
16+
result += num;
17+
}
18+
else {
19+
result *= num;
20+
}
21+
}
22+
23+
System.out.println(result);
24+
}
25+
}

11/3.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static String str;
6+
public static int count0 = 0; // 전부 0으로 바꾸는 경우
7+
public static int count1 = 0; // 전부 1로 바꾸는 경우
8+
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
str = sc.next();
12+
13+
// 첫 번째 원소에 대해서 처리
14+
if (str.charAt(0) == '1') {
15+
count0 += 1;
16+
}
17+
else {
18+
count1 += 1;
19+
}
20+
21+
// 두 번째 원소부터 모든 원소를 확인하며
22+
for (int i = 0; i < str.length() - 1; i++) {
23+
if (str.charAt(i) != str.charAt(i + 1)) {
24+
// 다음 수에서 1로 바뀌는 경우
25+
if (str.charAt(i + 1) == '1') count0 += 1;
26+
// 다음 수에서 0으로 바뀌는 경우
27+
else count1 += 1;
28+
}
29+
}
30+
31+
System.out.println(Math.min(count0, count1));
32+
}
33+
}

11/4.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n;
6+
public static ArrayList<Integer> arrayList = new ArrayList<>();
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
n = sc.nextInt();
11+
12+
for (int i = 0; i < n; i++) {
13+
arrayList.add(sc.nextInt());
14+
}
15+
16+
Collections.sort(arrayList);
17+
18+
int target = 1;
19+
for (int i = 0; i < n; i++) {
20+
// 만들 수 없는 금액을 찾았을 때 반복 종료
21+
if (target < arrayList.get(i)) break;
22+
target += arrayList.get(i);
23+
}
24+
25+
System.out.println(target);
26+
}
27+
}

11/5.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n, m;
6+
// 1부터 10까지의 무게를 담을 수 있는 배열
7+
public static int[] arr = new int[11];
8+
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
n = sc.nextInt();
12+
m = sc.nextInt();
13+
14+
for (int i = 0; i < n; i++) {
15+
int x = sc.nextInt();
16+
arr[x] += 1;
17+
}
18+
19+
int result = 0;
20+
21+
// 1부터 m까지의 각 무게에 대하여 처리
22+
for (int i = 1; i <= m; i++) {
23+
n -= arr[i]; // 무게가 i인 볼링공의 개수(A가 선택할 수 있는 개수) 제외
24+
result += arr[i] * n; // B가 선택하는 경우의 수와 곱해주기
25+
}
26+
27+
System.out.println(result);
28+
}
29+
}

11/6.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
class Food implements Comparable<Food> {
4+
5+
private int time;
6+
private int index;
7+
8+
public Food(int time, int index) {
9+
this.time = time;
10+
this.index = index;
11+
}
12+
13+
public int getTime() {
14+
return this.time;
15+
}
16+
17+
public int getIndex() {
18+
return this.index;
19+
}
20+
21+
// 시간이 짧은 것이 높은 우선순위를 가지도록 설정
22+
@Override
23+
public int compareTo(Food other) {
24+
return Integer.compare(this.time, other.time);
25+
}
26+
}
27+
28+
class Solution {
29+
public int solution(int[] food_times, long k) {
30+
// 전체 음식을 먹는 시간보다 k가 크거나 같다면 -1
31+
long summary = 0;
32+
for (int i = 0; i < food_times.length; i++) {
33+
summary += food_times[i];
34+
}
35+
if (summary <= k) return -1;
36+
37+
// 시간이 작은 음식부터 빼야 하므로 우선순위 큐를 이용
38+
PriorityQueue<Food> pq = new PriorityQueue<>();
39+
for (int i = 0; i < food_times.length; i++) {
40+
// (음식 시간, 음식 번호) 형태로 우선순위 큐에 삽입
41+
pq.offer(new Food(food_times[i], i + 1));
42+
}
43+
44+
summary = 0; // 먹기 위해 사용한 시간
45+
long previous = 0; // 직전에 다 먹은 음식 시간
46+
long length = food_times.length; // 남은 음식의 개수
47+
48+
// summary + (현재의 음식 시간 - 이전 음식 시간) * 현재 음식 개수와 k 비교
49+
while (summary + ((pq.peek().getTime() - previous) * length) <= k) {
50+
int now = pq.poll().getTime();
51+
summary += (now - previous) * length;
52+
length -= 1; // 다 먹은 음식 제외
53+
previous = now; // 이전 음식 시간 재설정
54+
}
55+
56+
// 남은 음식 중에서 몇 번째 음식인지 확인하여 출력
57+
ArrayList<Food> result = new ArrayList<>();
58+
while (!pq.isEmpty()) {
59+
result.add(pq.poll());
60+
}
61+
// 음식의 번호 기준으로 정렬
62+
Collections.sort(result, new Comparator<Food>() {
63+
@Override
64+
public int compare(Food a, Food b) {
65+
return Integer.compare(a.getIndex(), b.getIndex());
66+
}
67+
});
68+
return result.get((int) ((k - summary) % length)).getIndex();
69+
}
70+
}

14/1.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.*;
2+
3+
class Student implements Comparable<Student> {
4+
5+
private String name;
6+
private int kor;
7+
private int eng;
8+
private int m;
9+
10+
public Student(String name, int kor, int eng, int m) {
11+
this.name = name;
12+
this.kor = kor;
13+
this.eng = eng;
14+
this.m = m;
15+
}
16+
17+
/*
18+
[ 정렬 기준 ]
19+
1) 두 번째 원소를 기준으로 내림차순 정렬
20+
2) 두 번째 원소가 같은 경우, 세 번째 원소를 기준으로 오름차순 정렬
21+
3) 세 번째 원소가 같은 경우, 네 번째 원소를 기준으로 내림차순 정렬
22+
4) 네 번째 원소가 같은 경우, 첫 번째 원소를 기준으로 오름차순 정렬
23+
*/
24+
25+
public String getName() {
26+
return this.name;
27+
}
28+
29+
// 정렬 기준은 '점수가 낮은 순서'
30+
@Override
31+
public int compareTo(Student other) {
32+
if (this.kor == other.kor && this.eng == other.eng && this.m == other.m) {
33+
return this.name.compareTo(other.name);
34+
}
35+
if (this.kor == other.kor && this.eng == other.eng) {
36+
return Integer.compare(other.m, this.m);
37+
}
38+
if (this.kor == other.kor) {
39+
return Integer.compare(this.eng, other.eng);
40+
}
41+
return Integer.compare(other.kor, this.kor);
42+
}
43+
}
44+
45+
public class Main {
46+
47+
public static void main(String[] args) {
48+
Scanner sc = new Scanner(System.in);
49+
int n = sc.nextInt();
50+
51+
ArrayList<Student> students = new ArrayList<>();
52+
for (int i = 0; i < n; i++) {
53+
String name = sc.next();
54+
int kor = sc.nextInt();
55+
int eng = sc.nextInt();
56+
int m = sc.nextInt();
57+
students.add(new Student(name, kor, eng, m));
58+
}
59+
60+
Collections.sort(students);
61+
62+
// 정렬된 학생 정보에서 이름만 출력
63+
for (int i = 0; i < n; i++) {
64+
System.out.println(students.get(i).getName());
65+
}
66+
}
67+
}

14/2.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int n = sc.nextInt();
8+
9+
ArrayList<Integer> arrayList = new ArrayList<>();
10+
for (int i = 0; i < n; i++) {
11+
arrayList.add(sc.nextInt());
12+
}
13+
14+
Collections.sort(students);
15+
16+
// 중간값(median)을 출력
17+
System.out.println(v[(n - 1) / 2]);
18+
}
19+
}

14/3.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.*;
2+
3+
class Node implements Comparable<Node> {
4+
5+
private int stage;
6+
private double fail;
7+
8+
public Node(int stage, double fail) {
9+
this.stage = stage;
10+
this.fail = fail;
11+
}
12+
13+
public int getStage() {
14+
return this.stage;
15+
}
16+
17+
@Override
18+
public int compareTo(Node other) {
19+
if (this.fail == other.fail) {
20+
return Integer.compare(this.stage, other.stage);
21+
}
22+
return Double.compare(other.fail, this.fail);
23+
}
24+
}
25+
26+
class Solution {
27+
public int[] solution(int N, int[] stages) {
28+
int[] answer = new int[N];
29+
ArrayList<Node> arrayList = new ArrayList<>();
30+
int length = stages.length;
31+
32+
// 스테이지 번호를 1부터 N까지 증가시키며
33+
for (int i = 1; i <= N; i++) {
34+
// 해당 스테이지에 머물러 있는 사람의 수 계산
35+
int cnt = 0;
36+
for (int j = 0; j < stages.length; j++) {
37+
if (stages[j] == i) {
38+
cnt += 1;
39+
}
40+
}
41+
42+
// 실패율 계산
43+
double fail = 0;
44+
if (length >= 1) {
45+
fail = (double) cnt / length;
46+
}
47+
48+
// 리스트에 (스테이지 번호, 실패율) 원소 삽입
49+
arrayList.add(new Node(i, fail));
50+
length -= cnt;
51+
}
52+
53+
// 실패율을 기준으로 각 스테이지를 내림차순 정렬
54+
Collections.sort(arrayList);
55+
56+
// 정렬된 스테이지 번호 반환
57+
for (int i = 0; i < N; i++) {
58+
answer[i] = arrayList.get(i).getStage();
59+
}
60+
return answer;
61+
}
62+
}

0 commit comments

Comments
 (0)