-
금고털이코딩테스트/softeer 2023. 9. 3. 12:02
https://softeer.ai/practice/info.do?idx=1&eid=395
Softeer
연습문제를 담을 Set을 선택해주세요. 취소 확인
softeer.ai
뭔가 귀찮은 개념이 많이들어갔음
우선순위 정렬부터 다 외워야되나 싶은 문제
재밌지만 외울게 너무많아;;
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int limit = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken()); ArrayList<Item> itemList = new ArrayList<Item>(); for(int i=0; i<n; i++){ st = new StringTokenizer(in.readLine()); itemList.add(new Item(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()))); } Collections.sort(itemList, Collections.reverseOrder()); int result = 0; int currentWeight = 0; for(int i=0; i<n; i++){ int weight = 0; int restWeight = limit - currentWeight; if (restWeight <= itemList.get(i).getWeight()){ weight = restWeight; } else { weight = itemList.get(i).getWeight(); } currentWeight += weight; result += weight * itemList.get(i).getPrice(); if (currentWeight >= limit){ break; } } System.out.println(result); } public static class Item implements Comparable<Item> { private int weight; private int price; public Item (int weight, int price){ this.weight = weight; this.price = price; } public int getWeight(){ return this.weight; } public int getPrice(){ return this.price; } public int compareTo(Item item) { if (item.getPrice() < this.getPrice() ){ return 1; } else if(item.getPrice() > this.getPrice()){ return -1; } return 0; } } }
'코딩테스트 > softeer' 카테고리의 다른 글
장애물 인식 프로그램(java) (0) 2023.09.03 8단 변속기(java) (0) 2023.09.03 주행거리 비교하기 (java) (0) 2023.09.03 A+B (java) (0) 2023.09.03 근무시간 (java) (0) 2023.09.03