
Sorting
文章平均质量分 58
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Two City Scheduling
A company is planning to interview2npeople. Given the arraycostswherecosts[i] = [aCosti, bCosti],the cost of flying theithperson to cityaisaCosti, and the cost of flying theithperson to citybisbCosti.Returnthe minimum cost to fly every p...原创 2022-04-06 10:38:12 · 181 阅读 · 0 评论 -
Minimum Absolute Difference
Given an array ofdistinctintegersarr, find all pairs of elements with the minimum absolute difference of any two elements.Return a list of pairs in ascending order(with respect to pairs), each pair[a, b]followsa, bare fromarr a < b b - aeq...原创 2022-03-20 01:59:23 · 366 阅读 · 0 评论 -
Count Words Obtained After Adding a Letter
You are given two0-indexedarrays of stringsstartWordsandtargetWords. Each string consists oflowercase English lettersonly.For each string intargetWords, check if it is possible to choose a string fromstartWordsand perform aconversion operation...原创 2022-01-09 12:39:08 · 310 阅读 · 0 评论 -
Minimum Swaps to Arrange a Binary Grid
Given annx nbinarygrid, in one step you can choose twoadjacent rowsof the grid and swap them.A grid is said to bevalidif all the cells above the main diagonal arezeros.Returnthe minimum number of stepsneeded to make the grid valid, or-1if ...原创 2020-08-03 02:03:05 · 194 阅读 · 0 评论 -
Sorting (Merge Sort, Rainball Sort, quick select) 总结
Merge k Sorted Lists(这题是用PQ,或者merge sort都可以做,关于第K大的问题,参考: PriorityQueue 第K大问题总结)Sort an Array ( 重点掌握merge sort和quick sort,因为两者可以演变为很多其他的算法,divide conquer, quick select)Merge IntervalsSort ColorsSort ListConvert Sorted Array to Binary Searc...原创 2020-07-05 07:08:51 · 464 阅读 · 0 评论 -
Remove Sub-Folders from the Filesystem
Given a list of folders, remove all sub-folders in those folders and return inany orderthe folders after removing.If afolder[i]is located withinanotherfolder[j], it is called asub-folderof it.The format of a path isone or more concatenated str...原创 2020-06-10 12:06:56 · 296 阅读 · 0 评论 -
Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), wherehis the height of the person andkis the number of people in front of this person who have a height greater than or equal toh. Wri...原创 2020-06-08 12:11:12 · 170 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is9534330.Note: The result may be very原创 2015-01-15 16:10:25 · 496 阅读 · 0 评论 -
Sort an Array
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm.ExampleExample 1: Input: [3, 2, 1, 4, 5] Output: [1, 2, 3, 4, 5] E...原创 2019-12-27 13:36:29 · 245 阅读 · 0 评论 -
Pancake Sorting
Given anunsorted array, sort the given array. You are allowed to do only following operation on array.flip(arr, i): Reverse array from 0 to i Unlike a traditional sorting algorithm, which attem...原创 2019-11-04 03:44:01 · 213 阅读 · 0 评论 -
Sort Colors
Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0,...原创 2014-12-03 09:29:34 · 473 阅读 · 0 评论 -
Global and Local Inversions
We have some permutationAof[0, 1, ..., N - 1], whereNis the length ofA.The number of (global) inversions is the number ofi < jwith0 <= i < j < NandA[i] > A[j].The number ...原创 2019-04-07 12:05:15 · 160 阅读 · 0 评论 -
Merge Sort Array and Merge Sort Linked List
Merge Sort Array: 看完stanford的 CS106B的video,https://www.youtube.com/watch?v=LlNawf0JeF0&list=PLnfg8b9vdpLn9exZweTJx44CII1bYczuk&index=55醍醐灌顶;public class Solution { /** * @param A:...原创 2018-12-26 09:02:40 · 830 阅读 · 0 评论 -
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note原创 2014-01-02 07:21:44 · 479 阅读 · 0 评论 -
Wiggle Sort
Given an unsorted array nums, reorder it in-place such thatnums[0] = nums[2] .For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is[1, 6, 2, 5, 3, 4].思路:这题主要在观察规律,并且证明算法的可行性。n原创 2016-07-20 12:26:15 · 352 阅读 · 0 评论 -
Insertion Sort Integer Array & Insertion Sort Linked List
Sort Integer Array using Insertion sort.//******************************************************************************************************** /* Insertion Sort 原理:就是前面的sort部分全部是相对值,从后面拿一个元素,然...原创 2016-07-29 13:26:12 · 465 阅读 · 0 评论 -
Quick Sort
The basic step of sorting an array are as follows:Select a pivot, normally the middle one From both ends, swap elements and make left elements < pivot and all right > pivot Recursively so...原创 2016-10-15 09:56:39 · 722 阅读 · 1 评论 -
Reverse Words in a String II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always原创 2015-02-04 03:48:14 · 456 阅读 · 0 评论