441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

Given n, find the total number of full staircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

Example 1:

n = 5

The coins can form the following rows:
¤
¤ ¤
¤ ¤

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

n = 8

The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

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

要处理mid 的越界问题

public int arrangeCoins(int n) {
        if (n == 0) return 0;
        int beg = 1, end = n;
        while (beg + 1 < end) {
            long mid = beg + (end - beg) / 2;
            if (mid * (mid + 1) / 2 <= n) {
                beg = (int)mid;
            } else {
                end = (int)mid;
            }
        }
        return beg;

    }

  

时间: 2024-11-05 17:24:51

441. Arranging Coins的相关文章

LeetCode 441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[Leetcode + Lintcode] 441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[LeetCode] 441. Arranging Coins 排列硬币

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

441. 硬币楼梯 Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

leetcode 441. 排列硬币(Arranging Coins)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 1: n = 5 硬币可排列成以下几行: ¤ ¤ ¤ ¤ ¤ 因为第三行不完整,所以返回2. 示例 2: n = 8 硬币可排列成以下几行: ¤ ¤ ¤ ¤ ¤ ¤ ¤ ¤ 因为第四行不完整,所以返回3. 解法: class Soluti

LeetCode Arranging Coins

原题链接在这里:https://leetcode.com/problems/arranging-coins/ 题目: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be forme

[LeetCode] Arranging Coins 排列硬币

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

【Leetcode441--数学】Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

138.Arranging Coins

题目: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. 您想要以楼梯形状形成总共n个硬币,其中每个第k行必须具有恰好k个硬币. Given n, find the total number of full staircase rows that can be formed. 给定n,找到可以形成的完整楼梯行