백준 그리디 - 설탕배달, 문제풀이, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy, baekjoon, acmicpc

// Process
// 1. Input n
// 2. 5씩 배달할 수 있는 최대 횟수를 구한다.
// 3. 만족할 때까지 반복한다.
// 3.1. 최대횟수로 5씩 배달하고 남은 나머지가 3의 배수인지 확인한다.
// 3.1.1. 3의 배수이면 -> 만족하고 끝, 5횟수와 3횟수를 더한 값 리턴
// 3.1.2. 아니면 -> 5의 최대횟수를 줄이고, 나머지에 5를 더한다.
// 3.1.2.1. 이 때, 5의 최대횟수가 0보다 작으면 (5, 3으로 해결 할 수 없는 숫자이면) -> -1로 종료한다.
import java.io.IOException;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws IOException {
//input
Scanner scanner = new Scanner(System.in);
int candies = scanner.nextInt();
//logic
int deliveryCount = 0;
boolean isDone = false;
if (candies == 3) {
deliveryCount = 1;
isDone = true;
}
if (candies == 4) {
deliveryCount = -1;
isDone = true;
}
if (!isDone) {
int maxFive = candies / 5;
int fiveRemainder = candies % 5;
while (!isDone) {
if (fiveRemainder % 3 == 0) {
isDone = true;
deliveryCount = maxFive + (fiveRemainder / 3);
} else {
if (maxFive > 0) {
--maxFive;
fiveRemainder += 5;
} else {
deliveryCount = -1;
isDone = true;
}
}
}
}
//output
System.out.println(deliveryCount);
}
}
탐욕법 05 - 섬 연결하기, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm,
최소신장트리 (Minimum Spanning Tree, MST) 문제였는데, 처음보는거라서 Kruskal, Prim 방식 2가지가 있다고 해서 2가지로 풀어두었다. import java.util.*; // Kruskal mst // Process // 1. Input n, costs //..
itdar.tistory.com
힙 01 - 더맵게, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정,
# Process # 1. Input scoville array and threshold K # 2. Make heap queue # 3. Iterate while all scoville is over K # 3.1. Check scoville (smallest one) # 3.1.1. If..
itdar.tistory.com