File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ // 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.
2+
3+ // Given n, find the total number of full staircase rows that can be formed.
4+
5+ // n is a non-negative integer and fits within the range of a 32-bit signed integer.
6+
7+ // Example 1:
8+
9+ // n = 5
10+
11+ // The coins can form the following rows:
12+ // ¤
13+ // ¤ ¤
14+ // ¤ ¤
15+
16+ // Because the 3rd row is incomplete, we return 2.
17+ // Example 2:
18+
19+ // n = 8
20+
21+ // The coins can form the following rows:
22+ // ¤
23+ // ¤ ¤
24+ // ¤ ¤ ¤
25+ // ¤ ¤
26+
27+ // Because the 4th row is incomplete, we return 3.
28+
29+
30+ // class Solution {
31+ // public:
32+ // // TLE for INT_MAX
33+ // int arrangeCoins(int n) {
34+ // int count = 0;
35+ // int sum = 0;
36+ // int i = 1;
37+ // while(sum <= n){
38+ // sum += i;
39+ // i++;
40+ // count++;
41+ // }
42+ // return count-1;
43+ // }
44+ // };
45+
46+ class Solution {
47+ public:
48+ int arrangeCoins (int n) {
49+ // 8*n will overflow
50+ return int (floor (((sqrt (1 + double (8 ) * n)) - 1 ) / 2 ));
51+ }
52+ };
You can’t perform that action at this time.
0 commit comments