leetcode441(巧妙利用取整和解方程)
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.
首先理解题目的意思
给出一个数N,然后利用这个数去排列。
每行尽可能排满,第一行1个,第二行2个。。。。
返回排满的行数
首先列出看的见的等式。
(1+x)*x/2 + r = n;
其中x为最后有多少行,r为最后返回的结果,n为给出的数据。
//然后我们就把这个问题转换成了一个数学问题
也就是我们要计算出一个x要满足等式成立的情况下r最小。
如何找到这个x呢?
1、首先最简单的解法,枚举所有的x,直到r为负数为止,前一个x就是满足条件的。
所以最后的复杂度肯定是x+1,x为最终成立的行数,x为需要返回的结果。
还有更快的方法吗?
我的想法是,当r为0时,x可以从sqrt(n*2)向下取整,然后从这个数,按照方法1往上找。这个是n最大的情况
当r为x-1时,也就是n最小的情况。按照上面的方法也没错,因为这个数小,所以往上找也是可以的。
public class Solution {
public int arrangeCoins(int n) {
int x = (int) Math.floor(Math.sqrt(n*1.0));
int r = 0;
while (true){
if(x%2 == 0)
r = x / 2 * (1+x);
else
r = (1+x) / 2 * x;
if(n - r < 0)
break;
else
x++;
}
return --x;
}
}
其中要注意的是x的值过大时先要计算除法不然两个数相乘超过int就会出现一个极小数会影响判断。
当然还有更牛逼的方法
先不管r是多少,当做r是0,先利用解方程的方法n计算出来一个x
然后对这个x往小了取整数就可以了。
public class Solution {
public int arrangeCoins(int n) {
/* 数学推导
(1+k)*k/2 = n
k+k*k = 2*n
k*k + k + 0.25 = 2*n + 0.25
(k + 0.5) ^ 2 = 2*n +0.25
k + 0.5 = sqrt(2*n + 0.25)
k = sqrt(2*n + 0.25) - 0.5
*/
return (int) (Math.sqrt(2*(long)n+0.25) - 0.5);
}
}
leetcode441(巧妙利用取整和解方程)的更多相关文章
- Javascript-数值运算 保留小数点位数,并对最后一位小数各种取整方法
今天遇到Javascript数值运算的坑,说到底,还是用得少啊.得多用多敲代码多遇坑. 先介绍以下三个Javascript number取整运算方法. Math.floor() 对一个数退一取整 例: ...
- 一类巧妙利用利用失配树的序列DP
I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...
- scrapy进阶(CrawlSpider爬虫__爬取整站小说)
# -*- coding: utf-8 -*- import scrapy,re from scrapy.linkextractors import LinkExtractor from scrapy ...
- js 为何范围内随机取整要用floor,而不是ceil或者round呢
壹 ❀ 引 我在如何使用js取任意范围内随机整数这篇博客中,列举并分析了取[n,m)与[n,m]范围内整数的通用方法,并在文章结果留了一个疑问:为什么通用方法中取整操作,我们使用Math.floor ...
- JS基础入门篇(三)— for循环,取余,取整。
1.for循环 1.for的基本简介 作用: 根据一定的条件,重复地执行一行或多行代码 语法: for( 初始化 ; 判断条件 ; 条件改变 ){ 代码块 } 2.for循环的执行顺序 <bod ...
- 巧妙利用Camtasia制作网课
随着互联网的快速发展,网络学习变得非常流行.这种躺在床上就可以获取知识的方法让大家渐渐地都喜欢上了学习,那么我们是否想要了解一下网课的幕后制作呢. 今天我给大家带来的便是巧妙利用Camtasia进行网 ...
- Python技法:浮点数取整、格式化和NaN处理
1. 取整的三种方法 1.1 强转int类型 这种方法会直接对浮点数的小数部分进行截断(无论是正还是负). print(int(2.7)) # 2 print(int(-2.7)) # -2 1.2 ...
- ios小数向上、下取整,计算结果向上、下取整
[摘要:小数背上与整,指小数局部间接进1 x=3.14, ceilf (x)=4 小数背下与整,指间接往失落小数局部 x=3.14,floor(x)=3 盘算效果背上与整 A被除数,B除数 ,(AB- ...
- Java Math 取整的方式
1.Math.floor floor,英文原意:地板. Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的值肯定会小于或等于这个浮点数. 2.Math.ceil c ...
随机推荐
- js 的四种设计模式的优缺点
原始模式: var Car = new Object; Car.color = "blue"; Car.door = 4; Car.showColor = function() { ...
- autoconfig操作小结
1.添加maven配置在pom文件中 <profile> <id>prod</id> <properties> < ...
- android 操蛋的gradle
首先看语法: -include {filename} 从给定的文件中读取配置参数 -basedirectory {directoryname} 指定基础目录为以后相对的档案名称 -injars {cl ...
- CocoaPods 报错 [!] Error installing JSONModel
pod install p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #34bd26 } span.s1 { } ...
- URAL 1525 Path
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> us ...
- Java Swing 日期控件(转载)
http://www.cnblogs.com/lzy1991/p/5714935.html
- 字符串匹配—KMP 扩展KMP Manacher
kuangbin字符串专题传送门--http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview 算法模板: KMP: ; ...
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- Java 序列化 transient关键字
Java 序列化 transient关键字 @author 敏敏Alexia 转自:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html 1. tra ...
- vs2013+opencv2.4.11+Qt5.5.1配置
注意本教程配置环境:win7 32位 如果只配置vs2013+opencv2.4.11,参考http://jingyan.baidu.com/article/ff411625b1311a12e4823 ...