Skip to content

feat: add solutions to lc problems: No.2798~2800 #1513

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 1 commit into from
Aug 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ function numberOfEmployeesWhoMetTarget(
}
```

### **Rust**

```rust
impl Solution {
pub fn number_of_employees_who_met_target(hours: Vec<i32>, target: i32) -> i32 {
let mut ans = 0;
for &v in hours.iter() {
if v >= target {
ans += 1;
}
}
ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ function numberOfEmployeesWhoMetTarget(
}
```

### **Rust**

```rust
impl Solution {
pub fn number_of_employees_who_met_target(hours: Vec<i32>, target: i32) -> i32 {
let mut ans = 0;
for &v in hours.iter() {
if v >= target {
ans += 1;
}
}
ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn number_of_employees_who_met_target(hours: Vec<i32>, target: i32) -> i32 {
let mut ans = 0;
for &v in hours.iter() {
if v >= target {
ans += 1;
}
}
ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,58 @@ function countCompleteSubarrays(nums: number[]): number {
}
```

### **Rust**

```rust
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let mut set: HashSet<&i32> = nums.iter().collect();
let n = nums.len();
let m = set.len();
let mut ans = 0;
for i in 0..n {
set.clear();
for j in i..n {
set.insert(&nums[j]);
if set.len() == m {
ans += n - j;
break;
}
}
}
ans as i32
}
}
```

```rust
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let m = nums.iter().collect::<HashSet<&i32>>().len();
let mut map = HashMap::new();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
*map.entry(nums[j]).or_insert(0) += 1;
while map.len() == m {
ans += n - j;
let v = map.entry(nums[i]).or_default();
*v -= 1;
if *v == 0 {
map.remove(&nums[i]);
}
i += 1;
}
}
ans as i32
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,58 @@ function countCompleteSubarrays(nums: number[]): number {
}
```

### **Rust**

```rust
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let mut set: HashSet<&i32> = nums.iter().collect();
let n = nums.len();
let m = set.len();
let mut ans = 0;
for i in 0..n {
set.clear();
for j in i..n {
set.insert(&nums[j]);
if set.len() == m {
ans += n - j;
break;
}
}
}
ans as i32
}
}
```

```rust
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let m = nums.iter().collect::<HashSet<&i32>>().len();
let mut map = HashMap::new();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
*map.entry(nums[j]).or_insert(0) += 1;
while map.len() == m {
ans += n - j;
let v = map.entry(nums[i]).or_default();
*v -= 1;
if *v == 0 {
map.remove(&nums[i]);
}
i += 1;
}
}
ans as i32
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let m = nums.iter().collect::<HashSet<&i32>>().len();
let mut map = HashMap::new();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
*map.entry(nums[j]).or_insert(0) += 1;
while map.len() == m {
ans += n - j;
let v = map.entry(nums[i]).or_default();
*v -= 1;
if *v == 0 {
map.remove(&nums[i]);
}
i += 1;
}
}
ans as i32
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,93 @@ func min(a, b int) int {
}
```

### **TypeScript**

```ts
function minimumString(a: string, b: string, c: string): string {
const f = (s: string, t: string): string => {
if (s.includes(t)) {
return s;
}
if (t.includes(s)) {
return t;
}
const m = s.length;
const n = t.length;
for (let i = Math.min(m, n); i > 0; --i) {
if (s.slice(-i) === t.slice(0, i)) {
return s + t.slice(i);
}
}
return s + t;
};
const s: string[] = [a, b, c];
const perm: number[][] = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let ans = '';
for (const [i, j, k] of perm) {
const t = f(f(s[i], s[j]), s[k]);
if (
ans === '' ||
t.length < ans.length ||
(t.length === ans.length && t < ans)
) {
ans = t;
}
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
fn f(s1: String, s2: String) -> String {
if s1.contains(&s2) {
return s1;
}
if s2.contains(&s1) {
return s2;
}
for i in 0..s1.len() {
let s = &s1[i..];
if s2.starts_with(s) {
let n = s.len();
return s1 + &s2[n..];
}
}
s1 + s2.as_str()
}

pub fn minimum_string(a: String, b: String, c: String) -> String {
let s = [&a, &b, &c];
let perm = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let mut ans = String::new();
for [i, j, k] in perm.iter() {
let r = Self::f(Self::f(s[*i].clone(), s[*j].clone()), s[*k].clone());
if ans == "" || r.len() < ans.len() || (r.len() == ans.len() && r < ans) {
ans = r;
}
}
ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,93 @@ func min(a, b int) int {
}
```

### **TypeScript**

```ts
function minimumString(a: string, b: string, c: string): string {
const f = (s: string, t: string): string => {
if (s.includes(t)) {
return s;
}
if (t.includes(s)) {
return t;
}
const m = s.length;
const n = t.length;
for (let i = Math.min(m, n); i > 0; --i) {
if (s.slice(-i) === t.slice(0, i)) {
return s + t.slice(i);
}
}
return s + t;
};
const s: string[] = [a, b, c];
const perm: number[][] = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let ans = '';
for (const [i, j, k] of perm) {
const t = f(f(s[i], s[j]), s[k]);
if (
ans === '' ||
t.length < ans.length ||
(t.length === ans.length && t < ans)
) {
ans = t;
}
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
fn f(s1: String, s2: String) -> String {
if s1.contains(&s2) {
return s1;
}
if s2.contains(&s1) {
return s2;
}
for i in 0..s1.len() {
let s = &s1[i..];
if s2.starts_with(s) {
let n = s.len();
return s1 + &s2[n..];
}
}
s1 + s2.as_str()
}

pub fn minimum_string(a: String, b: String, c: String) -> String {
let s = [&a, &b, &c];
let perm = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let mut ans = String::new();
for [i, j, k] in perm.iter() {
let r = Self::f(Self::f(s[*i].clone(), s[*j].clone()), s[*k].clone());
if ans == "" || r.len() < ans.len() || (r.len() == ans.len() && r < ans) {
ans = r;
}
}
ans
}
}
```

### **...**

```
Expand Down
Loading