-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d49d49
commit 8d3745d
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Time Complexity: O(nlogn) | ||
Space Complexity: O(nlogn) | ||
*/ | ||
class Solution { | ||
|
||
int[] tree; | ||
int L = 1; | ||
|
||
public int lengthOfLIS(int[] nums) { | ||
init(nums); | ||
ArrayList<Tuple> tuples = new ArrayList<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
tuples.add(new Tuple(i, nums[i])); | ||
} | ||
|
||
Collections.sort(tuples, (a, b) -> { | ||
if (a.val == b.val) { | ||
return b.ref - a.ref; // 2순위 : ref 내림차순 | ||
} else { | ||
return a.val - b.val; // 1순위 : val 오름차순 | ||
} | ||
}); | ||
|
||
int ans = 0; | ||
for (int i = 0; i < nums.length; i++) { | ||
int curr = getMax(0, tuples.get(i).ref - 1) + 1; | ||
ans = Math.max(ans, curr); | ||
insert(tuples.get(i).ref, curr); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
public class Tuple { | ||
public int ref; | ||
public int val; | ||
|
||
public Tuple(int ref, int val) { | ||
this.ref = ref; | ||
this.val = val; | ||
} | ||
} | ||
|
||
public void init(int[] nums) { | ||
while (L < nums.length) { | ||
L *= 2; | ||
} | ||
|
||
tree = new int[L * 2]; | ||
|
||
for (int i = 1; i < L * 2; i++) { | ||
tree[i] = 0; | ||
} | ||
} | ||
|
||
public void insert(int idx, int v) { | ||
int i = idx + L; | ||
tree[i] = v; | ||
i /= 2; | ||
while (i >= 1) { | ||
tree[i] = Math.max(tree[i * 2], tree[i * 2 + 1]); | ||
i /= 2; | ||
} | ||
} | ||
|
||
public int getMax(int l, int r) { | ||
int i = l + L; | ||
int j = r + L; | ||
int ret = 0; | ||
while (i <= j) { | ||
if (i % 2 == 1) { | ||
ret = Math.max(ret, tree[i++]); | ||
} | ||
if (j % 2 == 0) { | ||
ret = Math.max(ret, tree[j--]); | ||
} | ||
i /= 2; | ||
j /= 2; | ||
} | ||
|
||
return ret; | ||
} | ||
} |