[] 300 최장 증가 부분 수열 LeetCode가 긴 시퀀스 (중간) 상승 (JAVA)
주제 주소 : https://leetcode.com/problems/longest-increasing-subsequence/
주제 설명 :
정수의 정렬되지 않은 배열을 지정해, 긴 증가 시퀀스의 길이를 찾을 수 있습니다.
예:
Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
노트 :
1. There may be more than one LIS combination, it is only necessary for you to return the length.
2. Your algorithm should run in O(n2) complexity.
후속 : 당신은 O로 개선 (N N 로그) 시간의 복잡성을 수 있을까요?
효과에 따라
주어진 정수 배열의 장애, 가장 긴 발견 상승 순서의 길이.
문제 해결 접근 방식을
도 1을 참조하면, 순서대로 대기열 목록 유지
요소가 마지막보다 크면 명령 큐에 추가되고, 새로운 요소가 순서화 된 큐에 대응하는 위치를 교체 2
class Solution {
public int lengthOfLIS(int[] nums) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
insert(list, nums[i]);
}
return list.size();
}
public void insert(List<Integer> list, int num) {
if (list.size() == 0 || list.get(list.size() - 1) < num) {
list.add(num);
return;
}
int start = 0;
int end = list.size() - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (list.get(mid) == num) {
start = mid;
break;
} else if (list.get(mid) > num) {
end = mid - 1;
} else {
start = mid + 1;
}
}
list.set(start, num);
}
}
실행이되면 1 MS가 자바에 제출하는 모든 사용자의 94.83 %를 이길
메모리 소비 : 37.7 MB, 자바에 제출하는 모든 사용자의 5.05 %를 이길