Divide and Conquer
来源:互联网 发布:隐藏手机号打电话软件 编辑:程序博客网 时间:2024/06/05 22:54
1. Kth Largest Element in an Array:
当寻找第K大的数时,我们可以用quickselect 方法。
1)The basic idea is to use Quick Select algorithm to partition the array with pivot:
Put numbers < pivot to pivot's leftPut numbers > pivot to pivot's right
2)then:
if indexOfPivot == k, return A[k]else if indexOfPivot < k, keep checking left part to pivotelse if indexOfPivot > k, keep checking right part to pivot
3)Time complexity = O(n)
Discard half each time: n+(n/2)+(n/4)..1 = n + (n-1) = O(2n-1) = O(n), because n/2+n/4+n/8+..1=n-1.
2. 在每行,每列升序排序的数组中查找值。
1). i 定位行,指向每行的第一个元素;j 定位列,开始指向每行的最后一个元素。
2). 遍历每行的最后一个元素定位好行后,用j指针从后往前遍历。
class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length == 0) return false; int l = matrix.length; int r = matrix[0].length; int i = 0; int j = r - 1; while( i < l && j >= 0){ if(matrix[i][j] == target) return true; else if(matrix[i][j] < target) i++; else{ j--; } } return false; }}
阅读全文
0 0
- Divide and conquer algorithm
- Divide-and-Conquer
- 分治 Divide and Conquer
- divide-and-conquer
- algorithm:divide and conquer
- Divide and Conquer
- Divide and Conquer
- Divide and conquer
- divide and conquer
- Divide and Conquer
- [leetcode] Divide and Conquer
- Divide and Conquer
- Divide and Conquer
- Divide and conquer 总结
- leetcode-Divide and Conquer
- LeetCode--Divide and Conquer
- Divide and Conquer
- Divide-and-Conquer
- SPV中如何利用默克尔树证明某个交易是否存在
- 平衡二叉树java实现
- pat 1134. Vertex Cover (25) 思维
- python-pcm音频播放器
- HOW2J——JAVA基础——百万富翁
- Divide and Conquer
- 【NodeJS】高阶函数是什么鬼??
- hnust 1607 Sharkovski’s Ordering(规律,排序)
- 【机器学习】sklearn.metrics绩效指标实例
- 习题3.3
- 异步任务神器 Celery 快速入门教程
- (5)HBase 1.2.6伪分布安装
- 动态规划
- python3之1007.素数对猜想 (20分)