-
[백준 11407] 동전 0코딩테스트 문제풀이 2021. 9. 22. 13:46
문제
풀이
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int tokenCount = scan.nextInt(); int goal = scan.nextInt(); List<Integer> tokenList = new ArrayList<Integer>(); for (int i = 0; i < tokenCount; i++) { tokenList.add(scan.nextInt()); } int count = 0; for(int i = tokenCount-1; i>=0; i--){ if(goal>=tokenList.get(i)){ count += goal/tokenList.get(i); goal = goal%tokenList.get(i); } } System.out.println(count); } }
- 가장 큰 동전을 하나씩 선택하여 goal의 값을 줄여나간다.
- 그리디 알고리즘 대표 문제
'코딩테스트 문제풀이' 카테고리의 다른 글
[프로그래머스] 소수 찾기 - 완전탐색 (0) 2021.10.02 [백준 2231] 분해합 (0) 2021.09.22 [백준 2606] 바이러스 (0) 2021.09.19 [백준 1260] DFS와 BFS (0) 2021.09.19 [프로그래머스] 구명보트 - Greedy (0) 2021.09.13