Skip to content

Commit 10cb4ca

Browse files
committed
1/16/2020
1 parent af048b1 commit 10cb4ca

7 files changed

+206
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
### leetcode practise solutions in RUST
33

44
### current status
5-
167/1552 completed
5+
169/1552 completed
66

77
### Finished problems:
88
|id|Solution|
@@ -138,6 +138,7 @@
138138
|1013|[Partition Array Into Three Parts With Equal Sum](src/_1013_partition_array_into_three_parts_with_equal_sum.rs)|
139139
|1021|[Remove Outermost Parentheses](src/_1021_remove_outermost_parentheses_1029_two_city_scheduling.rs)|
140140
|1029|[Two City Scheduling](src/_1029_two_city_scheduling.rs)|
141+
|1136|[Parallel Courses](src/_1136_parallel_courses.rs)|
141142
|1239|[Maximum Length of a Concatenated String with Unique Characters ](src/_1239_maximum_length_of_a_concatenated_string_with_unique_characters.rs)|
142143
|1249|[Minimum Remove to Make Valid Parentheses](src/_1249_minimum_remove_to_make_valid_parentheses.rs)|
143144
|1281|[Subtract the Product and Sum of Digits of an Integer](src/_1281_subtract_the_product_and_sum_of_digits_of_an_integer.rs)|
@@ -178,4 +179,5 @@
178179
|1704|[Determine if String Halves Are Alike](src/_1704_determine_if_string_halves_are_alike.rs)|
179180
|1705|[Maximum Number of Eaten Apples ](src/_1705_maximum_number_of_eaten_apples.rs)|
180181
|1706|[Where Will the Ball Fall](src/_1706_where_will_the_ball_fall.rs)|
181-
||[](src/.rs)|
182+
|1725|[Number Of Rectangles That Can Form The Largest Square](src/_1725_number_of_rectangles_that_can_form_the_largest_square.rs)|
183+
|1726|[Tuple with Same Product](src/_1726_tuple_with_same_product.rs)|

src/_0009_palindrome_number.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ impl Solution {
55
if x < 0 {
66
return false;
77
}
8-
let mut digits = Vec::new();
8+
let mut reversed = 0;
99
let mut number = x;
1010
while number > 0 {
11-
digits.push(number % 10);
11+
reversed = reversed * 10 + number % 10;
1212
number /= 10;
1313
}
14-
digits == digits.iter().copied().rev().collect::<Vec<i32>>()
14+
x == reversed
1515
}
1616
}
1717

