-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path441.java
More file actions
42 lines (41 loc) · 1.14 KB
/
441.java
File metadata and controls
42 lines (41 loc) · 1.14 KB
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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int arrangeCoins(int n) {
if (n <= 1) {
return n;
}
long low = 1;
long high = n;
long result = 0;
while (low <= high) {
long mid = low + (high - low) / 2;
if ((mid * (mid + 1) / 2) < n) {
low = mid + 1;
} else {
result = mid;
high = mid - 1;
}
}
if (result * (result + 1) / 2 == n) {
return (int)result;
} else {
return (int)result - 1;
}
}
}
__________________________________________________________________________________________________
sample 32140 kb submission
class Solution {
public int arrangeCoins(int n) {
int i = 1;
int rows = 0;
while (n >= 0) {
n-= i;
if (n >= 0) rows++;
i++;
}
return rows;
}
}
__________________________________________________________________________________________________