Skip to content

Commit 59ec40f

Browse files
authored
승연 백준 숙제(8/10/화) (#99)
* 1202, 1541 * 18139
1 parent 850f9fb commit 59ec40f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+486
-1
lines changed

‎.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.idea/
2-
.DS_Store
2+
.DS_Store
3+
codingtest_JavaJobJava.iml
4+
.idea
5+
out

‎syheo/baekjoon_java/Main_1202.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
/**
9+
* solved.ac
10+
* 백준
11+
* 1202
12+
* 보석 도둑
13+
* 그리디
14+
* 아이디어 :
15+
* 가방과 보석을 무게를 기준으로 오름차순 정렬
16+
* 현재 가방에 담을 수 있는 보석을 우선순위 큐에 넣고
17+
* 가장 가치가 높은 보석을 담음.
18+
* 보석과 가방의 갯수가 300,000, 최대 가치가 1,000,000 이므로 최악의 경우 300,000,000,000 이 되므로 long 타입을 써줘야됨.
19+
* */
20+
21+
public class Main_1202 {
22+
23+
static class Info implements Comparable<Info>{
24+
int weight;
25+
int value;
26+
27+
public Info(int weight,int value){
28+
this.weight = weight;
29+
this.value = value;
30+
}
31+
32+
@Override
33+
public int compareTo(Info info) {
34+
if(this.weight > info.weight) {
35+
return 1; // weight 에 대해서 내림차순
36+
}
37+
return -1;
38+
}
39+
}
40+
41+
42+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
43+
static StringTokenizer tokens;
44+
static int N, K; // 보석의 갯수, 가방의 갯수
45+
static ArrayList<Integer> bagWeights = new ArrayList<>(); //가방 무게 리스트
46+
static ArrayList<Info> jewels = new ArrayList<>(); // 보석 정보 리스트
47+
static PriorityQueue<Integer> candidates = new PriorityQueue<>(Collections.reverseOrder()); //가방에 담을 보석 우선순위 큐(최대 힙)
48+
//보석과 가방의 갯수가 300,000, 최대 가치가 1,000,000 이므로 최악의 경우 300,000,000,000 이 되므로 long 타입을 써줘야됨.
49+
static long result = 0;
50+
51+
52+
public static void main(String[] args) throws IOException {
53+
54+
// 1. input
55+
tokens = new StringTokenizer(input.readLine());
56+
N = Integer.parseInt(tokens.nextToken());
57+
K = Integer.parseInt(tokens.nextToken());
58+
59+
for (int i = 0; i < N; i++) {
60+
tokens = new StringTokenizer(input.readLine());
61+
jewels.add(new Info(Integer.parseInt(tokens.nextToken()),Integer.parseInt(tokens.nextToken())));
62+
}
63+
64+
for (int i = 0; i < K; i++) {
65+
bagWeights.add(Integer.parseInt(input.readLine()));
66+
}
67+
68+
// 2. sort
69+
Collections.sort(jewels); // 무게에 대해서 오름차순
70+
Collections.sort(bagWeights); // 무게에 대해서 오름차순
71+
72+
int id = 0;
73+
// 3. 가방에 보석 담기 -> 현재 가방에 담을 수 있는것을 우선순위 큐에 넣음.
74+
for (int i = 0; i < K; i++) {
75+
while(id< jewels.size() && jewels.get(id).weight <= bagWeights.get(i)){
76+
candidates.add(jewels.get(id).value);
77+
id++;
78+
79+
}
80+
if(!candidates.isEmpty()){
81+
result += candidates.poll();
82+
}
83+
}
84+
85+
// 4. print
86+
System.out.println(result);
87+
}
88+
}

‎syheo/baekjoon_java/Main_1541.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.ArrayList;
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
11+
/**
12+
* solved.ac
13+
* 백준
14+
* 1541
15+
* 잃어버린 괄호
16+
* 그리디
17+
*/
18+
19+
public class Main_1541 {
20+
21+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
22+
static ArrayList<Character> operators = new ArrayList<>();
23+
static Queue<Integer> numbers = new LinkedList<>();
24+
static int result;
25+
26+
public static void main(String[] args) throws IOException {
27+
// 1. input
28+
String str = input.readLine();
29+
30+
int startswith = 0;
31+
// 2. operators , numbers 추출
32+
for (int i = 0; i < str.length(); i++) {
33+
// operator case
34+
if ('0' > str.charAt(i) || str.charAt(i) > '9') {
35+
numbers.add(Integer.parseInt(str.substring(startswith,i)));
36+
operators.add(str.charAt(i));
37+
startswith = i+1;
38+
}
39+
}
40+
41+
// 2-1. 마지막 숫자 add
42+
numbers.add(Integer.parseInt(str.substring(startswith,str.length())));
43+
44+
// 3. 그리디
45+
// 3-1. 첫 숫자로 초기화.
46+
result = numbers.poll();
47+
48+
// 3-2. ��산자 갯수만큼 반복 -> '-'가 분기, '-'를 만나면 다음 '-'를 만나기 전까지 뺌
49+
int sum = 0;
50+
boolean isMinus = false;
51+
for (int i = 0; i < operators.size(); i++) {
52+
if(operators.get(i)=='-'){
53+
if(!isMinus){
54+
result += sum;
55+
}
56+
else{
57+
result -= sum;
58+
}
59+
isMinus = true;
60+
sum = numbers.poll();
61+
}
62+
else{
63+
sum += numbers.poll();
64+
}
65+
}
66+
if(!isMinus){
67+
result += sum;
68+
}
69+
else {
70+
result -= sum;
71+
}
72+
73+
//4. print
74+
System.out.println(result);
75+
76+
}
77+
}

0 commit comments

Comments
 (0)