Skip to content

feat: add rust solution to lc problem: No.0920 #1409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions solution/0900-0999/0920.Number of Music Playlists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
let mut dp: Vec<Vec<i64>> = vec![vec![0; n as usize + 1]; goal as usize + 1];

// Initialize the dp vector
dp[0][0] = 1;

// Begin the dp process
for i in 1..=goal as usize {
for j in 1..=n as usize {
// Choose the song that has not been chosen before
// We have n - (j - 1) songs to choose
dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64;

// Choose the song that has been chosen before
// We have j - k songs to choose if j > k
if j as i32 > k {
dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64;
}

// Update dp[i][j]
dp[i][j] %= (1e9 as i32 + 7) as i64;
}
}

dp[goal as usize][n as usize] as i32
}
}
```

### **Go**

```go
Expand Down
34 changes: 34 additions & 0 deletions solution/0900-0999/0920.Number of Music Playlists/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,40 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
let mut dp: Vec<Vec<i64>> = vec![vec![0; n as usize + 1]; goal as usize + 1];

// Initialize the dp vector
dp[0][0] = 1;

// Begin the dp process
for i in 1..=goal as usize {
for j in 1..=n as usize {
// Choose the song that has not been chosen before
// We have n - (j - 1) songs to choose
dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64;

// Choose the song that has been chosen before
// We have j - k songs to choose if j > k
if j as i32 > k {
dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64;
}

// Update dp[i][j]
dp[i][j] %= (1e9 as i32 + 7) as i64;
}
}

dp[goal as usize][n as usize] as i32
}
}
```

### **Go**

```go
Expand Down
29 changes: 29 additions & 0 deletions solution/0900-0999/0920.Number of Music Playlists/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
impl Solution {
#[allow(dead_code)]
pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
let mut dp: Vec<Vec<i64>> = vec![vec![0; n as usize + 1]; goal as usize + 1];

// Initialize the dp vector
dp[0][0] = 1;

// Begin the dp process
for i in 1..=goal as usize {
for j in 1..=n as usize {
// Choose the song that has not been chosen before
// We have n - (j - 1) songs to choose
dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64;

// Choose the song that has been chosen before
// We have j - k songs to choose if j > k
if j as i32 > k {
dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64;
}

// Update dp[i][j]
dp[i][j] %= (1e9 as i32 + 7) as i64;
}
}

dp[goal as usize][n as usize] as i32
}
}