Arranging Coins LeetCode solution in C++

1
class Solution {
2
public:
3
int arrangeCoins(int n) {
4
int count = 0;
5
while (n > 0) {
6
count++;
7
n = n - count;
8
}
9
if (n == 0) // all the coins are used to make complete rows so n if fully
10
// uitlized till 0
11
{
12
return count;
13
} else if (n < 0) // if the last row has not been created fully then n will
14
// go negative
15
{
16
return count - 1;
17
}
18
return -1; // this will never get encountered
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0