개발 공부/TIL(Today I Learned)

99클럽 코테 스터디 30일차 TIL Arranging Coins

애해 2024. 8. 21. 10:55
728x90

# 오늘의 학습 키워드 

이분탐색

 

# 오늘의 문제 

https://leetcode.com/problems/arranging-coins/description/

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:

 

Input: n = 5

Output: 2

Explanation: Because the 3rd row is incomplete, we return 2.

Example 2:

 

Input: n = 8

Output: 3

Explanation: Because the 4th row is incomplete, we return 3.

 

Constraints:

  • 1 <= n <= 231 - 1

 

# 나의 풀이방식 

class Solution {
    public int arrangeCoins(int n) {

 
    int i = 1;

    if(n == 1){ return 1;}

    while(n >= 0) {
        n -= i;
        i ++;
    }
    
    return i-2;
    
    }

}

 

 

 

반응형