Skip to content

Commit 2e7d632

Browse files
committedMar 6, 2022
feat: add solutions to lc problem: No.2100
No.2100.Find Good Days to Rob the Bank
1 parent 1c1bff4 commit 2e7d632

File tree

4 files changed

+170
-2
lines changed

4 files changed

+170
-2
lines changed
 

‎solution/2100-2199/2100.Find Good Days to Rob the Bank/README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,66 @@ func goodDaysToRobBank(security []int, time int) []int {
189189

190190
### **TypeScript**
191191

192-
<!-- 这里可写当前语言的特殊实现逻辑 -->
193-
194192
```ts
193+
function goodDaysToRobBank(security: number[], time: number): number[] {
194+
const n = security.length;
195+
if (n <= time * 2) {
196+
return [];
197+
}
198+
const l = new Array(n).fill(0);
199+
const r = new Array(n).fill(0);
200+
for (let i = 1; i < n; i++) {
201+
if (security[i] <= security[i - 1]) {
202+
l[i] = l[i - 1] + 1;
203+
}
204+
if (security[n - i - 1] <= security[n - i]) {
205+
r[n - i - 1] = r[n - i] + 1;
206+
}
207+
}
208+
const res = [];
209+
for (let i = time; i < n - time; i++) {
210+
if (time <= Math.min(l[i], r[i])) {
211+
res.push(i);
212+
}
213+
}
214+
return res;
215+
}
216+
```
195217

218+
### **Rust**
219+
220+
```rust
221+
use std::cmp::Ordering;
222+
223+
impl Solution {
224+
pub fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> {
225+
let time = time as usize;
226+
let n = security.len();
227+
if time * 2 >= n {
228+
return vec![];
229+
}
230+
let mut g = vec![0; n];
231+
for i in 1..n {
232+
g[i] = match security[i].cmp(&security[i - 1]) {
233+
Ordering::Less => -1,
234+
Ordering::Greater => 1,
235+
Ordering::Equal => 0,
236+
}
237+
}
238+
let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]);
239+
for i in 1..=n {
240+
a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 };
241+
b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 };
242+
}
243+
let mut res = vec![];
244+
for i in time..n - time {
245+
if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 {
246+
res.push((i) as i32);
247+
}
248+
}
249+
res
250+
}
251+
}
196252
```
197253

198254
### **...**

‎solution/2100-2199/2100.Find Good Days to Rob the Bank/README_EN.md

+58
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,65 @@ func goodDaysToRobBank(security []int, time int) []int {
178178
### **TypeScript**
179179

180180
```ts
181+
function goodDaysToRobBank(security: number[], time: number): number[] {
182+
const n = security.length;
183+
if (n <= time * 2) {
184+
return [];
185+
}
186+
const l = new Array(n).fill(0);
187+
const r = new Array(n).fill(0);
188+
for (let i = 1; i < n; i++) {
189+
if (security[i] <= security[i - 1]) {
190+
l[i] = l[i - 1] + 1;
191+
}
192+
if (security[n - i - 1] <= security[n - i]) {
193+
r[n - i - 1] = r[n - i] + 1;
194+
}
195+
}
196+
const res = [];
197+
for (let i = time; i < n - time; i++) {
198+
if (time <= Math.min(l[i], r[i])) {
199+
res.push(i);
200+
}
201+
}
202+
return res;
203+
}
204+
```
205+
206+
### **Rust**
181207

208+
```rust
209+
use std::cmp::Ordering;
210+
211+
impl Solution {
212+
pub fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> {
213+
let time = time as usize;
214+
let n = security.len();
215+
if time * 2 >= n {
216+
return vec![];
217+
}
218+
let mut g = vec![0; n];
219+
for i in 1..n {
220+
g[i] = match security[i].cmp(&security[i - 1]) {
221+
Ordering::Less => -1,
222+
Ordering::Greater => 1,
223+
Ordering::Equal => 0,
224+
}
225+
}
226+
let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]);
227+
for i in 1..=n {
228+
a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 };
229+
b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 };
230+
}
231+
let mut res = vec![];
232+
for i in time..n - time {
233+
if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 {
234+
res.push((i) as i32);
235+
}
236+
}
237+
res
238+
}
239+
}
182240
```
183241

184242
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::cmp::Ordering;
2+
3+
impl Solution {
4+
pub fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> {
5+
let time = time as usize;
6+
let n = security.len();
7+
if time * 2 >= n {
8+
return vec![];
9+
}
10+
let mut g = vec![0; n];
11+
for i in 1..n {
12+
g[i] = match security[i].cmp(&security[i - 1]) {
13+
Ordering::Less => -1,
14+
Ordering::Greater => 1,
15+
Ordering::Equal => 0,
16+
}
17+
}
18+
let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]);
19+
for i in 1..=n {
20+
a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 };
21+
b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 };
22+
}
23+
let mut res = vec![];
24+
for i in time..n - time {
25+
if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 {
26+
res.push((i) as i32);
27+
}
28+
}
29+
res
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function goodDaysToRobBank(security: number[], time: number): number[] {
2+
const n = security.length;
3+
if (n <= time * 2) {
4+
return [];
5+
}
6+
const l = new Array(n).fill(0);
7+
const r = new Array(n).fill(0);
8+
for (let i = 1; i < n; i++) {
9+
if (security[i] <= security[i - 1]) {
10+
l[i] = l[i - 1] + 1;
11+
}
12+
if (security[n - i - 1] <= security[n - i]) {
13+
r[n - i - 1] = r[n - i] + 1;
14+
}
15+
}
16+
const res = [];
17+
for (let i = time; i < n - time; i++) {
18+
if (time <= Math.min(l[i], r[i])) {
19+
res.push(i);
20+
}
21+
}
22+
return res;
23+
}

0 commit comments

Comments
 (0)