Skip to content

Commit 66db3ab

Browse files
committed
solve #84
1 parent 9f6742d commit 66db3ab

7 files changed

+308
-0
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@ mod n0076_minimum_window_substring;
8080
mod n0077_combinations;
8181
mod n0078_subsets;
8282
mod n0079_word_search;
83+
mod n0080_remove_duplicates_from_sorted_array_ii;
84+
mod n0081_search_in_rotated_sorted_array_ii;
85+
mod n0082_remove_duplicates_from_sorted_list_ii;
86+
mod n0083_remove_duplicates_from_sorted_list;
87+
mod n0084_largest_rectangle_in_histogram;

src/n0079_word_search.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Solution {}
2525

2626
// submission codes start here
2727

28+
// TODO: use HashSet to record visited pos
2829
impl Solution {
2930
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
3031
if board.is_empty() || word.len() < 1 { return false }
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* [80] Remove Duplicates from Sorted Array II
3+
*
4+
* Given a sorted array nums, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> such that duplicates appeared at most twice and return the new length.
5+
*
6+
* Do not allocate extra space for another array, you must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Given nums = [1,1,1,2,2,3],
12+
*
13+
* Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
14+
*
15+
* It doesn't matter what you leave beyond the returned length.
16+
*
17+
* Example 2:
18+
*
19+
*
20+
* Given nums = [0,0,1,1,1,1,2,3,3],
21+
*
22+
* Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
23+
*
24+
* It doesn't matter what values are set beyond the returned length.
25+
*
26+
*
27+
* Clarification:
28+
*
29+
* Confused why the returned value is an integer but your answer is an array?
30+
*
31+
* Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
32+
*
33+
* Internally you can think of this:
34+
*
35+
*
36+
* // nums is passed in by reference. (i.e., without making a copy)
37+
* int len = removeDuplicates(nums);
38+
*
39+
* // any modification to nums in your function would be known by the caller.
40+
* // using the length returned by your function, it prints the first len elements.
41+
* for (int i = 0; i < len; i++) {
42+
* print(nums[i]);
43+
* }
44+
*
45+
*
46+
*/
47+
pub struct Solution {}
48+
49+
// submission codes start here
50+
51+
impl Solution {
52+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
53+
0
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_80() {
65+
}
66+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [81] Search in Rotated Sorted Array II
3+
*
4+
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5+
*
6+
* (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
7+
*
8+
* You are given a target value to search. If found in the array return true, otherwise return false.
9+
*
10+
* Example 1:
11+
*
12+
*
13+
* Input: nums = [2,5,6,0,0,1,2], target = 0
14+
* Output: true
15+
*
16+
*
17+
* Example 2:
18+
*
19+
*
20+
* Input: nums = [2,5,6,0,0,1,2], target = 3
21+
* Output: false
22+
*
23+
* Follow up:
24+
*
25+
*
26+
* This is a follow up problem to <a href="/problems/search-in-rotated-sorted-array/description/">Search in Rotated Sorted Array</a>, where nums may contain duplicates.
27+
* Would this affect the run-time complexity? How and why?
28+
*
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// submission codes start here
34+
35+
impl Solution {
36+
pub fn search(nums: Vec<i32>, target: i32) -> bool {
37+
false
38+
}
39+
}
40+
41+
// submission codes end
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn test_81() {
49+
}
50+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* [82] Remove Duplicates from Sorted List II
3+
*
4+
* Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: 1->2->3->3->4->4->5
10+
* Output: 1->2->5
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: 1->1->1->2->3
17+
* Output: 2->3
18+
*
19+
*
20+
*/
21+
pub struct Solution {}
22+
use super::util::linked_list::{ListNode, to_list};
23+
24+
// submission codes start here
25+
26+
// Definition for singly-linked list.
27+
// #[derive(PartialEq, Eq, Debug)]
28+
// pub struct ListNode {
29+
// pub val: i32,
30+
// pub next: Option<Box<ListNode>>
31+
// }
32+
//
33+
// impl ListNode {
34+
// #[inline]
35+
// fn new(val: i32) -> Self {
36+
// ListNode {
37+
// next: None,
38+
// val
39+
// }
40+
// }
41+
// }
42+
impl Solution {
43+
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
44+
None
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
fn test_82() {
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* [83] Remove Duplicates from Sorted List
3+
*
4+
* Given a sorted linked list, delete all duplicates such that each element appear only once.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: 1->1->2
10+
* Output: 1->2
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: 1->1->2->3->3
17+
* Output: 1->2->3
18+
*
19+
*
20+
*/
21+
pub struct Solution {}
22+
use super::util::linked_list::{ListNode, to_list};
23+
24+
// submission codes start here
25+
26+
// Definition for singly-linked list.
27+
// #[derive(PartialEq, Eq, Debug)]
28+
// pub struct ListNode {
29+
// pub val: i32,
30+
// pub next: Option<Box<ListNode>>
31+
// }
32+
//
33+
// impl ListNode {
34+
// #[inline]
35+
// fn new(val: i32) -> Self {
36+
// ListNode {
37+
// next: None,
38+
// val
39+
// }
40+
// }
41+
// }
42+
impl Solution {
43+
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
44+
None
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
fn test_83() {
56+
}
57+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* [84] Largest Rectangle in Histogram
3+
*
4+
* Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
5+
*
6+
*
7+
*
8+
* <img src="https://assets.leetcode.com/uploads/2018/10/12/histogram.png" style="width: 188px; height: 204px;" /><br />
9+
* <small>Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].</small>
10+
*
11+
*
12+
*
13+
* <img src="https://assets.leetcode.com/uploads/2018/10/12/histogram_area.png" style="width: 188px; height: 204px;" /><br />
14+
* <small>The largest rectangle is shown in the shaded area, which has area = 10 unit.</small>
15+
*
16+
*
17+
*
18+
* Example:
19+
*
20+
*
21+
* Input: [2,1,5,6,2,3]
22+
* Output: 10
23+
*
24+
*
25+
*/
26+
pub struct Solution {}
27+
28+
// submission codes start here
29+
30+
// record the height and start position using 2 stack, thus we reuse the previously scanned information
31+
impl Solution {
32+
pub fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
33+
let mut positions = Vec::new();
34+
let mut hs = Vec::new();
35+
let mut max_area = 0;
36+
let len = heights.len();
37+
for (i, h) in heights.into_iter().enumerate() {
38+
let mut last_pop = None;
39+
while hs.last().is_some() && *hs.last().unwrap() >= h {
40+
max_area = i32::max(max_area, hs.last().unwrap() * ((i - positions.last().unwrap()) as i32));
41+
hs.pop();
42+
last_pop = positions.pop();
43+
}
44+
if last_pop.is_some() { positions.push(last_pop.unwrap()); } else { positions.push(i); }
45+
hs.push(h);
46+
}
47+
// drain stack
48+
while !hs.is_empty() {
49+
max_area = i32::max(max_area, hs.last().unwrap() * ((len - positions.last().unwrap()) as i32));
50+
positions.pop();
51+
hs.pop();
52+
}
53+
max_area
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_84() {
65+
assert_eq!(Solution::largest_rectangle_area(vec![2,1,5,6,2,3]), 10);
66+
assert_eq!(Solution::largest_rectangle_area(vec![1,1,1,1,1,1,1,1]), 8);
67+
assert_eq!(Solution::largest_rectangle_area(vec![2,2]), 4);
68+
assert_eq!(Solution::largest_rectangle_area(vec![1,2,8,8,2,2,1]), 16);
69+
assert_eq!(Solution::largest_rectangle_area(vec![2,1,2]), 3);
70+
assert_eq!(Solution::largest_rectangle_area(vec![1,3,2,1,2]), 5);
71+
}
72+
}

0 commit comments

Comments
 (0)