Skip to content

Commit cb4caf3

Browse files
committed
feat: add more
1 parent b41588d commit cb4caf3

10 files changed

+94
-27
lines changed

Array/_1603_design_parking_system.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,24 @@ struct ParkingSystem {
3737
}
3838

3939
impl ParkingSystem {
40-
4140
fn new(big: i32, medium: i32, small: i32) -> Self {
42-
ParkingSystem {
43-
big,
44-
medium,
45-
small,
46-
}
41+
ParkingSystem { big, medium, small }
4742
}
4843

4944
fn add_car(&mut self, car_type: i32) -> bool {
5045
match car_type {
5146
1 => {
5247
self.big = self.big - 1;
5348
self.big >= 0
54-
},
49+
}
5550
2 => {
56-
self.medium = self.medium -1;
51+
self.medium = self.medium - 1;
5752
self.medium >= 0
58-
},
53+
}
5954
3 => {
6055
self.small = self.small - 1;
6156
self.small >= 0
62-
},
57+
}
6358
_ => false,
6459
}
6560
}

Array/_2303_calculate_amount_paid_in_taxes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,8 @@ pub fn calculate_tax(brackets: Vec<Vec<i32>>, income: i32) -> f64 {
7070

7171
#[test]
7272
pub fn t1() {
73-
assert_eq!(calculate_tax(vec![vec![3, 50], vec![7, 10], vec![12, 25]], 10), 2.65);
73+
assert_eq!(
74+
calculate_tax(vec![vec![3, 50], vec![7, 10], vec![12, 25]], 10),
75+
2.65
76+
);
7477
}

Array/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mod _1550_three_consecutive_odds;
105105
mod _1572_matrix_diagonal_sum;
106106
mod _1582_special_positions_in_a_binary_matrix;
107107
mod _1588_sum_of_all_odd_length_subarrays;
108+
mod _1603_design_parking_system;
108109
mod _1619_mean_of_array_after_removing_some_elements;
109110
mod _1629_slowest_key;
110111
mod _1636_sort_array_by_increasing_frequency;
@@ -163,7 +164,6 @@ mod _2248_intersection_of_multiple_arrays;
163164
mod _2255_count_prefixes_of_a_given_string;
164165
mod _2273_find_resultant_array_after_removing_anagrams;
165166
mod _2293_min_max_game;
167+
mod _2303_calculate_amount_paid_in_taxes;
166168
mod _2319_check_if_matrix_is_x_matrix;
167169
mod _2335_minimum_amount_of_time_to_fill_cups;
168-
mod _2303_calculate_amount_paid_in_taxes;
169-
mod _1603_design_parking_system;

String/_1678_goal_parser_interpretation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// * `command` consists of `"G"`, `"()"`, and/or `"(al)"` in some order.
3737

3838
pub fn interpret(command: String) -> String {
39-
return str::replace(&str::replace(&command, "()", "o"), "(al)", "al").to_string()
39+
return str::replace(&str::replace(&command, "()", "o"), "(al)", "al").to_string();
4040
}
4141

4242
#[test]

String/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mod _0771_jewels_and_stones;
44
mod _1119_remove_vowels_from_a_string;
5+
mod _1678_goal_parser_interpretation;
56
mod _1859_sorting_the_sentence;
67
mod _2042_check_if_numbers_are_ascending_in_a_sentence;
78
mod _2325_decode_the_message;
8-
mod _1678_goal_parser_interpretation;

Tree/_2236_root_equals_sum_of_children.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
// * The tree consists only of the root, its left child, and its right child.
3030
// * `-100 <= Node.val <= 100`
3131

32+
use crate::base_tree::TreeNode;
3233
use std::cell::RefCell;
3334
use std::rc::Rc;
34-
use crate::base_tree::TreeNode;
3535

3636
pub fn check_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
3737
let root_node = root.as_ref().unwrap().borrow();

Tree/base_tree.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use std::rc::Rc;
55

66
#[derive(Debug, PartialEq, Eq)]
77
pub struct TreeNode {
8-
pub val: i32,
9-
pub left: Option<Rc<RefCell<TreeNode>>>,
10-
pub right: Option<Rc<RefCell<TreeNode>>>,
8+
pub val: i32,
9+
pub left: Option<Rc<RefCell<TreeNode>>>,
10+
pub right: Option<Rc<RefCell<TreeNode>>>,
1111
}
1212

1313
impl TreeNode {
14-
#[inline]
15-
pub fn new(val: i32) -> Self {
16-
TreeNode {
17-
val,
18-
left: None,
19-
right: None
14+
#[inline]
15+
pub fn new(val: i32) -> Self {
16+
TreeNode {
17+
val,
18+
left: None,
19+
right: None,
20+
}
2021
}
21-
}
2222
}

binary_search/_0704_binary_search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ pub fn search(nums: Vec<i32>, target: i32) -> i32 {
4545

4646
#[test]
4747
pub fn t1() {
48-
assert_eq!(search(vec![-1,0,3,5,9,12], 9), 4);
48+
assert_eq!(search(vec![-1, 0, 3, 5, 9, 12], 9), 4);
4949
}

stack/_0739_daily_temperatures.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// https://leetcode.com/problems/daily-temperatures
2+
//
3+
// Given an array of integers `temperatures` represents the daily temperatures, return _an array_ `answer` _such that_ `answer[i]` _is the number of days you have to wait after the_ `i<sup>th</sup>` _day to get a warmer temperature_. If there is no future day for which this is possible, keep `answer[i] == 0` instead.
4+
//
5+
// **Example 1:**
6+
//
7+
// ```
8+
// **Input:** temperatures = [73,74,75,71,69,72,76,73]
9+
// **Output:** [1,1,4,2,1,1,0,0]
10+
// ```
11+
//
12+
// **Example 2:**
13+
//
14+
// ```
15+
// **Input:** temperatures = [30,40,50,60]
16+
// **Output:** [1,1,1,0]
17+
// ```
18+
//
19+
// **Example 3:**
20+
//
21+
// ```
22+
// **Input:** temperatures = [30,60,90]
23+
// **Output:** [1,1,0]
24+
// ```
25+
//
26+
// **Constraints:**
27+
//
28+
// * `1 <= temperatures.length <= 10<sup>5</sup>`
29+
// * `30 <= temperatures[i] <= 100`
30+
31+
pub fn daily_temperatures_monotonic_stack(temperatures: Vec<i32>) -> Vec<i32> {
32+
let mut res = vec![0; temperatures.len()];
33+
let mut stack = Vec::new();
34+
for i in 0..temperatures.len() {
35+
while !stack.is_empty() && temperatures[i] > temperatures[stack[stack.len() - 1]] {
36+
let prev = stack.pop().unwrap();
37+
res[prev] = (i - prev) as i32;
38+
}
39+
stack.push(i);
40+
}
41+
42+
return res;
43+
}
44+
45+
pub fn daily_temperatures_greedy(temperatures: Vec<i32>) -> Vec<i32> {
46+
let mut res = vec![0; temperatures.len()];
47+
for i in 0..temperatures.len() {
48+
for j in i + 1..temperatures.len() {
49+
if temperatures[j] > temperatures[i] {
50+
res[i] = (j - i) as i32;
51+
break;
52+
}
53+
}
54+
}
55+
return res;
56+
}
57+
58+
#[test]
59+
pub fn t1() {
60+
assert_eq!(
61+
daily_temperatures_monotonic_stack(vec![73, 74, 75, 71, 69, 72, 76, 73]),
62+
vec![1, 1, 4, 2, 1, 1, 0, 0]
63+
);
64+
assert_eq!(
65+
daily_temperatures_greedy(vec![73, 74, 75, 71, 69, 72, 76, 73]),
66+
vec![1, 1, 4, 2, 1, 1, 0, 0]
67+
);
68+
}

stack/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
mod _0020_valid_parentheses;
44
mod _0022_generate_parentheses;
55
mod _0155_min_stack;
6+
mod _0739_daily_temperatures;
67
mod _1598_crawler_log_folder;

0 commit comments

Comments
 (0)