You are given two integers n and k and two integer arrays speed and efficiency both of length n. There are n engineers numbered from 1 to n. speed[i] and efficiency[i] represent the speed and efficiency of the i<sup>th</sup> engineer respectively.
Choose at mostk different engineers out of the n engineers to form a team with the maximum performance.
The performance of a team is the sum of their engineers’ speeds multiplied by the minimum efficiency among their engineers.
Return the maximum performance of this team. Since the answer can be a huge number, return it modulo10<sup>9</sup><span> </span>+ 7.
You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attack<sub>i</sub>, defense<sub>i</sub>] represents the properties of the i<sup>th</sup> character in the game.
A character is said to be weak if any other character has both attack and defense levels strictly greater than this character’s attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attack<sub>j</sub><span> </span>> attack<sub>i</sub> and defense<sub>j</sub><span> </span>> defense<sub>i</sub>.
You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and verticalCuts where:
horizontalCuts[i] is the distance from the top of the rectangular cake to the i<sup>th</sup> horizontal cut and similarly, and
verticalCuts[j] is the distance from the left of the rectangular cake to the j<sup>th</sup> vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrayshorizontalCutsandverticalCuts. Since the answer can be a large number, return this modulo10<sup>9</sup><span> </span>+ 7.
You are given an array of words where each word consists of lowercase English letters.
word<sub>A</sub> is a predecessor of word<sub>B</sub> if and only if we can insert exactly one letter anywhere in word<sub>A</sub>without changing the order of the other characters to make it equal to word<sub>B</sub>.
For example, "abc" is a predecessor of "ab<u>a</u>c", while "cba" is not a predecessor of "bcad".
A word chain* *is a sequence of words [word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>] with k >= 1, where word<sub>1</sub> is a predecessor of word<sub>2</sub>, word<sub>2</sub> is a predecessor of word<sub>3</sub>, and so on. A single word is trivially a word chain with k == 1.
Return *the length of the longest possible word chain with words chosen from the given list of *words.
You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the i<sup>th</sup> spell and potions[j] represents the strength of the j<sup>th</sup> potion.
You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at leastsuccess.
Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the i<sup>th</sup> spell.
You are given a 2D integer array stockPrices where stockPrices[i] = [day<sub>i</sub>, price<sub>i</sub>] indicates the price of the stock on day day<sub>i</sub> is price<sub>i</sub>. A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points. One such example is shown below:
You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The i<sup>th</sup> bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags.
Return* the maximum number of bags that could have full capacity after placing the additional rocks in some bags.*
Given an array of points where points[i] = [x<sub>i</sub>, y<sub>i</sub>] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).
The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x<sub>1</sub><span> </span>- x<sub>2</sub>)<sup>2</sup><span> </span>+ (y<sub>1</sub><span> </span>- y<sub>2</sub>)<sup>2</sup>).
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
Solution
此题可以采用快速排序的思想。可以在O(n)时间复杂度里完成排序。
Solution 2
优先级队列,根据每个点距离的平方排序。时间复杂度O(nlogk)。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicint[][] kClosest(int[][] points, int k) { int[][] ret = newint[k][2]; PriorityQueue<int[]> pq = newPriorityQueue<>((a, b) -> squaredDistance(a) - squaredDistance(b)); for(int[] point : points){ pq.add(point); } for(inti=0; i < k; i++){ ret[i] = pq.poll(); } return ret; } privateintsquaredDistance(int[] point){ intx= point[0], y = point[1]; return x * x + y * y; } }
Solution 3
直接排序。时间复杂度为O(nlogn)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { publicint[][] kClosest(int[][] points, int k) { int[][] ret = newint[k][2]; Arrays.sort(points, (a, b) -> squaredDistance(a) - squaredDistance(b)); for(inti=0; i < k; i++){ ret[i] = points[i]; } return ret; } privateintsquaredDistance(int[] point){ intx= point[0], y = point[1]; return x * x + y * y; } }
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.