Skip to content

승연 백준 숙제(8/10/화) #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
.DS_Store
.DS_Store
codingtest_JavaJobJava.iml
.idea
out
88 changes: 88 additions & 0 deletions syheo/baekjoon_java/Main_1202.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package syheo.baekjoon_java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
* solved.ac
* 백준
* 1202
* 보석 도둑
* 그리디
* 아이디어 :
* 가방과 보석을 무게를 기준으로 오름차순 정렬
* 현재 가방에 담을 수 있는 보석을 우선순위 큐에 넣고
* 가장 가치가 높은 보석을 담음.
* 보석과 가방의 갯수가 300,000, 최대 가치가 1,000,000 이므로 최악의 경우 300,000,000,000 이 되므로 long 타입을 써줘야됨.
* */

public class Main_1202 {

static class Info implements Comparable<Info>{
int weight;
int value;

public Info(int weight,int value){
this.weight = weight;
this.value = value;
}

@Override
public int compareTo(Info info) {
if(this.weight > info.weight) {
return 1; // weight 에 대해서 내림차순
}
return -1;
}
}


static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int N, K; // 보석의 갯수, 가방의 갯수
static ArrayList<Integer> bagWeights = new ArrayList<>(); //가방 무게 리스트
static ArrayList<Info> jewels = new ArrayList<>(); // 보석 정보 리스트
static PriorityQueue<Integer> candidates = new PriorityQueue<>(Collections.reverseOrder()); //가방에 담을 보석 우선순위 큐(최대 힙)
//보석과 가방의 갯수가 300,000, 최대 가치가 1,000,000 이므로 최악의 경우 300,000,000,000 이 되므로 long 타입을 써줘야됨.
static long result = 0;


public static void main(String[] args) throws IOException {

// 1. input
tokens = new StringTokenizer(input.readLine());
N = Integer.parseInt(tokens.nextToken());
K = Integer.parseInt(tokens.nextToken());

for (int i = 0; i < N; i++) {
tokens = new StringTokenizer(input.readLine());
jewels.add(new Info(Integer.parseInt(tokens.nextToken()),Integer.parseInt(tokens.nextToken())));
}

for (int i = 0; i < K; i++) {
bagWeights.add(Integer.parseInt(input.readLine()));
}

// 2. sort
Collections.sort(jewels); // 무게에 대해서 오름차순
Collections.sort(bagWeights); // 무게에 대해서 오름차순

int id = 0;
// 3. 가방에 보석 담기 -> 현재 가방에 담을 수 있는것을 우선순위 큐에 넣음.
for (int i = 0; i < K; i++) {
while(id< jewels.size() && jewels.get(id).weight <= bagWeights.get(i)){
candidates.add(jewels.get(id).value);
id++;

}
if(!candidates.isEmpty()){
result += candidates.poll();
}
}

// 4. print
System.out.println(result);
}
}
77 changes: 77 additions & 0 deletions syheo/baekjoon_java/Main_1541.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package syheo.baekjoon_java;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
* solved.ac
* 백준
* 1541
* 잃어버린 괄호
* 그리디
*/

public class Main_1541 {

static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static ArrayList<Character> operators = new ArrayList<>();
static Queue<Integer> numbers = new LinkedList<>();
static int result;

public static void main(String[] args) throws IOException {
// 1. input
String str = input.readLine();

int startswith = 0;
// 2. operators , numbers 추출
for (int i = 0; i < str.length(); i++) {
// operator case
if ('0' > str.charAt(i) || str.charAt(i) > '9') {
numbers.add(Integer.parseInt(str.substring(startswith,i)));
operators.add(str.charAt(i));
startswith = i+1;
}
}

// 2-1. 마지막 숫자 add
numbers.add(Integer.parseInt(str.substring(startswith,str.length())));

// 3. 그리디
// 3-1. 첫 숫자로 초기화.
result = numbers.poll();

// 3-2. 연산자 갯수만큼 반복 -> '-'가 분기, '-'를 만나면 다음 '-'를 만나기 전까지 뺌
int sum = 0;
boolean isMinus = false;
for (int i = 0; i < operators.size(); i++) {
if(operators.get(i)=='-'){
if(!isMinus){
result += sum;
}
else{
result -= sum;
}
isMinus = true;
sum = numbers.poll();
}
else{
sum += numbers.poll();
}
}
if(!isMinus){
result += sum;
}
else {
result -= sum;
}

//4. print
System.out.println(result);

}
}
Loading