
ACM-分治与二分查找
文章平均质量分 73
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
求两个等长有序数组的中位数的logN算法 分治法
题目:有两个长为n的非递减数组A和B,把B接在A的后面变成长为2n的数组C。设计算法求C的中位数(第n小数)。思路:O(n)的算法很容易找到,关键是用二分的思想设计logn算法。这题关键是用好a和b数组中脚标和为定值的元素的大小关系。 直观想法是:如果中位数在数组a中,那么若a[m] b [n-m-1],此时比a[m]小的数至少有n个,a[m]不可能为第n小数,偏大更原创 2012-01-11 16:46:40 · 8421 阅读 · 8 评论 -
LeetCode Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST is modifi原创 2015-07-19 15:11:29 · 3514 阅读 · 0 评论 -
LeetCode Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unknown to yo原创 2015-07-27 16:25:57 · 2817 阅读 · 0 评论 -
LeetCode Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is原创 2015-06-15 14:02:42 · 3816 阅读 · 0 评论 -
LeetCode First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.思路分析:原创 2014-12-31 15:07:00 · 3564 阅读 · 0 评论 -
LeetCode Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its inde原创 2014-11-29 04:08:29 · 2288 阅读 · 0 评论 -
LeetCode Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in that cas原创 2015-01-05 06:55:59 · 3934 阅读 · 0 评论 -
LeetCode Search Insert Position
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its inde原创 2014-11-29 03:35:44 · 1830 阅读 · 0 评论 -
LeetCode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).思路分析:这题容易想到O(m+n)的解法,就是先Merge两个数组,然后返原创 2015-02-09 14:40:06 · 1666 阅读 · 0 评论 -
LeetCode Pow(x, n)
LeetCode Pow(x, n) Implement pow(x, n).s原创 2014-10-30 05:41:43 · 2019 阅读 · 0 评论 -
POJ 3636 俄罗斯Nested Dolls 贪心二分 动态规划
首先要区分#include 中的qsort与C++ STL algorithm中的sort,后者只有三个参数且比较函数写法较简单些 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等但是用常规的贪心会超时,用二分优化后不超时,注意对l递增排,对w递减排// 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等// 上面这种解法会超时,用二分优化原创 2010-12-09 21:35:00 · 3342 阅读 · 2 评论 -
LeetCode Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array.思路分析原创 2015-07-27 16:21:30 · 2639 阅读 · 0 评论