File tree Expand file tree Collapse file tree
container-with-most-water
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ ์์์ธ๋ฑ์ค start์ ๋์ธ๋ฑ์ค end๋ฅผ ์ง์ ํ๊ณ result ๋ณ์๋ฅผ ๊ฐฑ์ , start์ end๋ฅผ ์ขํ๊ฐ๋ฉฐ ์ต๋ result๋ฅผ ์ฐพ๋๋ค
3+ - start์ end ์ค ๋ ์์ ๊ฐ์ ์ฎ๊ฒจ๊ฐ๋ฉฐ ๊ฐ์ ๋น๊ต.
4+ - ๋์ด ๊ฒน์ณ์ง๋ฉด ๋ฆฌํดํ๋ค.
5+
6+ result๋ฅผ ๋ฆฌํด
7+
8+ ์๊ฐ๋ณต์ก๋ : O(N), ๊ณต๊ฐ๋ณต์ก๋ : O(1)
9+ */
10+
11+ function maxArea ( height : number [ ] ) : number {
12+ let start = 0
13+ let end = height . length - 1
14+ let result = 0
15+
16+ function getAmount ( startIdx : number , endIdx : number ) : number {
17+ return ( endIdx - startIdx ) * Math . min ( height [ endIdx ] , height [ startIdx ] )
18+ }
19+
20+ while ( start < end ) {
21+ const area = getAmount ( start , end )
22+ result = Math . max ( result , area )
23+
24+ if ( height [ start ] < height [ end ] ) {
25+ start += 1
26+ } else {
27+ end -= 1
28+ }
29+ }
30+
31+ return result
32+ }
Original file line number Diff line number Diff line change 1+ /**
2+ ์ด๋ถํ์์ผ๋ก ์ ๋ ฌ ํ์. ์ต์๊ฐ์ด ๋ค์ด์๋ ๋ฒ์๋ฅผ ๋ฐํ์ผ๋ก ๊ณ์ ์ขํ ๊ฐ๋ค
3+ ์ค๊ฐ๊ฐ์ ๊ธฐ์ค๊ฐ์ผ๋ก ์ก๊ณ , ๋ง์ง๋ง ๊ฐ๋ณด๋ค ํฐ์ง ์์์ง ํ์ธํ๋ค
4+ ๊ธฐ์ค๊ฐ์ด ๋ง์ง๋ง ๊ฐ๋ณด๋ค ์์ ๊ฒฝ์ฐ, ์ต์๊ฐ์ ์ผ์ชฝ ๋ฐ์ชฝ์ ์๊ฑฐ๋ ๊ธฐ์ค๊ฐ์. ex) 5 1 2 3 4
5+ -> range๋ฅผ ์ผ์ชฝ ๋ฐ์ชฝ์ผ๋ก ์ค์ด๊ณ (๊ธฐ์ค๊ฐ ํฌํจ) ํด๋น ์ค๊ฐ๊ฐ์ ๊ธฐ์ค๊ฐ์ผ๋ก ์ก๋๋ค
6+ ๊ธฐ์ค๊ฐ์ด ๋ง์ง๋ง ๊ฐ๋ณด๋ค ํด ๊ฒฝ์ฐ, ์ต์๊ฐ์ ์ค๋ฅธ์ชฝ ๋ฐ์ชฝ์ ์์. ex) 3 4 5 1 2
7+ -> range๋ฅผ ์ค๋ฅธ์ชฝ ๋ฐ์ชฝ์ผ๋ก ์ค์ด๊ณ ํด๋น ์ค๊ฐ๊ฐ์ ๊ธฐ์ค๊ฐ์ผ๋ก ์ก๋๋ค
8+
9+ ์ต์ข
์ ์ผ๋ก start, end๊ฐ ๊ฐ์์ง๋ ๊ฐ์ด ์ต์๊ฐ์ด ๋๋ค.
10+
11+ ์๊ฐ๋ณต์ก๋ : O(LogN) -> N์ nums์ ๊ฐ์. ์ด๋ถํ์
12+ */
13+
14+ function findMin ( nums : number [ ] ) : number {
15+ let start = 0
16+ let end = nums . length - 1
17+
18+ while ( start < end ) {
19+ let target = Math . floor ( ( start + end ) / 2 )
20+ if ( nums [ target ] < nums [ end ] ) {
21+ end = target
22+ } else {
23+ start = target + 1
24+ }
25+ }
26+
27+ return nums [ start ]
28+ }
Original file line number Diff line number Diff line change 1+ /*
2+ ์คํ์ ๋ง๋ค์ด s๋ฅผ ํ์ํ๋ฉฐ ์ง์ด๋ฃ๊ณ , ํ์ฉ๋์ง ์์ ๊ดํธ๊ฐ ์ค๋ฉด false๋ฅผ ๋ฆฌํดํ๋ค
3+ - ํ์ฉํ๋ ๊ดํธ : ์ด๋ ค์๋ ๊ดํธ (, {, [ ํน์ ๋ง์ง๋ง ์์์ ์ง์ด ๋๋ ๋ซ๋๊ดํธ
4+
5+ ์๊ฐ๋ณต์ก๋ : O(N) - N์ s์ length
6+ ๊ณต๊ฐ๋ณต์ก๋ : O(N) (๊ดํธ ๋ฐฐ์ด ๋ฐ ๊ดํธ ์)
7+ */
8+
9+ function isValid ( s : string ) : boolean {
10+ const stack = [ ]
11+ const openBrackets = [ '(' , '{' , '[' ]
12+ const bracketMap = {
13+ '(' : ')' ,
14+ '{' : '}' ,
15+ '[' : ']' ,
16+ }
17+
18+ for ( const char of s ) {
19+ if ( openBrackets . includes ( char ) ) {
20+ stack . push ( char )
21+ continue
22+ }
23+ const last = stack . at ( - 1 )
24+ if ( char === bracketMap [ last ] ) {
25+ stack . pop ( )
26+ continue
27+ }
28+
29+ return false
30+ }
31+
32+ return stack . length === 0
33+ }
You canโt perform that action at this time.
0 commit comments