-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL441.java
More file actions
40 lines (33 loc) · 979 Bytes
/
L441.java
File metadata and controls
40 lines (33 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.liang.leetcode.binarySearch;
/**
* @ClassName: L441
* @Description: 排列硬币
* @Author: LiaNg
* @Date: 2020/3/23 10:16
*/
public class L441 {
public static void main(String[] args) {
L441 l441 = new L441();
int n = 1804289383;
System.out.println("l441.arrangeCoins(n) = " + l441.arrangeCoins(n));
}
/**
* 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
* 给定一个数字 n,找出可形成完整阶梯行的总行数。
* n 是一个非负整数,并且在 32 位有符号整型的范围内。
*/
public int arrangeCoins(int n) {
long l = 0;
long r = n;
long m;
while (l <= r) {
m = l + (r - l) / 2;
if (m * (m + 1) / 2 <= n) {
l = m + 1;
} else {
r = m - 1;
}
}
return (int) l - 1;
}
}