LeetCode //C - 441. Arranging Coins

441. Arranging Coins

You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the i t h i^{th} 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 3 r d 3^{rd} 3rd row is incomplete, we return 2.

Example 2:

Input: n = 8
Output: 3
Explanation: Because the 4 t h 4^{th} 4th row is incomplete, we return 3.

Constraints:
  • 1 < = n < = 2 31 − 1 1 <= n <= 2^{31} - 1 1<=n<=231−1

From: LeetCode

Link: 441. Arranging Coins


Solution:

Ideas:

1. Binary Search: This is an efficient algorithm used to find an item in a sorted list. The algorithm repeatedly divides the range of possible solutions in half, choosing the half where the item must be, until it narrows down to the exact location.

2. Using the Formula for the Sum of Natural Numbers: To find how many coins are needed to completely fill k rows, the formula k * (k + 1) / 2 is used. This is the sum of the first k natural numbers, and in the context of this problem, it represents the total number of coins needed to fill k rows of the staircase.

3. Finding the Maximum k: The goal is to find the largest k such that k * (k + 1) / 2 is less than or equal to n. This k represents the number of complete rows you can build with n coins.

4. Binary Search Implementation:

  • The search starts with the full range of possible row counts (left at 0 and right at n).
    It repeatedly calculates the middle point (mid) and the number of coins needed to fill that many rows (curr).
  • If curr equals n, we've found the exact number of rows we can fill and return mid.
  • If curr is greater than n, we have too many rows, so we reduce the search range to the left half.
  • If curr is less than n, we have too few rows, so we increase the search range to the right half.
  • The search stops when left exceeds right, meaning we've narrowed down the range as much as possible. At this point, right will be the greatest number of complete rows that can be built with n coins.
Code:
c 复制代码
int arrangeCoins(int n) {
    long left = 0, right = n;
    long mid, curr;
    
    while (left <= right) {
        mid = left + (right - left) / 2;
        curr = mid * (mid + 1) / 2;
        
        if (curr == n) return (int)mid;
        
        if (n < curr) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    
    return (int)right;
}
相关推荐
StarPrayers.15 分钟前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
qiuiuiu41322 分钟前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
爱吃橘的橘猫23 分钟前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
2351626 分钟前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
weixin_307779132 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json
柳安忆2 小时前
【论文阅读】Sparks of Science
算法
web安全工具库2 小时前
从课堂笔记到实践:深入理解Linux C函数库的奥秘
java·数据库·算法
爱编程的鱼3 小时前
C# 变量详解:从基础概念到高级应用
java·算法·c#
tkevinjd4 小时前
反转链表及其应用(力扣2130)
数据结构·leetcode·链表
HalvmånEver4 小时前
红黑树实现与原理剖析(上篇):核心规则与插入平衡逻辑
数据结构·c++·学习·算法·红黑树