File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ /**
3+ * ์ต์ฅ ์ฆ๊ฐ ๋ถ๋ถ ์์ด์ ๊ณ์ฐ
4+ * @param {number[] } nums
5+ * @returns {number } - ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ด์ ์ต๋ ๊ธธ์ด
6+ *
7+ * ์๊ฐ ๋ณต์ก๋: O( n log(n) )
8+ * - n์ ๋ฐฐ์ด์ ๊ธธ์ด
9+ * - ๊ฐ ์์๋ฅผ ์ฒ๋ฆฌํ ๋ ์ด๋ถ ํ์์ผ๋ก ์์น๋ฅผ ์ฐพ๊ธฐ ๋๋ฌธ์ log(n)์ด ์์๋จ
10+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
11+ * - ๋ฐฐ์ด์ ์ต๋ n๊ฐ์ ์์๋ฅผ ์ ์ฅ ๊ฐ๋ฅ
12+ */
13+ function lengthOfLIS ( nums : number [ ] ) : number {
14+ // ์์ด์ ์ ์ฅํ ๋ฐฐ์ด
15+ const sequnces : number [ ] = [ ] ;
16+
17+ for ( let num of nums ) {
18+ // ์ด๋ถ ํ์์ ์ฌ์ฉํ์ฌ num์ด ๋ค์ด๊ฐ ์์น๋ฆ ์ฐพ์
19+ let left = 0 ;
20+ let right = sequnces . length ;
21+
22+ while ( left < right ) {
23+ const mid = Math . floor ( ( left + right ) / 2 ) ;
24+ if ( sequnces [ mid ] < num ) {
25+ left = mid + 1 ;
26+ } else {
27+ right = mid ;
28+ }
29+ }
30+
31+ // ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ์กด ์์๋ฅผ ๋์ฒด
32+ if ( left < sequnces . length ) {
33+ sequnces [ left ] = num ;
34+ } else {
35+ sequnces . push ( num )
36+ }
37+ }
38+
39+ return sequnces . length ;
40+ }
41+
You canโt perform that action at this time.
0 commit comments