FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-study/coin-change/dohyeon2.java at main · DaleStudy/leetcode-study · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DaleStudy
/
leetcode-study
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
361
Star
155
Code
Issues
75
Pull requests
5
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-study
/
coin-change
/
dohyeon2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (74 loc) · 2.59 KB
Breadcrumbs
leetcode-study
/
coin-change
/
dohyeon2.java
Copy path
File metadata and controls
93 lines (74 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import
java
.
util
.
Arrays
;
class
Solution
{
// TC: O(amount * coins.length)
// SC: O(amount)
public
int
coinChange
(
int
[]
coins
,
int
amount
) {
int
[]
dp
=
new
int
[
amount
+
1
];
// Maximum depth = amount
// amount + 1 means undefined; set to use Math.min
Arrays
.
fill
(
dp
,
amount
+
1
);
dp
[
0
] =
0
;
for
(
int
i
=
1
;
i
<=
amount
;
i
++){
for
(
int
coin
:
coins
){
if
(
i
-
coin
>=
0
){
// depth without the coin + with the coin(1)
dp
[
i
] =
Math
.
min
(
dp
[
i
],
dp
[
i
-
coin
] +
1
);
}
}
}
return
dp
[
amount
] >
amount
? -
1
:
dp
[
amount
];
}
}
// DFS + Memoization
// class Solution {
// public int coinChange(int[] coins, int amount) {
// int[] memo = new int[amount + 1];
// Arrays.fill(memo, -2); // -2 = 아직 계산 안함
// return dfs(0, coins, amount, memo);
// }
// private int dfs(int depth, int[] coins, int amount, int[] memo){
// if(amount < 0){
// return -1;
// }
// if(amount == 0){
// return depth;
// }
// if(memo[amount] != -2) return memo[amount];
// int min = -1;
// for(int i = 0; i < coins.length; i++){
// int descended = coins[coins.length - i - 1];
// int d = dfs(depth + 1, coins, amount - descended, memo);
// if(d >= 0 && (d < min || min == -1)){
// min = d;
// }
// }
// if(memo[amount] == -2) memo[amount] = min; // 계산한 적 없는 경우에만 저장
// return min;
// }
// }
// BFS : 문제 서술과 가장 어울리는 풀이지만, Bottom-Up DP가 더 빠른 결과.
// class Solution {
// public int coinChange(int[] coins, int amount) {
// Queue<Integer> queue = new LinkedList<>();
// boolean[] visited = new boolean[amount + 1];
// queue.offer(amount);
// visited[amount] = true;
// int level = 0;
// while (!queue.isEmpty()) {
// int size = queue.size();
// for (int i = 0; i < size; i++) {
// int cur = queue.poll();
// if (cur == 0) return level;
// for (int coin : coins) {
// int next = cur - coin;
// if (next >= 0 && !visited[next]) {
// visited[next] = true;
// queue.offer(next);
// }
// }
// }
// level++;
// }
// return -1;
// }
// }
Back
|
FazBrowse Home
|
New Git URL