https://leetcode.com/problems/arranging-coins/

public class Solution {
    public int arrangeCoins(int n) {
        // n >= x*(x+1)/2; 2n >= x^2 + x; 8n+1 >= 4x^2 + 4x + 1 = (2x+1)^2
        // (8n+1)^(1/2) = 2x+1; x = ((8n+1)^(1/2)-1)/2;
        // 注意下面的n要转成long,不然可能溢出
        return (int)(Math.sqrt(8*(long)n+1)-1)/2;
    }
}

 

相关文章:

  • 2022-01-25
  • 2019-08-17
  • 2021-05-24
  • 2021-12-17
  • 2021-10-05
  • 2021-08-09
  • 2021-06-07
  • 2021-10-03
猜你喜欢
  • 2021-06-21
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2021-04-28
相关资源
相似解决方案