File tree Expand file tree Collapse file tree 5 files changed +178
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +178
-0
lines changed Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // SC: O(1)
3+ impl Solution {
4+ pub fn max_area ( heights : Vec < i32 > ) -> i32 {
5+ let ( mut left, mut right) = ( 0 , heights. len ( ) - 1 ) ;
6+ let mut res = 0i32 ;
7+ while left <= right {
8+ res = res. max ( ( ( right - left) as i32 ) * heights[ left] . min ( heights[ right] ) ) ;
9+
10+ if heights[ right] < heights[ left] {
11+ right -= 1 ;
12+ } else {
13+ left += 1 ;
14+ }
15+ }
16+
17+ res
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ type WordDictionary struct {
2+ root * TrieNode
3+ }
4+
5+ func Constructor () WordDictionary {
6+ return WordDictionary {root : NewTrieNode ()}
7+ }
8+
9+ // TC: O(n)
10+ // SC: O(t + n)
11+ // t = total number of TrieNodes created so far
12+ func (this * WordDictionary ) AddWord (word string ) {
13+ cur := this .root
14+ for _ , c := range word {
15+ if cur .Children [c - 'a' ] == nil {
16+ cur .Children [c - 'a' ] = NewTrieNode ()
17+ }
18+ cur = cur .Children [c - 'a' ]
19+ }
20+ cur .IsWord = true
21+ }
22+
23+ // TC: O(n)
24+ func (this * WordDictionary ) Search (word string ) bool {
25+ return this .dfs (word , 0 , this .root )
26+ }
27+
28+ func (this * WordDictionary ) dfs (word string , index int , root * TrieNode ) bool {
29+ cur := root
30+ for i := index ; i < len (word ); i ++ {
31+ c := word [i ]
32+ if c == '.' {
33+ for _ , child := range cur .Children {
34+ if child != nil && this .dfs (word , i + 1 , child ) {
35+ return true
36+ }
37+ }
38+ return false
39+ } else {
40+ if cur .Children [c - 'a' ] == nil {
41+ return false
42+ }
43+ cur = cur .Children [c - 'a' ]
44+ }
45+ }
46+ return cur .IsWord
47+ }
48+
49+ type TrieNode struct {
50+ Children [26 ]* TrieNode
51+ IsWord bool
52+ }
53+
54+ func NewTrieNode () * TrieNode {
55+ return & TrieNode {}
56+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n ^ 2)
2+ // SC: O(n)
3+ func lengthOfLIS (nums []int ) int {
4+ dp := make ([]int , len (nums ))
5+
6+ for i := range dp {
7+ dp [i ] = 1
8+ }
9+
10+ for i := len (nums ) - 2 ; i >= 0 ; i -- {
11+ for j := i + 1 ; j < len (nums ); j ++ {
12+ if nums [i ] < nums [j ] {
13+ if dp [i ] < 1 + dp [j ] {
14+ dp [i ] = 1 + dp [j ]
15+ }
16+ }
17+ }
18+ }
19+
20+ maxLength := 1
21+ for _ , length := range dp {
22+ maxLength = max (maxLength , length )
23+ }
24+ return maxLength
25+ }
26+
27+ func max (a , b int ) int {
28+ if a > b {
29+ return a
30+ }
31+ return b
32+ }
Original file line number Diff line number Diff line change 1+ // TC: O(m * n)
2+ // SC: O(m * n)
3+ impl Solution {
4+ pub fn spiral_order ( matrix : Vec < Vec < i32 > > ) -> Vec < i32 > {
5+ let mut result: Vec < i32 > = Vec :: new ( ) ;
6+
7+ let mut left = 0i32 ;
8+ let mut right = matrix[ 0 ] . len ( ) as i32 ;
9+ let mut top = 0i32 ;
10+ let mut bottom = matrix. len ( ) as i32 ;
11+
12+ while left < right && top < bottom {
13+ for i in left..right {
14+ result. push ( matrix[ top as usize ] [ i as usize ] ) ;
15+ }
16+ top += 1 ;
17+ for i in top..bottom {
18+ result. push ( matrix[ i as usize ] [ ( right - 1 ) as usize ] ) ;
19+ }
20+ right -= 1 ;
21+
22+ if !( left < right && top < bottom) {
23+ break ;
24+ }
25+
26+ for i in ( left..right) . rev ( ) {
27+ result. push ( matrix[ ( bottom - 1 ) as usize ] [ i as usize ] ) ;
28+ }
29+ bottom -= 1 ;
30+ for i in ( top..bottom) . rev ( ) {
31+ result. push ( matrix[ i as usize ] [ left as usize ] ) ;
32+ }
33+ left += 1 ;
34+ }
35+
36+ result
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // SC: O(n)
3+ impl Solution {
4+ pub fn is_valid ( s : String ) -> bool {
5+ let mut stack = Vec :: new ( ) ;
6+
7+ for c in s. chars ( ) {
8+ match c {
9+ '{' | '(' | '[' => {
10+ stack. push ( c) ;
11+ } ,
12+ '}' => {
13+ if Some ( '{' ) != stack. pop ( ) {
14+ return false ;
15+ }
16+ } ,
17+ ')' => {
18+ if Some ( '(' ) != stack. pop ( ) {
19+ return false ;
20+ }
21+ } ,
22+ ']' => {
23+ if Some ( '[' ) != stack. pop ( ) {
24+ return false ;
25+ }
26+ } ,
27+ _ => { } ,
28+ } ;
29+ }
30+
31+ stack. is_empty ( )
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments