File tree 4 files changed +59
-0
lines changed
4 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 106
106
| 11 | | [ Baekjoon-13549 숨바꼭질 3] ( ./src/BFS/P13549 ) | |
107
107
| 12 | ⭐️ | [ Baekjoon-20304 비밀번호 제작] ( ./src/BFS/P20304 ) | |
108
108
109
+ ## Greedy Algorithm
110
+
111
+ | # | ☆ | Problem | Note |
112
+ | :-: | :-: | :----------------------------------------------------- | :--- |
113
+ | 01 | | [ Baekjoon-2839 설탕 배달] ( ./src/Greedy/P2839 ) | |
114
+
109
115
## Data Structure
110
116
111
117
### Array
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments