FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-study/coin-change/bky373.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
/
bky373.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
22 lines (21 loc) · 621 Bytes
Breadcrumbs
leetcode-study
/
coin-change
/
bky373.java
Copy path
File metadata and controls
22 lines (21 loc) · 621 Bytes
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
// time: O(n * m), where n is the amount and m is the number of coins
// space: O(n)
class
Solution
{
public
int
coinChange
(
int
[]
coins
,
int
amount
) {
int
max
=
amount
+
1
;
int
[]
dp
=
new
int
[
amount
+
1
];
Arrays
.
fill
(
dp
,
max
);
dp
[
0
] =
0
;
for
(
int
i
=
1
;
i
<=
amount
;
i
++) {
for
(
int
j
=
0
;
j
<
coins
.
length
;
j
++) {
if
(
coins
[
j
] <=
i
) {
dp
[
i
] =
Math
.
min
(
dp
[
i
],
dp
[
i
-
coins
[
j
]] +
1
);
}
}
}
if
(
dp
[
amount
] >
amount
) {
return
-
1
;
}
return
dp
[
amount
];
}
}
Back
|
FazBrowse Home
|
New Git URL