๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 6752_Time on task

๋ฟŒ์•ผ._. 2024. 12. 4. 16:22
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/6752)

< Time on task >

 

๋ฌธ์ œ ํ’€์ด 

 

์ •๋ ฌ ํ›„ ์ˆœ์ฐจ ํƒ์ƒ‰์„ ํ†ตํ•ด t์•ˆ์— ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

 my solution (Java)

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

public class _6752_ { // Time on task

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        
		int t = Integer.parseInt(bf.readLine());
		int c = Integer.parseInt(bf.readLine());
        
		int arr[] = new int[c];
		for (int i = 0; i < c; i++) {
			arr[i] = Integer.parseInt(bf.readLine());
		}
        
		Arrays.sort(arr);
        
		int result = -1;
		for (int i = 0; i < c; i++) {
			if (t - arr[i] >= 0) {
				t -= arr[i];
			} else {
				result = i;
				break;
			}
		}
		if (result == -1) {
			result = c;
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
t, c : ์‹œ๊ฐ„, ์ผ ๊ฐœ์ˆ˜
arr : ๋ฐฐ์—ด ์ •๋ณด
result : ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์˜ ๊ฐœ์ˆ˜

 

t, c๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. c๋งŒํผ ์ผ ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ ์ˆœ์ฐจ ํƒ์ƒ‰ํ•˜๋ฉฐ t ์‹œ๊ฐ„ ์•ˆ์— ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค. ์ตœ์ข… result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

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

[Baekjoon] 21177_No Thanks!  (1) 2024.12.06
[Baekjoon] 14769_Stacking Cups  (0) 2024.12.05
[Baekjoon] 21194_Meditation  (0) 2024.12.03
[Baekjoon] 6147_Bookshelf  (0) 2024.12.02
[Baekjoon] 9047_6174  (1) 2024.11.29