
Two pointers
文章平均质量分 67
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Shortest Subarray to be Removed to Make Array Sorted
Given an integer arrayarr, remove a subarray (can be empty) fromarrsuch that the remaining elements inarrarenon-decreasing.Returnthe length of the shortest subarray to remove.Asubarrayis a contiguous subsequence of the array.Example 1:Inp...原创 2022-04-06 11:51:31 · 298 阅读 · 0 评论 -
Is Subsequence
Given two stringssandt, returntrueifsis asubsequenceoft, orfalseotherwise.Asubsequenceof a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative posit...原创 2022-04-01 01:41:03 · 407 阅读 · 0 评论 -
Product of Two Run-Length Encoded Arrays
Run-length encodingis a compression algorithm that allows for an integer arraynumswith many segments ofconsecutive repeatednumbers to be represented by a (generally smaller) 2D arrayencoded. Eachencoded[i] = [vali, freqi]describes theithsegment o...原创 2022-03-21 12:44:38 · 195 阅读 · 0 评论 -
Palindromic Substrings
Given a strings, returnthe number ofpalindromic substringsin it.A string is apalindromewhen it reads the same backward as forward.Asubstringis a contiguous sequence of characters within the string.Example 1:Input: s = "abc"Output: 3Exp...原创 2022-01-18 13:11:46 · 366 阅读 · 0 评论 -
Minimum Swaps to Group All 1‘s Together
Given abinary arraydata, returnthe minimum number of swaps required to group all1’s present in the array together inany placein the array.Example 1:Input: data = [1,0,1,0,1]Output: 1Explanation: There are 3 ways to group all 1's together:[1,1...原创 2022-01-09 14:17:38 · 237 阅读 · 0 评论 -
Split Two Strings to Make Palindrome
You are given two stringsaandbof the same length. Choose an index and split both stringsat the same index, splittingainto two strings:aprefixandasuffixwherea = aprefix+ asuffix, and splittingbinto two strings:bprefixandbsuffixwhereb = b...原创 2020-10-11 12:36:34 · 267 阅读 · 0 评论 -
Minimum Deletion Cost to Avoid Repeating Letters
Given astringsand an array of integerscostwherecost[i]is the cost ofdeletingthe characteriins.Return the minimum cost of deletionssuch that there are no two identical letters next to each other.Notice that you will delete the chosen charac...原创 2020-09-06 13:20:43 · 683 阅读 · 0 评论 -
Positions of Large Groups
In a stringSof lowercase letters, these letters form consecutive groups of the same character.For example, a string likeS = "abbxxxxzyy"has the groups"a","bb","xxxx","z"and"yy".Call a grouplargeif it has 3 or more characters. We would like...原创 2020-06-29 03:36:28 · 160 阅读 · 0 评论 -
Valid Word Abbreviation
Given anon-emptystringsand an abbreviationabbr, return whether the string matches with the given abbreviation.A string such as"word"contains only the following valid abbreviations:["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1...原创 2020-06-29 02:51:36 · 216 阅读 · 0 评论 -
Swap Adjacent in LR String
In a string composed of'L','R', and'X'characters, like"RXXLRXRXL", a move consists of either replacing one occurrence of"XL"with"LX", or replacing one occurrence of"RX"with"XR". Given the starting stringstartand the ending stringend, returnT...原创 2020-06-20 11:07:33 · 173 阅读 · 0 评论 -
Fruit Into Baskets
In a row of trees, thei-th treeproducesfruit with typetree[i].Youstart at any treeof your choice, then repeatedly perform the following steps:Add one piece of fruit from this tree to your baskets. If you cannot, stop. Move to the next tree to t...原创 2020-06-20 05:51:39 · 243 阅读 · 0 评论 -
Longest Mountain in Array
Let's call any (contiguous) subarray B (of A)amountainif the following properties hold:B.length >= 3 There exists some0 < i< B.length - 1such thatB[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1](Note th...原创 2020-06-17 10:11:34 · 176 阅读 · 0 评论 -
3Sum Smaller
Given an array of n integers nums and a target, find the number of index tripletsi, j, k with 0 that satisfy the conditionnums[i] + nums[j] + nums[k] .For example, given nums = [-2, 0, 1, 3], a原创 2016-08-17 11:46:23 · 384 阅读 · 0 评论 -
Candy Crush
This question is about implementing a basic elimination algorithm for Candy Crush.Given a 2D integer arrayboardrepresenting the grid of candy, different positive integersboard[i][j]represent different types of candies. A value ofboard[i][j] = 0repr...原创 2020-06-09 07:30:11 · 555 阅读 · 0 评论 -
Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.Note原创 2020-06-08 09:04:15 · 215 阅读 · 0 评论 -
Shuffle the Array
Given the arraynumsconsisting of2nelements in the form[x1,x2,...,xn,y1,y2,...,yn].Return the array in the form[x1,y1,x2,y2,...,xn,yn].Example 1:Input: nums = [2,5,1,3,4,7], n = 3Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3...原创 2020-06-07 12:18:00 · 334 阅读 · 0 评论 -
Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical ord原创 2020-06-05 13:50:15 · 193 阅读 · 0 评论 -
Compare Version Numbers
Compare two version numbersversion1andversion1.Ifversion1>version2return 1, ifversion1version2return -1, otherwise return 0.You may assume that the version strings are non-empty and cont原创 2014-12-17 06:36:39 · 769 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"o原创 2014-12-06 12:03:02 · 394 阅读 · 0 评论 -
String Compression
Given an array of characters, compress itin-place.The length after compression must always be smaller than or equal to the original array.Every element of the array should be acharacter(not int) of length 1.After you are donemodifying the input a...原创 2020-05-31 08:15:43 · 170 阅读 · 0 评论 -
Permutation in String
Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the second string.Example 1:Input: s1 = "ab" s2 = "eidbaooo"Output: TrueExp...原创 2020-05-30 06:07:02 · 176 阅读 · 0 评论 -
Shortest Way to Form String
From any string, we can form asubsequenceof that string by deleting some number of characters (possibly no deletions).Given two stringssourceandtarget, return the minimum number of subsequences ofsourcesuch that their concatenation equalstarget. ...原创 2020-05-22 12:44:38 · 238 阅读 · 0 评论 -
Design Circular Queue
用circular array去implement queue,这个题目被面到两次了。昨天onsite被问到了。今天看了下正确答案,确实不难,是自己复习不到位。算法就是:用front 和rear分别表示头和尾,最重要的考点就是用count来表示里面有多少个元素。这样 front和rear只需要一直向前走就可以了。每次enqueue和dequeue的时候,都用size,也就是count来...原创 2016-10-14 05:39:58 · 1560 阅读 · 0 评论 -
Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example 1:Input: [2,2,3,4]Output: 3Explanation:Valid combinat原创 2020-05-19 14:22:55 · 118 阅读 · 0 评论 -
Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example 1:Input: [2,2,3,4]Output: 3Explanation:Valid combinat原创 2020-05-19 13:26:31 · 153 阅读 · 0 评论 -
同向双指针高阶题总结
有一类题,看起来很像同向双指针,但是不完全是,一般都是求exactly K同向双指针教会了我们如何求at most K, 现在我们要求exactly K. 那么我们可以转换为 exactly K = atMost(K) - atMost(K - 1);这类题有好多,如下:Substrings of size K with K distinct charsGiven a stringsand an intk, return an int representing the number o...原创 2020-05-17 08:42:09 · 484 阅读 · 0 评论 -
Count Number of Nice Subarrays
Given an array of integersnumsand an integerk. Asubarrayis calledniceif there arekodd numbers on it.Return the number ofnicesub-arrays.Example 1:Input: nums = [1,1,2,1,1], k = 3Output: 2Explanation: The only sub-arrays with 3 odd number...原创 2020-05-17 08:37:27 · 289 阅读 · 0 评论 -
Intersection of Three Sorted Arrays
Given three integer arraysarr1,arr2andarr3sortedinstrictly increasingorder, return a sorted array ofonlytheintegers that appeared inallthree arrays.Example 1:Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]Output: [1,5]...原创 2020-05-14 11:08:14 · 237 阅读 · 0 评论 -
Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.Example 1:Given s = "hello", return "holle".Example 2:Given s = "leetcode", return "leotcede".思路:主原创 2016-07-06 12:52:01 · 268 阅读 · 0 评论 -
Is Subsequence
Given a stringsand a stringt, check ifsis subsequence oft.You may assume that there is only lower case English letters in bothsandt.tis potentially a very long (length ~= 500,000) string, andsis a short string (<=100).A subsequence of a...原创 2020-05-11 01:11:54 · 209 阅读 · 1 评论 -
Long Pressed Name
Your friend is typing hisnameinto a keyboard. Sometimes, when typing a characterc, the key might getlong pressed, and the character will be typed 1 or more times.You examine thetypedcharacters of the keyboard. ReturnTrueif it is possible that i...原创 2020-05-10 08:41:37 · 182 阅读 · 0 评论 -
Remove Outermost Parentheses
A valid parentheses string is either empty(""),"(" + A + ")", orA + B, whereAandBare valid parentheses strings, and+represents string concatenation. For example,"","()","(())()", and"(()(()))"are all valid parentheses strings.A valid paren...原创 2020-05-10 07:43:02 · 132 阅读 · 0 评论 -
Best Meeting Point
A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calcu原创 2016-08-27 10:32:30 · 585 阅读 · 0 评论 -
Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:上下左右替换,layer 注意offset = i - first;是算与前面的差距。然后就原创 2013-12-31 10:34:50 · 612 阅读 · 0 评论 -
Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consist ...原创 2020-05-01 14:16:20 · 219 阅读 · 0 评论 -
Container With Most Water
Given n non-negative integers a1, a2, ...,an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of linei is at (i, ai) and (i, 0). Find原创 2014-02-10 12:03:13 · 462 阅读 · 0 评论 -
Meeting Scheduler
Given the availability time slots arraysslots1andslots2of two people and a meeting durationduration, return theearliest time slotthat works for both of them and is of durationduration.If the...原创 2020-04-28 13:12:21 · 464 阅读 · 0 评论 -
Subarrays with K Different Integers
Given an arrayAof positive integers, call a (contiguous, not necessarily distinct) subarray ofAgoodif the number of different integers in that subarray is exactlyK.(For example,[1,2,3,1,2]ha...原创 2020-04-27 11:54:17 · 422 阅读 · 0 评论 -
Substrings of size K with K distinct chars
Given a stringsand an intk, return an int representing the number of substrings (not unique) ofswith exactlykdistinct characters. If the given string doesn't havekdistinct characters, return ...原创 2020-04-27 11:44:10 · 315 阅读 · 0 评论 -
Sort Transformed Array
Given asortedarray of integersnumsand integer valuesa,bandc. Apply a quadratic function of the form f(x) =ax2+bx+cto each elementxin the array.The returned array must be insorted or...原创 2020-04-21 14:55:06 · 207 阅读 · 0 评论