Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a3db10

Browse files
solve: 279. Perfect Squares in rustlang
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent 0f14b52 commit 2a3db10

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::cmp::min;
2+
3+
impl Solution {
4+
pub fn num_squares(n: i32) -> i32 {
5+
// make a dp array of size n + 1 and filled with 0
6+
let mut dp = vec![n; n as usize + 1];
7+
8+
// set the first element to 0
9+
dp[0] = 0;
10+
11+
// iterate from 1 to n
12+
for i in 1..=n as usize {
13+
// iterate from 1 to sqrt(i)
14+
for j in 1..=(i as f64).sqrt() as usize {
15+
// set dp[i] to the minimum of dp[i] and dp[i - j * j] + 1
16+
dp[i] = min(dp[i], dp[i - j * j] + 1);
17+
}
18+
}
19+
20+
dp[n as usize]
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /