Skip to content

Instantly share code, notes, and snippets.

@bonoogi
Created July 2, 2020 13:51
Show Gist options
  • Save bonoogi/f5f80c37607c47b4bfc475c47dfc3747 to your computer and use it in GitHub Desktop.
Save bonoogi/f5f80c37607c47b4bfc475c47dfc3747 to your computer and use it in GitHub Desktop.
swift
class Solution {
func arrangeCoins(_ n: Int) -> Int {
let sq = Int(sqrt(Double(n * 2)))
let diff = sq * (sq + 1) / 2
if n >= diff {
return sq
} else {
return sq - 1
}
}
}
@bonoogi
Copy link
Author

bonoogi commented Jul 2, 2020

맨 처음에 시도했던 방법. Kotlin

class Solution {
    fun arrangeCoins(n: Int): Int {
        var number = n
        var staircase = 1
        var increase = 0
        while (staircase <= number) {
            number -= staircase
            staircase++
            increase++
        }
        return increase
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment