• [LeetCode] 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 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

    排列硬币。

    你总共有 n 枚硬币,并计划将它们按阶梯状排列。对于一个由 k 行组成的阶梯,其第 i 行必须正好有 i 枚硬币。阶梯的最后一行 可能 是不完整的。

    给你一个数字 n ,计算并返回可形成 完整阶梯行 的总行数。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/arranging-coins
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    我给出一个网友的思路

    时间O(1)

    空间O(1)

    Java实现

    1 class Solution {
    2     public int arrangeCoins(int n) {
    3         return (int) (Math.sqrt((double) 2 * n + 0.25) - 0.5);
    4     }
    5 }

    JavaScript实现

    1 /**
    2  * @param {number} n
    3  * @return {number}
    4  */
    5 var arrangeCoins = function (n) {
    6     return Math.floor(Math.sqrt(2 * n + 1 / 4) - 1 / 2);
    7 };

    LeetCode 题目总结

  • 相关阅读:
    elementui form 相关表单验证
    函数注释备注
    elementui elform 某一值为数组,各项必填验证
    selenium等待的三种方式(详细)
    什么是python
    pytest基础
    pymysql数据库操作
    自动化测试1
    allure生成测试报告
    python读取excel的文件
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13222616.html
Copyright © 2020-2023  润新知