728x90
문제
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.
Given n, find the total number of full staircase rows that can be formed.
n is a non-negative integer and fits within the range of a 32-bit signed integer.
class Solution {
func arrangeCoins(_ n: Int) -> Int {
//이전보다 작은 경우
//이전과 같은 경우
//1...n 까지 합은 n(n+1)/2
var x = 0
var coin = n
while x*(x+1) < n*2 {
x += 1
coin -= x
if coin <= x {
break
}
}
return x
}
}
728x90
'Algorithm > LeetCode' 카테고리의 다른 글
💯 Daily LeetCode Challenge Day_03 - Prison Cells After N Days (0) | 2020.07.03 |
---|---|
💯 Daily LeetCode Challenge Day_02 - Binary Tree Level Order Traversal II (0) | 2020.07.03 |
LeetCode - 994. Rotting Oranges (0) | 2020.06.01 |
LeetCode - 15. 3Sum (0) | 2020.06.01 |
LeetCode - 3. Longest Substring Without Repeating Characters (0) | 2020.05.28 |
댓글