• [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 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.

    思路:

    解法1:设一个变量 cur表示当前行需要的硬币数,  初始值为1,从n中减去cur,cur自加1,如果此时剩余的硬币小于cur,返回cur-1即可

    解法2:二分搜索法Binary Search,等差数列前m项和: m * (m + 1) / 2, 搜索前i行之和刚好大于n的临界点,这样我们减一个就是能排满的行数

    解法3:利用等差数列的公式n = (1 + x) * x / 2,  则x = (-1 + sqrt(8 * n + 1)) / 2,  取整后就是能填满的行数

     C++: Time: O(n), Space: O(1)

    class Solution {
    public:
        int arrangeCoins(int n) {
            int cur = 1, rem = n - 1;
            while (rem >= cur + 1) {
                ++cur;
                rem -= cur;
            }
            return n == 0 ? 0 : cur;
        }
    };
    

    C++: Time: O(logn), Space: O(1)

    class Solution {
    public:
        int arrangeCoins(int n) {
            if (n <= 1) return n;
            long low = 1, high = n;
            while (low < high) {
                long mid = low + (high - low) / 2;
                if (mid * (mid + 1) / 2 <= n) low = mid + 1;
                else high = mid;
            }
            return low - 1;
        }
    };
    

    C++: Time: O(logn), Space: O(1) 

    class Solution {
    public:
        int arrangeCoins(int n) {
            return (int)((-1 + sqrt(1 + 8 * (long)n)) / 2); # sqrt is O(logn) time.
    } };

    Python: O(n), Space: O(1) 

    class Solution(object):
        def arrangeCoins(self, n):
            cur = 1
            rem = n
            while rem >= cur:
                rem -= cur
                cur += 1
                
            return 0 if n == 0 else cur - 1
    

    Python: O(logn), Space: O(1)

    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            left, right = 1, n
            while left <= right:
                mid = left + (right - left) / 2
                if 2 * n < mid * (mid+1):
                    right = mid - 1
                else:
                    left = mid + 1
            return left - 1
    

    Python: O(logn), Space: O(1)

    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            l, r = 0, n
            while l <= r:
                mid = (l + r) / 2
                if mid * (mid + 1) / 2 > n:
                    r = mid - 1
                else:
                    l = mid + 1
            return r
    

    Python: O(logn), Space: O(1)

    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            return int((math.sqrt(8*n+1)-1) / 2)  # sqrt is O(logn) time.
    

      

      

      

      

      

  • 相关阅读:
    java代码中存在的Big Endian 和 Little Endian
    通过hbase实现日志的转存(MR AnalyserLogDataRunner和AnalyserLogDataMapper)
    hbase使用MapReduce操作1(基本增删改查)
    配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)
    数据库分页查询
    将博客搬至CSDN
    Dev Winform本地化
    C#,OleDbType,Access 对应数据类型
    C#字符串转义和反转义
    C# 正则表达式
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8483034.html
Copyright © 2020-2023  润新知