FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_26/Leetcode_441_26.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_441_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (47 loc) · 1.52 KB
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_441_26.java
Copy path
File metadata and controls
50 lines (47 loc) · 1.52 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
// Source : https://leetcode.com/problems/arranging-coins/
// Id : 441
// Author : Fanlu Hai
// Date : 2018-04-17
public
class
ArrangingCoins
{
public
int
arrangeCoinsWithLong
(
int
n
) {
int
rows
=
0
;
long
coins
=
0
;
while
(
true
) {
// System.out.println(n + "**" + coins + " " + rows);
if
(
n
>
coins
) {
rows
++;
coins
+=
rows
;
}
else
if
(
n
==
coins
) {
return
rows
;
}
else
{
return
rows
-
1
;
}
}
}
//faster than 82.04%, smaller than 100%
public
int
arrangeCoins
(
int
n
) {
int
rows
=
0
;
long
coins
=
0
;
while
(
true
) {
// System.out.println(n + "**" + coins + " " + rows);
if
(
n
>
0
) {
rows
++;
// use n - row instead of coins + to avoid int overflow
n
-=
rows
;
}
else
if
(
n
==
0
) {
return
rows
;
}
else
{
return
rows
-
1
;
}
}
}
public
static
void
main
(
String
[]
args
) {
ArrangingCoins
arrangingCoins
=
new
ArrangingCoins
();
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
0
));
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
1
));
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
5
));
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
6
));
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
8
));
System
.
out
.
println
(
arrangingCoins
.
arrangeCoins
(
2147483647
));
}
}
Back
|
FazBrowse Home
|
New Git URL