Skip to content

Commit fc69f99

Browse files
committed
Added tasks 84-153
1 parent 6474adf commit fc69f99

File tree

21 files changed

+1654
-0
lines changed

21 files changed

+1654
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 79\. Word Search
5+
6+
Medium
7+
8+
Given an `m x n` grid of characters `board` and a string `word`, return `true` _if_ `word` _exists in the grid_.
9+
10+
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
15+
16+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
23+
24+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
25+
26+
**Output:** true
27+
28+
**Example 3:**
29+
30+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
31+
32+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `m == board.length`
39+
* `n = board[i].length`
40+
* `1 <= m, n <= 6`
41+
* `1 <= word.length <= 15`
42+
* `board` and `word` consists of only lowercase and uppercase English letters.
43+
44+
**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
45+
46+
## Solution
47+
48+
```rust
49+
impl Solution {
50+
pub fn exist(mut board: Vec<Vec<char>>, word: String) -> bool {
51+
let mut word = Vec::from_iter(word.chars());
52+
let mut map = [0; 58]; //A-65 z-122
53+
for i in 0..board.len() {
54+
for j in 0..board[0].len() {
55+
map[(board[i][j] as u8 - 65) as usize] += 1;
56+
}
57+
}
58+
59+
for c in &word {
60+
if map[(*c as u8 - 65) as usize] == 0 {
61+
return false;
62+
}
63+
}
64+
65+
if map[(word[0] as u8 - 65) as usize] > map[(word[word.len() - 1] as u8 - 65) as usize] {
66+
word.reverse();
67+
}
68+
69+
for i in 0..board.len() {
70+
for j in 0..board[0].len() {
71+
if board[i][j] != word[0] {
72+
continue;
73+
}
74+
let mut p = 0;
75+
let (mut k, mut l) = (i, j);
76+
let mut dir = vec![-1; word.len() - 1];
77+
loop {
78+
if p == word.len() - 1 {
79+
return true;
80+
}
81+
if dir[p] == -1 {
82+
board[k][l] = ' ';
83+
dir[p] = 0;
84+
if k > 0 && board[k - 1][l] == word[p + 1] {
85+
p += 1;
86+
k -= 1;
87+
continue;
88+
}
89+
}
90+
if dir[p] == 0 {
91+
dir[p] = 1;
92+
if l < board[k].len() - 1 && board[k][l + 1] == word[p + 1] {
93+
p += 1;
94+
l += 1;
95+
continue;
96+
}
97+
}
98+
if dir[p] == 1 {
99+
dir[p] = 2;
100+
if k < board.len() - 1 && board[k + 1][l] == word[p + 1] {
101+
p += 1;
102+
k += 1;
103+
continue;
104+
}
105+
}
106+
if dir[p] == 2 {
107+
dir[p] = 3;
108+
if l > 0 && board[k][l - 1] == word[p + 1] {
109+
p += 1;
110+
l -= 1;
111+
continue;
112+
}
113+
}
114+
while p > 0 && dir[p] == 3 {
115+
board[k][l] = word[p];
116+
dir[p] = -1;
117+
p -= 1;
118+
if dir[p] == 0 {
119+
k += 1;
120+
continue;
121+
}
122+
if dir[p] == 1 {
123+
l -= 1;
124+
continue;
125+
}
126+
if dir[p] == 2 {
127+
k -= 1;
128+
continue;
129+
}
130+
if dir[p] == 3 {
131+
l += 1;
132+
continue;
133+
}
134+
}
135+
if p == 0 && dir[p] == 3 {
136+
board[k][l] = word[p];
137+
break;
138+
}
139+
}
140+
}
141+
}
142+
false
143+
}
144+
}
145+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 84\. Largest Rectangle in Histogram
5+
6+
Hard
7+
8+
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return _the area of the largest rectangle in the histogram_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)
13+
14+
**Input:** heights = [2,1,5,6,2,3]
15+
16+
**Output:** 10
17+
18+
**Explanation:** The above is a histogram where width of each bar is 1.
19+
20+
The largest rectangle is shown in the red area, which has an area = 10 units.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)
25+
26+
**Input:** heights = [2,4]
27+
28+
**Output:** 4
29+
30+
**Constraints:**
31+
32+
* <code>1 <= heights.length <= 10<sup>5</sup></code>
33+
* <code>0 <= heights[i] <= 10<sup>4</sup></code>
34+
35+
## Solution
36+
37+
```rust
38+
impl Solution {
39+
pub fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
40+
Self::largest_area(&heights, 0, heights.len())
41+
}
42+
43+
fn largest_area(heights: &[i32], start: usize, limit: usize) -> i32 {
44+
if heights.is_empty() || start == limit {
45+
return 0;
46+
}
47+
if limit - start == 1 {
48+
return heights[start];
49+
}
50+
if limit - start == 2 {
51+
let max_of_two_bars = heights[start].max(heights[start + 1]);
52+
let area_from_two = heights[start].min(heights[start + 1]) * 2;
53+
return max_of_two_bars.max(area_from_two);
54+
}
55+
if Self::check_if_sorted(heights, start, limit) {
56+
let mut max_when_sorted = 0;
57+
for i in start..limit {
58+
max_when_sorted = max_when_sorted.max(heights[i] * (limit - i) as i32);
59+
}
60+
return max_when_sorted;
61+
} else {
62+
let min_ind = Self::find_min_in_array(heights, start, limit);
63+
return Self::max_of_three_nums(
64+
Self::largest_area(heights, start, min_ind),
65+
heights[min_ind] * (limit - start) as i32,
66+
Self::largest_area(heights, min_ind + 1, limit),
67+
);
68+
}
69+
}
70+
71+
fn find_min_in_array(heights: &[i32], start: usize, limit: usize) -> usize {
72+
let mut min = i32::MAX;
73+
let mut min_index = 0;
74+
for i in start..limit {
75+
if heights[i] < min {
76+
min = heights[i];
77+
min_index = i;
78+
}
79+
}
80+
min_index
81+
}
82+
83+
fn check_if_sorted(heights: &[i32], start: usize, limit: usize) -> bool {
84+
for i in start + 1..limit {
85+
if heights[i] < heights[i - 1] {
86+
return false;
87+
}
88+
}
89+
true
90+
}
91+
92+
fn max_of_three_nums(a: i32, b: i32, c: i32) -> i32 {
93+
a.max(b).max(c)
94+
}
95+
}
96+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 94\. Binary Tree Inorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
13+
14+
**Input:** root = [1,null,2,3]
15+
16+
**Output:** [1,3,2]
17+
18+
**Example 2:**
19+
20+
**Input:** root = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** root = [1]
27+
28+
**Output:** [1]
29+
30+
**Example 4:**
31+
32+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)
33+
34+
**Input:** root = [1,2]
35+
36+
**Output:** [2,1]
37+
38+
**Example 5:**
39+
40+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)
41+
42+
**Input:** root = [1,null,2]
43+
44+
**Output:** [1,2]
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is in the range `[0, 100]`.
49+
* `-100 <= Node.val <= 100`
50+
51+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
52+
53+
## Solution
54+
55+
```rust
56+
// Definition for a binary tree node.
57+
// pub struct TreeNode {
58+
// pub val: i32,
59+
// pub left: Option<Rc<RefCell<TreeNode>>>,
60+
// pub right: Option<Rc<RefCell<TreeNode>>>,
61+
// }
62+
//
63+
// impl TreeNode {
64+
// #[inline]
65+
// pub fn new(val: i32) -> Self {
66+
// TreeNode {
67+
// val,
68+
// left: None,
69+
// right: None
70+
// }
71+
// }
72+
// }
73+
use std::rc::Rc;
74+
use std::cell::RefCell;
75+
impl Solution {
76+
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
77+
root.map(|root| {
78+
let n = root.borrow();
79+
let mut result = Self::inorder_traversal(n.left.clone());
80+
result.push(n.val);
81+
result.extend(Self::inorder_traversal(n.right.clone()));
82+
result
83+
})
84+
.unwrap_or_default()
85+
}
86+
}
87+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 96\. Unique Binary Search Trees
5+
6+
Medium
7+
8+
Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)
13+
14+
**Input:** n = 3
15+
16+
**Output:** 5
17+
18+
**Example 2:**
19+
20+
**Input:** n = 1
21+
22+
**Output:** 1
23+
24+
**Constraints:**
25+
26+
* `1 <= n <= 19`
27+
28+
## Solution
29+
30+
```rust
31+
impl Solution {
32+
pub fn num_trees(n: i32) -> i32 {
33+
let mut result: i64 = 1;
34+
for i in 0..n {
35+
result *= 2 * (n as i64) - (i as i64);
36+
result /= (i + 1) as i64;
37+
}
38+
result /= (n + 1) as i64;
39+
result as i32
40+
}
41+
}
42+
```

0 commit comments

Comments
 (0)