File tree Expand file tree Collapse file tree 7 files changed +345
-0
lines changed
Expand file tree Collapse file tree 7 files changed +345
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=57 lang=rust
3+ *
4+ * [57] Insert Interval
5+ */
6+
7+ // @lc code=start
8+ use std:: mem;
9+ use std:: cmp;
10+
11+ impl Solution {
12+ pub fn insert ( mut intervals : Vec < Vec < i32 > > , new_interval : Vec < i32 > ) -> Vec < Vec < i32 > > {
13+ let N = intervals. len ( ) ;
14+ if N == 0 { return vec ! [ new_interval] ; }
15+
16+ let mut ans = vec ! [ ] ;
17+ let mut i = 0 ;
18+
19+ while i < N && intervals[ i] [ 1 ] < new_interval[ 0 ] {
20+ ans. push ( mem:: replace ( & mut intervals[ i] , vec ! [ ] ) ) ;
21+ i += 1 ;
22+ }
23+
24+ if i < N {
25+ let left = cmp:: min ( intervals[ i] [ 0 ] , new_interval[ 0 ] ) ;
26+ let mut right = new_interval[ 1 ] ;
27+
28+ while i < N && intervals[ i] [ 0 ] <= new_interval[ 1 ] {
29+ if intervals[ i] [ 1 ] > right {
30+ right = intervals[ i] [ 1 ] ;
31+ }
32+ i += 1 ;
33+ }
34+
35+ ans. push ( vec ! [ left, right] ) ;
36+ } else {
37+ ans. push ( new_interval) ;
38+ }
39+
40+ while i < N {
41+ ans. push ( mem:: replace ( & mut intervals[ i] , vec ! [ ] ) ) ;
42+ i += 1 ;
43+ }
44+
45+ ans
46+ }
47+ }
48+ // @lc code=end
49+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=72 lang=rust
3+ *
4+ * [72] Edit Distance
5+ */
6+
7+ // @lc code=start
8+ impl Solution {
9+ pub fn min_distance ( word1 : String , word2 : String ) -> i32 {
10+ let chars1: Vec < char > = word1. chars ( ) . collect ( ) ;
11+ let chars2: Vec < char > = word2. chars ( ) . collect ( ) ;
12+
13+ let N1 = chars1. len ( ) ;
14+ let N2 = chars2. len ( ) ;
15+
16+ let mut dp = vec ! [ vec![ 0 ; N2 +1 ] ; N1 +1 ] ;
17+
18+ for i in 0 ..N1 +1 {
19+ for j in 0 ..N2 +1 {
20+ if i == 0 {
21+ dp[ i] [ j] = j;
22+ } else if j == 0 {
23+ dp[ i] [ j] = i;
24+ } else if chars1[ i-1 ] == chars2[ j-1 ] {
25+ dp[ i] [ j] = dp[ i-1 ] [ j-1 ] ;
26+ } else {
27+ dp[ i] [ j] = * [
28+ dp[ i] [ j-1 ] ,
29+ dp[ i-1 ] [ j] ,
30+ dp[ i-1 ] [ j-1 ]
31+ ] . iter ( ) . min ( ) . unwrap ( ) + 1 ;
32+ }
33+ }
34+ }
35+
36+ dp[ N1 ] [ N2 ] as i32
37+ }
38+ }
39+ // @lc code=end
40+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=76 lang=rust
3+ *
4+ * [76] Minimum Window Substring
5+ */
6+
7+ // @lc code=start
8+ use std:: collections:: HashMap ;
9+
10+ impl Solution {
11+ pub fn min_window ( s : String , t : String ) -> String {
12+ let mut t_cnt: HashMap < char , i32 > = HashMap :: new ( ) ;
13+ let mut s_cnt: HashMap < char , i32 > = HashMap :: new ( ) ;
14+
15+ let s_chars = s. chars ( ) . collect :: < Vec < char > > ( ) ;
16+
17+ for ch in t. chars ( ) {
18+ * t_cnt. entry ( ch) . or_insert ( 0 ) += 1 ;
19+ }
20+
21+ let mut n = t_cnt. len ( ) ;
22+ let mut left = 0 ;
23+ let mut right = 0 ;
24+ let mut ans = s. clone ( ) ;
25+ let mut has_window = false ;
26+
27+ while right < s_chars. len ( ) {
28+
29+ let counter = s_cnt. entry ( s_chars[ right] ) . or_insert ( 0 ) ;
30+ * counter += 1 ;
31+ if counter == t_cnt. get ( & s_chars[ right] ) . unwrap_or ( & 0 ) {
32+ n -= 1 ;
33+ }
34+
35+ while n == 0 {
36+ has_window = true ;
37+ let ch = s_chars[ left] ;
38+
39+ let count = s_cnt. get_mut ( & ch) . unwrap ( ) ;
40+ if t_cnt. contains_key ( & ch) && count == t_cnt. get ( & ch) . unwrap ( ) {
41+ if right - left < ans. len ( ) {
42+ ans = s_chars[ left..right+1 ] . iter ( ) . collect :: < String > ( ) ;
43+ }
44+ n += 1 ;
45+ }
46+
47+ * count -= 1 ;
48+ left += 1 ;
49+ }
50+ right += 1 ;
51+ }
52+
53+ if !has_window {
54+ "" . to_owned ( )
55+ } else {
56+ ans
57+ }
58+
59+ }
60+ }
61+ // @lc code=end
62+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=84 lang=rust
3+ *
4+ * [84] Largest Rectangle in Histogram
5+ */
6+
7+ // @lc code=start
8+ use std:: cmp;
9+
10+ impl Solution {
11+ pub fn largest_rectangle_area ( heights : Vec < i32 > ) -> i32 {
12+ let N = heights. len ( ) ;
13+
14+ let mut less_from_left = vec ! [ 0 ; N ] ;
15+ let mut less_from_right = vec ! [ 0 ; N ] ;
16+
17+ for i in 0 ..N {
18+ let mut p = i as i32 - 1 ;
19+
20+ while p >= 0 && heights[ p as usize ] >= heights[ i] {
21+ p = less_from_left[ p as usize ] ;
22+ }
23+
24+ less_from_left[ i] = p;
25+ }
26+
27+ for i in ( 0 ..N ) . rev ( ) {
28+ let mut p = i as i32 + 1 ;
29+
30+ while p < N as i32 && heights[ p as usize ] >= heights[ i] {
31+ p = less_from_right[ p as usize ] ;
32+ }
33+
34+ less_from_right[ i] = p;
35+ }
36+
37+ let mut ans = 0 ;
38+ for i in 0 ..N {
39+ ans = cmp:: max ( ans, heights[ i] * ( less_from_right[ i] - less_from_left[ i] - 1 ) ) ;
40+ }
41+
42+ ans
43+ }
44+ }
45+ // @lc code=end
46+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=85 lang=rust
3+ *
4+ * [85] Maximal Rectangle
5+ */
6+
7+ // @lc code=start
8+ use std:: cmp;
9+
10+ impl Solution {
11+ pub fn maximal_rectangle ( matrix : Vec < Vec < char > > ) -> i32 {
12+ let R = matrix. len ( ) ;
13+ if R == 0 { return 0 ; }
14+ let C = matrix[ 0 ] . len ( ) ;
15+
16+ let mut cols = vec ! [ 0 ; C +1 ] ;
17+ let mut ans = 0 ;
18+
19+ for i in 0 ..R {
20+ for j in 0 ..C {
21+ if matrix[ i] [ j] == '1' {
22+ cols[ j+1 ] += 1 ;
23+ } else {
24+ cols[ j+1 ] = 0 ;
25+ }
26+ }
27+
28+ ans = cmp:: max ( ans, Self :: largest_rectangle_area ( & cols) ) ;
29+ }
30+
31+ ans
32+ }
33+
34+ pub fn largest_rectangle_area ( heights : & Vec < i32 > ) -> i32 {
35+ let N = heights. len ( ) ;
36+
37+ let mut less_from_left = vec ! [ 0 ; N ] ;
38+ let mut less_from_right = vec ! [ 0 ; N ] ;
39+
40+ for i in 0 ..N {
41+ let mut p = i as i32 - 1 ;
42+
43+ while p >= 0 && heights[ p as usize ] >= heights[ i] {
44+ p = less_from_left[ p as usize ] ;
45+ }
46+
47+ less_from_left[ i] = p;
48+ }
49+
50+ for i in ( 0 ..N ) . rev ( ) {
51+ let mut p = i as i32 + 1 ;
52+
53+ while p < N as i32 && heights[ p as usize ] >= heights[ i] {
54+ p = less_from_right[ p as usize ] ;
55+ }
56+
57+ less_from_right[ i] = p;
58+ }
59+
60+ let mut ans = 0 ;
61+ for i in 0 ..N {
62+ ans = cmp:: max ( ans, heights[ i] * ( less_from_right[ i] - less_from_left[ i] - 1 ) ) ;
63+ }
64+
65+ ans
66+ }
67+ }
68+ // @lc code=end
69+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=97 lang=rust
3+ *
4+ * [97] Interleaving String
5+ */
6+
7+ // @lc code=start
8+ impl Solution {
9+ pub fn is_interleave ( s1 : String , s2 : String , s3 : String ) -> bool {
10+ let chars1 = s1. chars ( ) . collect :: < Vec < char > > ( ) ;
11+ let chars2 = s2. chars ( ) . collect :: < Vec < char > > ( ) ;
12+ let chars3 = s3. chars ( ) . collect :: < Vec < char > > ( ) ;
13+
14+ let n1 = chars1. len ( ) ;
15+ let n2 = chars2. len ( ) ;
16+ let n3 = chars3. len ( ) ;
17+
18+ if n1 + n2 != n3 { return false ; }
19+
20+ let mut dp = vec ! [ vec![ false ; n2+1 ] ; n1+1 ] ;
21+
22+ for i in 0 ..n1+1 {
23+ for j in 0 ..n2+1 {
24+ if i == 0 {
25+ if j == 0 {
26+ dp[ i] [ j] = true ;
27+ } else {
28+ dp[ i] [ j] = dp[ i] [ j-1 ] && chars2[ j-1 ] == chars3[ i+j-1 ] ;
29+ }
30+ } else {
31+ if j == 0 {
32+ dp[ i] [ j] = dp[ i-1 ] [ j] && chars1[ i-1 ] == chars3[ i+j-1 ] ;
33+ } else {
34+ dp[ i] [ j] = ( dp[ i] [ j-1 ] && chars2[ j-1 ] == chars3[ i+j-1 ] ) ||
35+ ( dp[ i-1 ] [ j] && chars1[ i-1 ] == chars3[ i+j-1 ] ) ;
36+ }
37+ }
38+ }
39+ }
40+
41+ dp[ n1] [ n2]
42+ }
43+ }
44+ // @lc code=end
45+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=99 lang=rust
3+ *
4+ * [99] Recover Binary Search Tree
5+ */
6+
7+ // @lc code=start
8+ // Definition for a binary tree node.
9+ // #[derive(Debug, PartialEq, Eq)]
10+ // pub struct TreeNode {
11+ // pub val: i32,
12+ // pub left: Option<Rc<RefCell<TreeNode>>>,
13+ // pub right: Option<Rc<RefCell<TreeNode>>>,
14+ // }
15+ //
16+ // impl TreeNode {
17+ // #[inline]
18+ // pub fn new(val: i32) -> Self {
19+ // TreeNode {
20+ // val,
21+ // left: None,
22+ // right: None
23+ // }
24+ // }
25+ // }
26+ use std:: rc:: Rc ;
27+ use std:: cell:: RefCell ;
28+ impl Solution {
29+ pub fn recover_tree ( root : & mut Option < Rc < RefCell < TreeNode > > > ) {
30+
31+ }
32+ }
33+ // @lc code=end
34+
You can’t perform that action at this time.
0 commit comments