src/_1136_parallel_courses.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::collections::{HashMap, VecDeque};
2+
3+
struct Solution;
4+
impl Solution {
5+
pub fn minimum_semesters(n: i32, relations: Vec<Vec<i32>>) -> i32 {
6+
let mut adjacents: HashMap<i32, Vec<i32>> = HashMap::new();
7+
let mut indegrees: Vec<i32> = vec![0; n as usize + 1];
8+
let mut queue: VecDeque<i32> = VecDeque::new();
9+
let mut answer = 0;
10+
11+
for v in relations {
12+
adjacents.entry(v[0]).or_default().push(v[1]);
13+
indegrees[v[1] as usize] += 1;
14+
}
15+
16+
for i in 1..=n as usize {
17+
if indegrees[i] == 0 {
18+
queue.push_back(i as i32);
19+
}
20+
}
21+
22+
while !queue.is_empty() {
23+
answer += 1;
24+
let m = queue.len();
25+
for _ in 0..m {
26+
let u = queue.pop_front().unwrap();
27+
if let Some(adjs) = adjacents.get(&u) {
28+
for &w in adjs.iter() {
29+
indegrees[w as usize] -= 1;
30+
if indegrees[w as usize] == 0 {
31+
queue.push_back(w);
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
for v in indegrees {
39+
if v > 0 {
40+
return -1;
41+
}
42+
}
43+
answer
44+
}
45+
}
46+
47+
#[test]
48+
fn test() {
49+
assert_eq!(
50+
Solution::minimum_semesters(3, vec_vec_i32![[1, 3], [2, 3]]),
51+
2
52+
);
53+
assert_eq!(
54+
Solution::minimum_semesters(3, vec_vec_i32![[1, 2], [2, 3], [3, 1]]),
55+
-1
56+
);
57+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn count_good_rectangles(rectangles: Vec<Vec<i32>>) -> i32 {
5+
let mut max_square_side = 0;
6+
let mut max_square_count = 0;
7+
8+
for v in rectangles {
9+
let side = v[0].min(v[1]);
10+
if side > max_square_side {
11+
max_square_side = side;
12+
max_square_count = 1;
13+
} else if side == max_square_side {
14+
max_square_count += 1;
15+
}
16+
}
17+
18+
max_square_count
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
assert_eq!(
25+
Solution::count_good_rectangles(vec_vec_i32![[5, 8], [3, 9], [5, 12], [16, 5]]),
26+
3
27+
);
28+
assert_eq!(
29+
Solution::count_good_rectangles(vec_vec_i32![[2, 3], [3, 7], [4, 3], [3, 7]]),
30+
3
31+
);
32+
}

src/_1726_tuple_with_same_product.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::HashMap;
2+
struct Solution;
3+
impl Solution {
4+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
5+
let n = nums.len();
6+
let mut result = 0;
7+
let mut products: HashMap<i32, i32> = HashMap::new();
8+
for i in 0..n - 1 {
9+
for j in i + 1..n {
10+
*products.entry(nums[i] * nums[j]).or_default() += 1;
11+
}
12+
}
13+
14+
for (_, v) in products {
15+
result += v * (v - 1) / 2;
16+
}
17+
result * 8
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
assert_eq!(Solution::tuple_same_product(vec![2, 3, 4, 6]), 8);
24+
assert_eq!(Solution::tuple_same_product(vec![1, 2, 4, 5, 10]), 16);
25+
assert_eq!(Solution::tuple_same_product(vec![2, 3, 4, 6, 8, 12]), 40);
26+
assert_eq!(Solution::tuple_same_product(vec![2, 3, 5, 7]), 0);
27+
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ mod _1021_remove_outermost_parentheses;
276276
//
277277
mod _1029_two_city_scheduling;
278278
//
279+
mod _1136_parallel_courses;
280+
//
279281
mod _1152_analyze_user_website_visit_pattern;
280282
//
281283
mod _1239_maximum_length_of_a_concatenated_string_with_unique_characters;
@@ -358,4 +360,9 @@ mod _1705_maximum_number_of_eaten_apples;
358360
//
359361
mod _1706_where_will_the_ball_fall;
360362
//
363+
mod _1725_number_of_rectangles_that_can_form_the_largest_square;
364+
//
365+
mod _1726_tuple_with_same_product;
366+
//
361367
mod test;
368+
//

src/test.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
1+
struct Solution;
12

3+
impl Solution {
4+
pub fn count_good_rectangles(rectangles: Vec<Vec<i32>>) -> i32 {
5+
let mut max_square_side = 0;
6+
let mut max_square_count = 0;
7+
8+
for v in rectangles {
9+
let side = v[0].min(v[1]);
10+
if side > max_square_side {
11+
max_square_side = side;
12+
max_square_count = 1;
13+
} else if side == max_square_side {
14+
max_square_count += 1;
15+
}
16+
}
17+
18+
max_square_count
19+
}
20+
}
21+
22+
use std::collections::HashMap;
23+
impl Solution {
24+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
25+
let n = nums.len();
26+
let mut result = 0;
27+
let mut products: HashMap<i32, i32> = HashMap::new();
28+
for i in 0..n - 1 {
29+
for j in i + 1..n {
30+
*products.entry(nums[i] * nums[j]).or_default() += 1;
31+
}
32+
}
33+
34+
for (_, v) in products {
35+
result += v * (v - 1) / 2;
36+
}
37+
result * 8
38+
}
39+
}
40+
41+
impl Solution {
42+
pub fn largest_submatrix(matrix: Vec<Vec<i32>>) -> i32 {
43+
let mut result: i32 = 0;
44+
let n = matrix.len();
45+
let m = matrix[0].len();
46+
if n == 1 {
47+
return matrix[0].clone().into_iter().sum();
48+
}
49+
for j in 0..m {
50+
let mut ix = 0;
51+
let mut iy = 0;
52+
while iy < n {
53+
if matrix[iy][j] == 0 {
54+
ix = iy + 1;
55+
} else {
56+
let mut right_count = 0;
57+
for j_test in j..m {
58+
let mut all_one = true;
59+
for i_test in ix..=iy {
60+
if matrix[i_test][j_test] == 0 {
61+
all_one = false;
62+
break;
63+
}
64+
}
65+
if all_one {
66+
right_count += 1;
67+
}
68+
}
69+
println!("{:?},{:?},{:?}", ix, iy, right_count);
70+
result = result.max((iy - ix + 1) as i32 * right_count);
71+
}
72+
iy += 1;
73+
}
74+
}
75+
result
76+
}
77+
}

0 commit comments

Comments
 (0)