File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change 1+ function maxArea ( height : number [ ] ) : number {
2+ let left = 0 ;
3+ let right = height . length - 1 ;
4+ let maxSize = 0 ;
5+
6+ while ( left < right ) {
7+ maxSize = Math . max ( maxSize , getMaxSize ( height , left , right ) ) ;
8+
9+ if ( height [ left ] < height [ right ] ) {
10+ left ++ ;
11+ } else {
12+ right -- ;
13+ }
14+ }
15+
16+ return maxSize ;
17+ }
18+
19+ function getMaxSize ( height : number [ ] , left : number , right : number ) {
20+ return Math . min ( ...[ height [ right ] , height [ left ] ] ) * ( right - left ) ;
21+ }
22+
23+ // TC: O(n)
24+ // SC: O(1)
Original file line number Diff line number Diff line change 1+ class WordDictionary {
2+ wordCountMap : Map < number , Set < string > > ;
3+ constructor ( ) {
4+ this . wordCountMap = new Map ( ) ;
5+ }
6+
7+ // TC: O(1)
8+ // SC: O(n)
9+ addWord ( word : string ) : void {
10+ const length = word . length ;
11+ if ( this . wordCountMap . has ( length ) ) {
12+ this . wordCountMap . get ( length ) . add ( word ) ;
13+ } else {
14+ this . wordCountMap . set ( length , new Set ( [ word ] ) ) ;
15+ }
16+ return null ;
17+ }
18+
19+ // TC: O(m*n) // m: words length, n: word length
20+ // SC: O(n)
21+ search ( word : string ) : boolean {
22+ const len = word . length ;
23+ const targetWord = word . replace ( / \. / g, "" ) ;
24+ const hasDot = len - targetWord . length !== 0 ;
25+
26+ if ( ! this . wordCountMap . has ( len ) ) {
27+ return false ;
28+ }
29+ const words = this . wordCountMap . get ( len ) ;
30+ if ( ! hasDot ) {
31+ return words . has ( word ) ;
32+ }
33+
34+ for ( const w of words ) {
35+ let match = true ;
36+ for ( let j = 0 ; j < w . length ; j ++ ) {
37+ if ( word [ j ] !== "." && word [ j ] !== w [ j ] ) {
38+ match = false ;
39+ break ;
40+ }
41+ }
42+ if ( match ) {
43+ return true ;
44+ }
45+ }
46+ return false ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ function spiralOrder ( matrix : number [ ] [ ] ) : number [ ] {
2+ const rows = matrix . length ;
3+ const cols = matrix [ 0 ] . length ;
4+ const total = rows * cols ;
5+ let srow = 0 ; // start row
6+ let scol = 0 ;
7+ let erow = rows - 1 ; // end row
8+ let ecol = cols - 1 ;
9+ let count = 0 ;
10+ const ans : number [ ] = [ ] ;
11+
12+ while ( count < total ) {
13+ for ( let i = scol ; i <= ecol && count < total ; i ++ ) {
14+ ans . push ( matrix [ srow ] [ i ] ) ;
15+ count ++ ;
16+ }
17+ srow ++ ;
18+ for ( let i = srow ; i <= erow && count < total ; i ++ ) {
19+ ans . push ( matrix [ i ] [ ecol ] ) ;
20+ count ++ ;
21+ }
22+ ecol -- ;
23+ for ( let i = ecol ; i >= scol && count < total ; i -- ) {
24+ ans . push ( matrix [ erow ] [ i ] ) ;
25+ count ++ ;
26+ }
27+ erow -- ;
28+ for ( let i = erow ; i >= srow && count < total ; i -- ) {
29+ ans . push ( matrix [ i ] [ scol ] ) ;
30+ count ++ ;
31+ }
32+ scol ++ ;
33+ }
34+
35+ return ans ;
36+ }
37+
38+ // TC: O(m*n)
39+ // SC: O(m*n)
Original file line number Diff line number Diff line change 1+ /*
2+ * ์์ด๋์ด
3+ * stack ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํด ์ฌ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด ์์๋๋ก ์ ์ฅํด๋๋ค.
4+ * stack์ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฐ์ฅ ์ต๊ทผ ์ฌ๋ ๊ดํธ๊ฐ ์ด๋ค ๊ฒ์ธ์ง ํ์ธํ๊ธฐ ์ํจ์ด๋ค.(๋ง์ง๋ง์ ์ฝ์
ํ ๊ฐ์ ๋จผ์ ์ฌ์ฉํจ)
5+ * ๋ซ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด stack์ ๋ง์ง๋ง ๊ฐ์ด pair์ธ ์ฌ๋ ๊ดํธ์ธ์ง ์ฒดํฌํ๋ค. ๋ค๋ฅด๋ฉด return false
6+ * ๋ฌธ์์ด ๋ฐ๋ณต๋ฌธ์ ๋ค ๋๊ณ stack์ ์ฌ๋ ๊ดํธ๊ฐ ๋จ์์์ง ์์์ง ๋ณธ๋ค. ๋จ์์์ผ๋ฉด return false
7+ * ์์ ๋ ๊ฒฝ์ฐ์ ํด๋น๋์ง ์์ผ๋ฉด return true
8+ */
9+ function isValid ( s : string ) : boolean {
10+ const openCharStack = [ ] ;
11+ const CHAR_PAIR = {
12+ "]" : "[" ,
13+ "}" : "{" ,
14+ ")" : "(" ,
15+ } ;
16+
17+ for ( let i = 0 ; i <= s . length - 1 ; i ++ ) {
18+ const char = s [ i ] ;
19+
20+ if ( char in CHAR_PAIR ) {
21+ const pair = CHAR_PAIR [ char ] ;
22+ const lastOpenChar = openCharStack . pop ( ) ;
23+ if ( lastOpenChar !== pair ) {
24+ return false ;
25+ }
26+ } else {
27+ openCharStack . push ( char ) ;
28+ }
29+ }
30+
31+ const isEmpty = openCharStack . length === 0 ;
32+ return isEmpty ;
33+ }
34+ // TC: O(n)
35+ // SC: O(n)
You canโt perform that action at this time.
0 commit comments