首页 > 其他分享 >leetcode-441-easy

leetcode-441-easy

时间:2023-01-03 21:45:10浏览次数:48  
标签:return staircase int 441 coins easy leetcode incomplete row

Arranging Coins

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

思路一:用求和公式循环计算。看了一下题解,最简单的是用一元二次方程直接计算答案。还有一种是二分法,搜索 1-n 中符合答案的解

    public int arrangeCoins(int n) {
        int result = 1;
        while (countSum(result) <= n) {
            result++;
        }

        return result - 1;
    }

    public static long countSum(long n) {
        return (1 + n) * n / 2;
    }

标签:return,staircase,int,441,coins,easy,leetcode,incomplete,row
From: https://www.cnblogs.com/iyiluo/p/17023455.html

相关文章

  • leetcode-459-easy
    RepeatedSubstringPatternGivenastrings,checkifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether......
  • leetcode-492-easy
    ConstructtheRectangleAwebdeveloperneedstoknowhowtodesignawebpage'ssize.So,givenaspecificrectangularwebpage’sarea,yourjobbynowisto......
  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......
  • 【队列】LeetCode 232. 用栈实现队列
    题目链接232.用栈实现队列思路设置一个主栈mainStack和一个辅助栈assistantStack,在进行入队的时候,将mainStack中的元素全部放入assistantStack中,再将x入队,然......
  • [LeetCode] 1325. Delete Leaves With a Given Value 删除给定值的叶子结点
    Givenabinarytree root andaninteger target,deleteallthe leafnodes withvalue target.Notethatonceyoudeletealeafnodewithvalue target, ......
  • 【队列】LeetCode 225. 用队列实现栈
    题目链接225.用队列实现栈思路设置一个主队列mainQueue和一个辅助队列assistantQueue,在进行压栈的时候,将mainQueue中的元素全部放入assistantQueue中,再将x压......
  • Leetcode[LeetCode]4 两个有序数组的中位数
    上图为剑指Offer之字符串的排列,基于回溯法的思想。简单算法01数组中第二大的数02合并排序链表03链表反转04判断链表环05两个链表的首个交点06数组中出现大与一般的数07手写......
  • 【链表】LeetCode 328. 奇偶链表
    题目链接328.奇偶链表思路根据题意,我们只需要将扫描到的索引为奇数的结点不断插入到正确位置。比如1->2->3->4->5==》1->3->2->4->5==》1->3->5->2->4在扫描过......
  • 阿里easyexcel解析百万级大数据量的Excel表格,看这一篇文章就够了
    ​1、应用场景1.1、实际工作中可能会遇到百万条数据量的Excel表格上传到后台进行解析。那么传统的POI,它只适用于数据量较小的Excel表格解析,当数据量到一定级别时,后台程序就......