File tree 2 files changed +42
-20
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder 2 files changed +42
-20
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
+ * 441. Arranging Coins
5
+ *
4
6
* You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
5
7
6
8
Given n, find the total number of full staircase rows that can be formed.
32
34
Because the 4th row is incomplete, we return 3.*/
33
35
public class _441 {
34
36
35
- public static int arrangeCoins (int n ) {
36
- if (n < 2 ) {
37
- return n ;
37
+ public static class Solution1 {
38
+ public int arrangeCoins (int n ) {
39
+ if (n < 2 ) {
40
+ return n ;
41
+ }
42
+ int row = 0 ;
43
+ int count = 0 ;
44
+ long sum = 0 ;
45
+ while (sum < n ) {
46
+ row += 1 ;
47
+ sum += row ;
48
+ count ++;
49
+ }
50
+ if (sum == n ) {
51
+ return count ;
52
+ }
53
+ return count - 1 ;
38
54
}
39
- int row = 0 ;
40
- int count = 0 ;
41
- long sum = 0 ;
42
- while (sum < n ) {
43
- row += 1 ;
44
- sum += row ;
45
- count ++;
46
- }
47
- if (sum == n ) {
48
- return count ;
49
- }
50
- return count - 1 ;
51
55
}
52
56
53
- public static void main (String ... args ) {
54
- int n = 3 ;//should be 2
55
- System .out .println (arrangeCoins (n ));
56
- }
57
- }
57
+ }
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._441 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .assertEquals ;
8
+
9
+ public class _441Test {
10
+ private static _441 .Solution1 solution1 ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ solution1 = new _441 .Solution1 ();
15
+ }
16
+
17
+ @ Test
18
+ public void test1 () {
19
+ assertEquals (2 , solution1 .arrangeCoins (3 ));
20
+ }
21
+
22
+ }
You can’t perform that action at this time.
0 commit comments