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 2238f29

Browse files
authored
feat: add rust solution to lc problem: No.2483 (doocs#1542)
1 parent d95b0f1 commit 2238f29

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

‎solution/2400-2499/2483.Minimum Penalty for a Shop/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ public:
146146
};
147147
```
148148
149+
### **Rust**
150+
151+
```rust
152+
impl Solution {
153+
#[allow(dead_code)]
154+
pub fn best_closing_time(customers: String) -> i32 {
155+
let n = customers.len();
156+
let mut penalty = i32::MAX;
157+
let mut ret = -1;
158+
let mut prefix_sum = vec![0; n + 1];
159+
160+
// Initialize the vector
161+
for (i, c) in customers.chars().enumerate() {
162+
prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 };
163+
}
164+
165+
// Calculate the answer
166+
for i in 0..=n {
167+
if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 {
168+
penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32;
169+
ret = i as i32;
170+
}
171+
}
172+
173+
ret
174+
}
175+
}
176+
```
177+
149178
### **Go**
150179

151180
```go

‎solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@ public:
128128
};
129129
```
130130
131+
### **Rust**
132+
133+
```rust
134+
impl Solution {
135+
#[allow(dead_code)]
136+
pub fn best_closing_time(customers: String) -> i32 {
137+
let n = customers.len();
138+
let mut penalty = i32::MAX;
139+
let mut ret = -1;
140+
let mut prefix_sum = vec![0; n + 1];
141+
142+
// Initialize the vector
143+
for (i, c) in customers.chars().enumerate() {
144+
prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 };
145+
}
146+
147+
// Calculate the answer
148+
for i in 0..=n {
149+
if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 {
150+
penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32;
151+
ret = i as i32;
152+
}
153+
}
154+
155+
ret
156+
}
157+
}
158+
```
159+
131160
### **Go**
132161

133162
```go
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn best_closing_time(customers: String) -> i32 {
4+
let n = customers.len();
5+
let mut penalty = i32::MAX;
6+
let mut ret = -1;
7+
let mut prefix_sum = vec![0; n + 1];
8+
9+
// Initialize the vector
10+
for (i, c) in customers.chars().enumerate() {
11+
prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 };
12+
}
13+
14+
// Calculate the answer
15+
for i in 0..=n {
16+
if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 {
17+
penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32;
18+
ret = i as i32;
19+
}
20+
}
21+
22+
ret
23+
}
24+
}

0 commit comments

Comments
(0)

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