본문 바로가기
Algorithm/LeetCode

💯 Daily LeetCode Challenge Day_01 - Arranging Coins

by HaningYa 2020. 7. 1.
728x90

 

Account Login - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


문제

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

댓글