-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path441_Arranging Coins
62 lines (45 loc) · 1.35 KB
/
441_Arranging Coins
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
/*Runtime: 1 ms, faster than 100.00% of Java online submissions for Arranging Coins.
Memory Usage: 37.1 MB, less than 34.64% of Java online submissions for Arranging Coins.*/
class Solution {
public int arrangeCoins(int n) {
return (int) ((Math.sqrt(1 + 8.0 * n) - 1) / 2);
}
}
/*using for loop
Runtime: 8 ms, faster than 28.90% of Java online submissions for Arranging Coins.
Memory Usage: 36.5 MB, less than 95.78% of Java online submissions for Arranging Coins.
class Solution {
public int arrangeCoins(int n) {
if(n==0)
return 0;
int temp=n/2;
temp++;
for(int i=1;i<=temp;i++){
n=n-i;
if(n<0)
return i-1;
if(n==0)
return i;
}
return 0;
}
}
*/
/*Using recursion
Runtime: 58 ms, faster than 5.02% of Java online submissions for Arranging Coins.
Memory Usage: 41.1 MB, less than 5.04% of Java online submissions for Arranging Coins.
class Solution {
public int arrangeCoins(int n) {
if(n==0)
return 0;
return helper(n,1,0);
}
public int helper(int n,int c,int count) {
if(n<0)
return count-1;
if(n==0)
return count;
return helper(n-c,c+1,count+1);
}
}
*/