File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 3ms
3+ * Time Complexity: O(n log n)
4+ * - ๊ฐ ์์๋ง๋ค ์ด๋ถ ํ์
5+ *
6+ * Memory: 45.99MB
7+ * Space Complexity: O(n)
8+ *
9+ * Approach: Patience Sorting, ๊ฐ ์์๋ฅผ ์์๋๋ก ํ์ธํ๋ฉฐ ํ์ฌ๊น์ง ๋ง๋ ๊ฐ์ฅ ์ ๋ฆฌํ ์์ด ๊ณ์ฐ
10+ * - result์ ๋ชจ๋ ์์๋ณด๋ค ํฌ๋ฉด ๋งจ ๋ค์ ์ถ๊ฐ
11+ * - ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ด๋ถ ํ์์ผ๋ก ๋์จ ์์น์ ๊ฐ์ ๋์ฒด
12+ */
13+ class Solution {
14+ public int lengthOfLIS (int [] nums ) {
15+ int [] result = new int [nums .length ];
16+ int resultSize = 0 ;
17+
18+ for (int num : nums ) {
19+ int left = 0 ;
20+ int right = resultSize ;
21+
22+ while (left < right ) {
23+ int mid = left + (right -left ) / 2 ;
24+ if (result [mid ] < num ) {
25+ left = mid +1 ;
26+ } else {
27+ right = mid ;
28+ }
29+ }
30+
31+ result [left ] = num ;
32+
33+ if (left == resultSize ) {
34+ resultSize ++;
35+ }
36+ }
37+
38+ return resultSize ;
39+ }
40+ }
You canโt perform that action at this time.
0 commit comments