随笔分类 -  binary search

160 Find K Closest Elements
摘要:Solution 1 : Log n + k http://www.cnblogs.com/grandyang/p/7519466.html https://shineboy2013.github.io/2018/01/28/lee-658/ // correct class Solution { 阅读全文

posted @ 2018-12-06 21:53 猪猪🐷

162. Find Peak Element
摘要:A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return 阅读全文

posted @ 2018-12-06 20:06 猪猪🐷

392. Is Subsequence
摘要:0 3 5 7 Prev = 4 Smallest larger or equal is 5 If prev = 8, return -1 阅读全文

posted @ 2018-11-09 11:25 猪猪🐷

744. Find Smallest Letter Greater Than Target
摘要:一开始这个规则没看到, Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'. 把规则都弄清楚后, 还是走例子, Given a 阅读全文

posted @ 2018-11-09 10:59 猪猪🐷

852. Peak Index in a Mountain Array
摘要:Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[ 阅读全文

posted @ 2018-11-09 10:56 猪猪&#128055;

374. Guess Number Higher or Lower
摘要:We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is highe... 阅读全文

posted @ 2018-11-09 10:55 猪猪&#128055;

287. Find the Duplicate Number
摘要:这个解法用 count 来 数 数组中一共有多少位在mid value 之前, 这样就不用 sort 这个array 了 如果本来有两个在前面, 但是 数出来三个, 就说明 多余的那个 就在前面, otherwise, 在后面 This solution is based on binary sea 阅读全文

posted @ 2018-11-09 10:53 猪猪&#128055;

436. Find Right Interval
摘要:Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is o... 阅读全文

posted @ 2018-11-09 10:30 猪猪&#128055;

4. Median of Two Sorted Arrays
摘要:这个题的解法就是在较短的 那个 array 上 做 binary search 如果 较短的 这半需要增长点, 那么使 left = mid , 如果 这段 需要短一点, 那么 right = mid 判断需要增长还是 变短 是根据 阅读全文

posted @ 2018-11-09 10:26 猪猪&#128055;

774. Minimize Max Distance to Gas Station
摘要:On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., stations[N-1], where N = stations.length. Now, we add K more gas stations so that D, the maximum distance... 阅读全文

posted @ 2018-11-09 06:40 猪猪&#128055;

528. Random Pick with Weight
摘要:Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1. 1 psum = new ArrayLi... 阅读全文

posted @ 2018-11-08 16:16 猪猪&#128055;

354. Russian Doll Envelopes
摘要:lamda expression and n^2 longest increasing subsequence int[] = width, height lamda expression and binary search in longest increasing subsequence 阅读全文

posted @ 2018-11-08 01:40 猪猪&#128055;

215. Kth Largest Element in an Array
摘要:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Examp... 阅读全文

posted @ 2018-11-07 04:58 猪猪&#128055;

300. Longest Increasing Subsequence
摘要:Smallest larger than 4 is 5 1 3 5 7 4 1 3 4 7 这道题,遇到新的数, 如果新的数比队尾的数还要大, 那么就把新的数加到队尾, 如果新的数等于或者小于队尾的数, 那么就在队列中找到比第一个比新的数大的最小的数, smallest larger , 这里用到了 阅读全文

posted @ 2018-11-06 07:32 猪猪&#128055;

774. Minimize Max Distance to Gas Station
摘要:On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., stations[N-1], where N = stations.length. Now, we add K more gas stations so that D, the maximum distanc... 阅读全文

posted @ 2018-09-25 04:23 猪猪&#128055;

50. Pow(x, n)
摘要:Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output:... 阅读全文

posted @ 2018-09-20 18:16 猪猪&#128055;

最左侧的1
摘要:类似如下grid:每个横⾏行行只要出现1,右边的数字都会是1。例例⼦子中的最左侧的1是倒数第⼆二⾏行行第⼆二列列的那 个。
000011
001111 000001 001111 011111 000111 public int[] leftMostOne(int[][] grid){ // col firCol = getLeft(grid[0]); ... 阅读全文

posted @ 2018-08-28 20:12 猪猪&#128055;

34 Find First and Last Position of Element in Sorted Array
摘要:这个也是, 得自己走例子, 才知道什么时候需要post processing , 什么时候不需要, 这个题一个问不需要, 第二问就需要了 所以不要背答案, 要真的理解, 自己走例子 Your algorithm's runtime complexity must be in the order of 阅读全文

posted @ 2018-08-10 14:47 猪猪&#128055;

268 Missing Number
摘要:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Example 2: 阅读全文

posted @ 2018-08-09 18:40 猪猪&#128055;

29. Divide Two Integers
摘要:Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by divisor. The integer division sh... 阅读全文

posted @ 2018-08-09 17:21 猪猪&#128055;

导航