Skip to content

Commit 6a91a86

Browse files
committed
[Greedy] baekjoon-2839
1 parent be4311b commit 6a91a86

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
| 11 | | [Baekjoon-13549 숨바꼭질 3](./src/BFS/P13549) | |
107107
| 12 | ⭐️ | [Baekjoon-20304 비밀번호 제작](./src/BFS/P20304) | |
108108

109+
## Greedy Algorithm
110+
111+
| # || Problem | Note |
112+
| :-: | :-: | :----------------------------------------------------- | :--- |
113+
| 01 | | [Baekjoon-2839 설탕 배달](./src/Greedy/P2839) | |
114+
109115
## Data Structure
110116

111117
### Array

src/Greedy/P2839/Main.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Greedy.P2839;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
int N = sc.nextInt();
10+
int ans = 0;
11+
while (N > 0) {
12+
if (N%5==0) { ans += N/5; N = 0; }
13+
else { ans ++; N-= 3; }
14+
}
15+
if (N < 0) System.out.println(-1);
16+
else System.out.println(ans);
17+
}
18+
}

src/Greedy/P2839/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## [baekjoon-2839] 설탕 배달
2+
3+
![image](https://user-images.githubusercontent.com/22045163/108313759-2f0cd700-71fc-11eb-9d3f-36acbec3d89f.png)
4+
5+
### Greedy Algorithm vs DP
6+
7+
- [using Greedy Alg](Main.java)
8+
![image](https://user-images.githubusercontent.com/22045163/108313951-77c49000-71fc-11eb-8ec5-c4b1fb8b2e0a.png)
9+
10+
- [using DP](usingDP.java)
11+
![image](https://user-images.githubusercontent.com/22045163/108313992-857a1580-71fc-11eb-990b-564ab4aab094.png)

src/Greedy/P2839/usingDP.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Greedy.P2839;
2+
3+
import java.util.*;
4+
5+
public class usingDP {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
10+
int N = sc.nextInt();
11+
12+
int[] dp = new int[5001];
13+
Arrays.fill(dp, 5000);
14+
15+
dp[3] = 1;
16+
dp[5] = 1;
17+
for (int i = 6; i <= N; i++) {
18+
dp[i] = Math.min(dp[i-3], dp[i-5]) + 1;
19+
}
20+
21+
if (dp[N] >= 5000) System.out.println(-1);
22+
else System.out.println(dp[N]);
23+
}
24+
}

0 commit comments

Comments
 (0)