Skip to content

Commit b661602

Browse files
authored
Merge pull request #2922 from thearyanahmed/441-arranging-coins
Rust-0441 | Add solution for 0441 arranging coins (rust)
2 parents c3062d0 + 87f79fd commit b661602

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rust/0441-arranging-coins.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn arrange_coins(n: i32) -> i32 {
3+
let (mut l, mut r) = (1, n);
4+
let mut res = 0;
5+
6+
while l <= r {
7+
let mid = l + (r - l) / 2;
8+
let coins = (mid as i64 * (mid as i64 + 1)) / 2;
9+
10+
if coins > n as i64 {
11+
r = mid - 1;
12+
} else {
13+
l = mid + 1;
14+
res = mid;
15+
}
16+
}
17+
18+
res as i32
19+
20+
}
21+
}

0 commit comments

Comments
 (0)