Skip to content

441. Arranging Coins

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  int arrangeCoins(long n) {
    return (-1 + sqrt(8 * n + 1)) / 2;
  }
};
1
2
3
4
5
class Solution {
  public int arrangeCoins(long n) {
    return (int) (-1 + Math.sqrt(8 * n + 1)) / 2;
  }
}
1
2
3
class Solution:
  def arrangeCoins(self, n: int) -> int:
    return int((-1 + math.sqrt(8 * n + 1)) // 2)