문제
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
Given the integer n, return the number of complete rows of the staircase you will build.
Example 1:
!https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg
Input: n = 5
Output: 2
Explanation: Because the 3rd row is incomplete, we return 2.
Example 2:
!https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg
Input: n = 8
Output: 3
Explanation: Because the 4th row is incomplete, we return 3.
Constraints:
- 1 <= n <= 231 - 1
풀이
나의 풀이법
풀이 접근 과정
그냥 칸을 늘려주면서 coin 개수를 빼주면 된다고 생각했다.
최종 소스코드
class Solution {
fun arrangeCoins(n: Int): Int {
var space = 1
var coins = n
while (coins >= space) {
coins -= space
space++
}
return space-1
}
}
Comment
5분도 안 걸린 짧은 풀이
'LeetCode > Kotlin | Easy' 카테고리의 다른 글
[LeetCode/Kotlin]Easy - 463. Island Perimeter (0) | 2023.09.14 |
---|---|
[LeetCode/Kotlin]Easy - 345. Reverse Vowels of a String (0) | 2023.09.14 |
[LeetCode/Kotlin]Easy - 141. Linked List Cycle (0) | 2023.08.27 |
[LeetCode/Kotlin]Easy - 392. Is Subsequence (0) | 2023.07.07 |
[LeetCode/Kotlin]Easy - 58. Length of Last Word (0) | 2023.06.30 |