
Binary Search
文章平均质量分 63
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Cutting Ribbons
You are given an integer arrayribbons, whereribbons[i]represents the length of theithribbon, and an integerk. You may cut any of the ribbons into any number of segments ofpositive integerlengths, or perform no cuts at all.For example, if you have...原创 2022-03-28 01:55:53 · 350 阅读 · 0 评论 -
Find Smallest Letter Greater Than Target
Given a characters arraylettersthat is sorted innon-decreasingorder and a charactertarget, returnthe smallest character in the array that is larger thantarget.Notethat the letters wrap around.For example, iftarget == 'z'andletters == ['a', '...原创 2022-02-12 01:03:48 · 192 阅读 · 0 评论 -
Kth Missing Positive Number
Given an arrayarrof positive integerssorted in astrictly increasing order, and an integerk.Find thekthpositive integer that is missing from this array.Example 1:Input: arr = [2,3,4,7,11], k = 5Output: 9Explanation: The missing positive inte...原创 2022-01-22 03:17:27 · 233 阅读 · 0 评论 -
Find First and Last Position of Element in Sorted Array
Given an array of integersnumssorted in non-decreasing order, find the starting and ending position of a giventargetvalue.Iftargetis not found in the array, return[-1, -1].You mustwrite an algorithm withO(log n)runtime complexity.Example 1:...原创 2022-01-19 05:31:38 · 222 阅读 · 0 评论 -
Minimum Operations to Make the Array K-Increasing
You are given a0-indexedarrayarrconsisting ofnpositive integers, and a positive integerk.The arrayarris calledK-increasingifarr[i-k] <= arr[i]holds for every indexi, wherek <= i <= n-1.For example,arr = [4, 1, 5, 2, 6, 2]is K-...原创 2021-12-19 14:27:13 · 260 阅读 · 0 评论 -
Range Frequency Queries
Design a data structure to find thefrequencyof a given value in a given subarray.Thefrequencyof a value in a subarray is the number of occurrences of that value in the subarray.Implement theRangeFreqQueryclass:RangeFreqQuery(int[] arr)Construc...原创 2021-11-22 13:24:18 · 279 阅读 · 0 评论 -
Longest Increasing Subsequence
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], therefor...原创 2016-08-27 12:41:37 · 608 阅读 · 0 评论 -
Maximum Profit in Job Scheduling
We havenjobs, where every jobis scheduled to be done fromstartTime[i]toendTime[i], obtaining a profitofprofit[i].You're given thestartTime,endTimeandprofitarrays,you need to output the maximum profit you can take such that there are no 2 j...原创 2020-06-28 05:46:15 · 377 阅读 · 0 评论 -
Odd Even Jump
You are given an integer arrayA. Fromsome starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...)jumps in the series are calledodd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are calledeven numbered jumps.You ...原创 2020-06-25 11:56:59 · 287 阅读 · 0 评论 -
Number of Matching Subsequences
Given stringSand adictionary of wordswords, find the number ofwords[i]that is a subsequence ofS.Example :Input: S = "abcde"words = ["a", "bb", "acd", "ace"]Output: 3Explanation: There are three words in words that are a subsequence of S: "a"...原创 2020-06-17 08:18:43 · 203 阅读 · 0 评论 -
Minimum Number of Days to Make m Bouquets
Given an integer arraybloomDay, an integermand an integerk.We need to makembouquets. To make a bouquet,you need to usekadjacent flowersfrom the garden.The garden consists ofnflowers, theithflower will bloom in thebloomDay[i]and then can...原创 2020-06-14 12:22:47 · 324 阅读 · 0 评论 -
Leftmost Column with at Least a One
(This problem is aninteractive problem.)A binary matrix means that all elements are0or1. For eachindividualrow of the matrix, this rowis sorted in non-decreasing order.Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0...原创 2020-06-08 02:54:46 · 297 阅读 · 0 评论 -
Divide Chocolate
You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the arraysweetness.You want to share the chocolate with yourKfriends so you start cutting the chocolate bar intoK+1pieces usingKcuts, each piece con...原创 2020-05-25 07:52:16 · 451 阅读 · 0 评论 -
Koko Eating Bananas
Koko loves to eat bananas. There areNpiles of bananas, thei-thpile haspiles[i]bananas. The guards have gone and will come back inHhours.Koko can decide her bananas-per-hour eating speed ofK. Each hour, she chooses some pile of bananas, and ea...原创 2020-05-19 14:54:16 · 314 阅读 · 0 评论 -
Single Element in a Sorted Array
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactlyonce. Find this single element that appears only once....原创 2020-05-06 13:01:13 · 158 阅读 · 0 评论 -
Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows:I pick a number from1ton. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number is higher or...原创 2020-05-02 13:59:17 · 136 阅读 · 0 评论 -
Find in Mountain Array
(This problem is aninteractive problem.)You may recall that an arrayAis amountain arrayif and only if:A.length >= 3 There exists someiwith0 < i< A.length - 1such that: A[0] &l...原创 2020-05-02 07:08:23 · 184 阅读 · 0 评论 -
Random Pick with Weight
Given an arraywof positive integers, wherew[i]describes the weight of indexi,write a functionpickIndexwhich randomlypicks an indexin proportionto its weight.Note:1 <= w.length <= ...原创 2020-05-23 02:03:31 · 345 阅读 · 0 评论 -
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2020-04-21 13:48:38 · 150 阅读 · 0 评论 -
Time Based Key-Value Store
Create a timebased key-value store classTimeMap, that supports two operations.1.set(string key, string value, int timestamp)Stores thekeyandvalue, along with the giventimestamp.2.get(strin...原创 2020-04-15 06:14:33 · 169 阅读 · 0 评论 -
Peak Index in a Mountain Array
Let's call an arrayAamountainif the following properties hold:A.length >= 3 There exists some0 < i< A.length - 1such thatA[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ....原创 2020-03-09 13:40:14 · 123 阅读 · 0 评论 -
Missing Element in Sorted Array
Given a sorted arrayAofuniquenumbers, find theK-thmissing number starting from the leftmost number of the array.Example 1:Input: A = [4,7,9,10], K = 1Output: 5Explanation: The first m...原创 2020-02-25 15:32:06 · 703 阅读 · 0 评论 -
Binary Search 总结
Find K Closest ElementsGiventarget, a non-negative integerkand an integer arrayAsorted in ascending order, find thekclosest numbers totargetinA, sorted in ascending order by the difference...原创 2020-02-25 14:20:43 · 489 阅读 · 0 评论 -
Find First and Last Position of Element in Sorted Array
Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).If the...原创 2020-02-24 14:06:59 · 165 阅读 · 0 评论 -
二分答案总结
二分答案是binary search里面比较难的题型,也就是,直接求一个最值很难,但是我们可以很简单的在解空间做判定性问题,比如能不能,行不行,大了还是小了,return true or false,从而缩小解空间,记住,这种方法有个前提条件就是有单调递增或者递减的性质,才能用。这也是binary search使用的条件;Wood Cut有一些原木,现在想把这些木头切割成一些长度相同的小段...原创 2020-01-17 01:51:36 · 2567 阅读 · 0 评论 -
Smallest Rectangle Enclosing Black Pixels
An image is represented by a binary matrix with0as a white pixel and1as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and v...原创 2019-12-24 13:52:37 · 173 阅读 · 0 评论 -
Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in ...原创 2016-07-20 14:26:08 · 294 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array II
Suppose an array sorted in ascending order 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.The array may contai...原创 2014-12-01 14:31:32 · 418 阅读 · 0 评论 -
Search for a Range
Given a sorted array ofnintegers, find the starting and ending position of a given target value.If the target is not found in the array, return[-1, -1].ExampleExample 1:Input:[]9Output:...原创 2019-12-23 14:25:02 · 112 阅读 · 0 评论 -
Total Occurrence of Target
Given a target number and an integer array sorted in ascending order. Find the total number of occurrences of target in the array.ExampleExample1:Input: [1, 3, 3, 4, 5] and target = 3, Output:...原创 2019-12-23 13:46:15 · 180 阅读 · 0 评论 -
drop eggs
There is a building ofnfloors. If an egg drops from thekth floor or above, it will break. If it's dropped from any floor below, it will not break.You're given two eggs, Findkwhile minimize the...原创 2019-12-23 10:23:32 · 303 阅读 · 0 评论 -
Find Peak Element II
Given an integer matrixAwhich has the following features :The numbers in adjacent positions are different. The matrix hasnrows andmcolumns. For alli < n,A[i][0] < A[i][1] && ...原创 2019-12-06 14:07:33 · 222 阅读 · 0 评论 -
Copy Books II
Givennbooks and each book has the same number of pages. There arekpersons to copy these books and thei-thperson needstimes[i]minutes to copy a book.Each person can copy any number of books a...原创 2019-12-06 13:40:03 · 420 阅读 · 0 评论 -
Copy Books
Givennbooks and thei-thbook haspages[i]pages. There arekpersons to copy these books.These books list in a row and each person can claim a continous range of books. For example, one copier ca...原创 2019-12-06 13:06:18 · 1853 阅读 · 0 评论 -
Maximum Average Subarray II
Given an array with positive and negative numbers, find themaximum average subarraywhich length should be greater or equal to given lengthk.ExampleExample 1:Input:[1,12,-5,-6,50,3]3Output...原创 2019-12-06 12:31:35 · 345 阅读 · 0 评论 -
Wood Cut
Given n pieces of wood with lengthL[i](integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get...原创 2019-12-06 11:29:57 · 639 阅读 · 0 评论 -
Sqrt(x) II
Implementdouble sqrt(double x)andx >= 0.Compute and return the square root of x.ExampleExample 1:Input: n = 2 Output: 1.41421356Example 2:Input: n = 3Output: 1.73205081Notice...原创 2019-12-04 15:41:17 · 248 阅读 · 0 评论 -
Median of K Sorted Arrays
There areksorted arraysnums. Find the median of the givenksorted arrays.ExampleExample 1:Input:[[1],[2],[3]]Output:2.00Example 2:Input:[[1,1,2],[2,4,8],[3,7,10,20]]Output:3.50...原创 2019-11-27 08:58:03 · 304 阅读 · 0 评论 -
Russian Doll Envelopes
Give a number of envelopes with widths and heights given as a pair of integers(w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the wi...原创 2019-11-22 13:21:37 · 217 阅读 · 0 评论 -
Fast Power
Calculate thean% bwhere a, b and n are all 32bit non-negative integers.ExampleFor 231% 3 = 2For 1001000% 1000 = 0ChallengeO(logn)思想:recursion算一半,然后base case,处理算完一半以后的情况;公式就是 (a*b) %...原创 2019-10-31 12:46:44 · 340 阅读 · 0 评论