반응형
https://leetcode.com/problems/arranging-coins/description/
구현 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
정답 ans를 선언해줍니다. 상기 이미지의 한 행을 나타냅니다.
📔 풀이과정
n은 전체 coin, ans는 한 행을 나타내므로 n에서 ans를 빼고 1씩 증가시켜 주는 방식으로 loop를 수행합니다.n에서 ans만큼 즉, 한 행씩 빼면서 남은 동전이 채워야 될 현재 행 ans보다 작거나 같으면 1을 증가하지 않고 break 해줍니다.
📔 정답 출력 | 반환
ans를 반환합니다.
📕 Code
📔 C++
class Solution {
public:
int arrangeCoins(int n) {
int ans = 1;
while(1) {
n -= ans;
if(n <= ans) break;
ans++;
}
return ans;
}
};
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - LeetCode (easy) 485. Max Consecutive Ones (0) | 2023.03.28 |
---|---|
(C++) - LeetCode (easy) 461. Hamming Distance (0) | 2023.03.23 |
(Python) - LeetCode (easy) 415. Add Strings (0) | 2023.03.13 |
(C++) - LeetCode (easy) 409. Longest Palindrome (0) | 2023.03.09 |
(C++) - LeetCode (easy) 405. Convert a Number to Hexadecimal (0) | 2023.03.07 |