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;
}
相关推荐
YGGP5 小时前
【Golang】LeetCode 64. 最小路径和
算法·leetcode
意趣新5 小时前
C 语言源文件从编写完成到最终生成可执行文件的完整、详细过程
c语言·开发语言
古城小栈6 小时前
Rust变量设计核心:默认不可变与mut显式可变的深层逻辑
算法·rust
电商API&Tina6 小时前
跨境电商 API 对接指南:亚马逊 + 速卖通接口调用全流程
大数据·服务器·数据库·python·算法·json·图搜索算法
LYFlied6 小时前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
lengjingzju8 小时前
一网打尽Linux IPC(三):System V IPC
linux·服务器·c语言
长安er8 小时前
LeetCode 20/155/394/739/84/42/单调栈核心原理与经典题型全解析
数据结构·算法·leetcode·动态规划·
MarkHD8 小时前
智能体在车联网中的应用:第28天 深度强化学习实战:从原理到实现——掌握近端策略优化(PPO)算法
算法
能源系统预测和优化研究8 小时前
【原创代码改进】考虑共享储能接入的工业园区多类型负荷需求响应经济运行研究
大数据·算法
yoke菜籽8 小时前
LeetCode——三指针
算法·leetcode·职场和发展