Skip to content

Commit 7b0a2f9

Browse files
refactor 441
1 parent ca4b5a3 commit 7b0a2f9

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/main/java/com/fishercoder/solutions/_441.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 441. Arranging Coins
5+
*
46
* 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.
57
68
Given n, find the total number of full staircase rows that can be formed.
@@ -32,26 +34,24 @@
3234
Because the 4th row is incomplete, we return 3.*/
3335
public class _441 {
3436

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;
3854
}
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;
5155
}
5256

53-
public static void main(String... args) {
54-
int n = 3;//should be 2
55-
System.out.println(arrangeCoins(n));
56-
}
57-
}
57+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy