Arrays.binarySearch
来源:互联网 发布:android源码编译后rom 编辑:程序博客网 时间:2024/05/09 18:20
Take Longest Increasing Subsequence as example
The question is :
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
Code:
public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; int len = 0; for(int x : nums) { int i = Arrays.binarySearch(dp, 0, len, x); if(i < 0) i = -(i + 1); dp[i] = x; if(i == len) len++; } return len;}
BinarySearch’s Explanations:
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the sort(int[], int, int) method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.**Parameters:**a - the array to be searchedfromIndex - the index of the first element (inclusive) to be searchedtoIndex - the index of the last element (exclusive) to be searchedkey - the value to be searched for**Returns:**index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
My Explanation:
Its is easy to understand:
1. when find the key in a[], then return that index
2. when cannot find the key exactly, then return the -(insertion point + 1)
the insertion point is: the first element in the range greater than the key!
like the follow picture, I need to find 6, and fromindex=1, endindex=4. there is no 6 in the array. so the insertion point is 8, which is the first element greater than 6. so the return value is 5. Because before insert 6, 8’s index is 4. and return -(insertion point + 1) = -5;
3. when cannot find the key && key is greater than the largest element in the range: also return -(insertion point + 1)
but now the insertion point is toindex
4. when cannot find the key && key is greater than the largest element in the range: insertion point is fromindex
- Arrays.binarySearch()
- Arrays.binarySearch();
- Arrays.binarySearch
- JAVA Arrays.binarySearch
- Arrays.binarySearch用法
- JAVA Arrays.binarySearch
- JAVA Arrays.binarySearch
- Arrays.binarySearch(二分法检索)
- Arrays.binarySearch() 方便记忆版
- Arrays.binarySearch()返回值问题
- Arrays.binarySearch(int[] a, int key)
- java API------Arrays类binarySearch方法
- Arrays类中sort,binarySearch,fill实例
- Arrays.binarySearch(int[] a, int key)
- Arrays.binarySearch以及Longest Increasing Subsequence
- BinarySearch
- BinarySearch
- binarySearch
- Android开发-Fragment的生命周期和重建问题解决方案
- [分块][哈希] [Romanian IOI 2017 Selection #6] Jolteon && [LOJ #6187] Odd
- IPV4须知
- Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPointerExceptio
- 解决fatal error: bsoncxx/json.hpp: 没有那个文件或目录
- Arrays.binarySearch
- python 列表
- 使用c++11新特性实现线程池
- 微信支付类
- 最近比较忙
- js高级程序设计笔记——13章事件
- Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
- Gson笔记
- 学习shiro代码报错:Line argument must contain a key and a value. Only one string token was found.