while (!queue.isEmpty()) { TreeNodecurNode= queue.poll(); inti= nodeMap.get(curNode);
res.computeIfAbsent(i, k -> newArrayList<>()).add(curNode.val); if (curNode.left != null) { queue.add(curNode.left); nodeMap.put(curNode.left, i - 1); }
if (curNode.right != null) { queue.add(curNode.right); nodeMap.put(curNode.right, i + 1); } }
Suppose you are at a party with n people labeled from 0 to n - 1 and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know the celebrity, but the celebrity does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is ask questions like: “Hi, A. Do you know B?” to get information about whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b) that tells you whether A knows B. Implement a function int findCelebrity(n). There will be exactly one celebrity if they are at the party.
Return the celebrity’s label if there is a celebrity at the party. If there is no celebrity, return -1.
Example 1:
Input: graph = [[1,1,0],[0,1,0],[1,1,1]] Output: 1 Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2 know him but 1 does not know anybody.
Example 2:
Input: graph = [[1,0,1],[1,1,0],[0,1,1]] Output: -1 Explanation: There is no celebrity.
/* The knows API is defined in the parent class Relation. boolean knows(int a, int b); */
publicclassSolutionextendsRelation { publicintfindCelebrity(int n) { intresult=0; for (inti=1; i < n; i++) { if (knows(result, i)) result = i; } for (inti=0; i < n; i++) { if (i == result) continue; if (knows(result, i) || !knows(i, result)) return -1; } return result; } }
You are given an integer array ribbons, where ribbons[i] represents the length of the ith ribbon, and an integer k. You may cut any of the ribbons into any number of segments of positive integer lengths, or perform no cuts at all.
For example, if you have a ribbon of length 4, you can: Keep the ribbon of length 4, Cut it into one ribbon of length 3 and one ribbon of length 1, Cut it into two ribbons of length 2, Cut it into one ribbon of length 2 and two ribbons of length 1, or Cut it into four ribbons of length 1. Your goal is to obtain k ribbons of all the same positive integer length. You are allowed to throw away any excess ribbon as a result of cutting.
Return the maximum possible positive integer length that you can obtain k ribbons of, or 0 if you cannot obtain k ribbons of the same length.
Example 1:
Input: ribbons = [9,7,5], k = 3 Output: 5 Explanation:
Cut the first ribbon to two ribbons, one of length 5 and one of length 4.
Cut the second ribbon to two ribbons, one of length 5 and one of length 2.
Keep the third ribbon as it is.
Now you have 3 ribbons of length 5.
Example 2:
Input: ribbons = [7,5,9], k = 4 Output: 4 Explanation:
Cut the first ribbon to two ribbons, one of length 4 and one of length 3.
Cut the second ribbon to two ribbons, one of length 4 and one of length 1.
Cut the third ribbon to three ribbons, two of length 4 and one of length 1.
Now you have 4 ribbons of length 4.
Example 3:
Input: ribbons = [5,7,9], k = 22 Output: 0 Explanation: You cannot obtain k ribbons of the same positive integer length.
Constraints:
1 <= ribbons.length <= 105
1 <= ribbons[i] <= 105
1 <= k <= 109
题解
采用二分法求解,绳子的最小长度为 0,最大长度为绳长最大值。根据当前绳长 m 所能得到的绳子段数总和与 k 进行比较来判断边界是否满足条件。
classSolution { publicintlongestRepeatingSubstring(String s) { if (s == null || s.length() < 2) return0;
intl=0, r= s.length() - 1; while (l < r) { intm= l + (r - l + 1) / 2; if (exist(s, m)) l = m; else r = m - 1; }
return l; }
privatebooleanexist(String s, int m) { /* 存储字符串的 hash 值, 节省空间 */ Set<Integer> set = newHashSet<>();
for (inti=0; i <= s.length() - m; i++) { intj= i + m - 1; Stringsub= s.substring(i, j + 1); inthash= sub.hashCode(); if (set.contains(hash)) returntrue; else set.add(hash); }
Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal tok that has the maximum average value. And you need to output the maximum average value.
Input: [1, 12, -5, -6, 50, 3], k = 4 Output: 12.75 Explanation: when length is 5, maximum average value is 10.8, when length is 6, maximum average value is 9.16667. Thus return 12.75.
Note:
1 <= k <= n <= 10,000.
Elements of the given array will be in range [-10,000, 10,000].
The answer with the calculation error less than 10-5 will be accepted.
题解
本题的技巧性较强,可采用滑动窗口加二分法求解,用猜平均值的方式逼近真实值,实现的关键在于找到二分的条件。对于一个数组来说,其所有可能的子数组的最大平均值必介于最大值和最小值之间。取 mid = (min + max) / 2.0,对数组依次求长度大于等于 k 的所有可能的子数组的平均值。若平均值序列中有值大于等于 mid,则最大平均值必位于 [mid, max] 区间(即 l = mid);若平均值序列中所有值都小于 mid,则最大平均值必位于 [min, mid] 上(即 r = mid)。其中,在计算平均值时,可将计算简化为 xi - mid 的和与 0 进行比较。
另一个 trick 是在计算数组所有可能的平均值时,不必对所有长度大于 k 的子数组再次进行遍历,只需在前 k 个元素和的基础上向右扩增,再减去前几项的和既可得到新的子数组的和。如 k = 4,当滑动窗口中包含 0 ~ 5 六个元素时,如果想得到长度为 5 和 4 的子数组的和,只需分别减去第 0 和第 0 ~ 1 个元素即可。而由于是在 mid 两侧判断是否存在子数组满足条件,故直接比较子数组的最大和与最小的前缀和的大小既可。
publicclassSolution { publicdoublefindMaxAverage(int[] nums, int k) { intmin= Integer.MAX_VALUE, max = Integer.MIN_VALUE; for (int num : nums) { min = Math.min(min, num); max = Math.max(max, num); }
doublel= min, r = max; while (r - l > 0.00001) { doublemid= (l + r) / 2.0; if (exist(nums, k, mid)) l = mid; else r = mid; }
return l; }
privatebooleanexist(int[] nums, int k, double mid) { doublesum=0, prev = 0, minPrev = 0; for (inti=0; i < k; i++) sum += nums[i] - mid; if (sum >= 0) returntrue;
for (inti= k; i < nums.length; i++) { sum += nums[i] - mid; prev += nums[i - k] - mid; minPrev = Math.min(prev, minPrev); // 比较 子数组的最大和 与 最小的前缀和 的大小 if (sum - minPrev >= 0) returntrue; }
Given a list of scores of different students, return the average score of each student’s top five scores in the order of each student’s id.
Each entry items[i] has items[i][0] the student’s id, and items[i][1] the student’s score. The average score is calculated using integer division.
Example 1:
1 2 3 4 5
Input: [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]] Output: [[1,87],[2,88]] Explanation: The average of the student withid = 1is87. The average of the student withid = 2is88.6. But with integer division their average converts to 88.
Note:
1 <= items.length <= 1000 items[i].length == 2 The IDs of the students is between 1 to 1000 The score of the students is between 1 to 100 For each student, there are at least 5 scores