๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 19709_LunchBox

๋ฟŒ์•ผ._. 2025. 3. 14. 17:28
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/19709)

< LunchBox >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ•™๊ต์—์„œ ์š”์ฒญํ•œ ๋„์‹œ๋ฝ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ N๊ฐœ์˜ ๋„์‹œ๋ฝ์„ ๋‚˜๋ˆ ์ค„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํ•™๊ต ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

my solution (Java)

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.StringTokenizer;
 
 public class _19709_ { // LunchBox
 
 	public static void main(String[] args) throws IOException {
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 
 		int n = Integer.parseInt(st.nextToken());
 		int m = Integer.parseInt(st.nextToken());
 
 		int arr[] = new int[m];
 		for (int i = 0; i < m; i++) {
 			arr[i] = Integer.parseInt(bf.readLine());
 		}
 
 		Arrays.sort(arr);
 
 		int result = 0;
 		for (int i = 0; i < m; i++) {
 			if (n - arr[i] >= 0) {
 				n -= arr[i];
 				result += 1;
 			} else {
 				break;
 			}
 		}
 		System.out.println(result);
 	}
 }
๋ณ€์ˆ˜)
n : ๋„์‹œ๋ฝ ์ˆ˜
m : ํ•™๊ต ์ˆ˜
arr : ๊ฐ ํ•™๊ต์˜ ํ•„์š”ํ•œ ๋„์‹œ๋ฝ ์ˆ˜
result : ์ตœ๋Œ€ ํ•™๊ต ์ˆ˜

 

๋„์‹œ๋ฝ ์ˆ˜ n๊ณผ ํ•™๊ต ์ˆ˜ m์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. m๋งŒํผ ๊ฐ ํ•™๊ต์˜ ํ•„์š”ํ•œ ๋„์‹œ๋ฝ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ ์•ž์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ n๊ฐœ์˜ ๋„์‹œ๋ฝ์œผ๋กœ ๋‚˜๋ˆ ์ค„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํ•™๊ต ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 12724_Minimum Scalar Product (Large)  (0) 2025.03.13
[Baekjoon] 12723_Minimum Scalar Product (Small)  (0) 2025.03.11
[Baekjoon] 24155_ๅพ—็‚น (Score)  (0) 2025.03.10
[Baekjoon] 17599_Bags  (0) 2025.02.28
[Baekjoon] 5092_Air Old Zeeland  (0) 2025.02.27