LeetCode Weekly Contest 142
又是兩道題目,感覺rank要掉了呀~ 第一道看錯題目了,然后浪費了很長時間,第三道很簡單,思路也有,但是沒時間了。
1093.?Statistics from a Large Sample
We sampled integers between?0?and?255, and stored the results in an array?count:??count[k]?is the number of integers we sampled equal to?k.
Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of?floating point numbers.? The mode is guaranteed to be unique.
(Recall that the median of a sample is:
- The middle element, if the elements of the sample were sorted and the number of elements is odd;
- The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)
?
Example 1:
Input: count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: [1.00000,3.00000,2.37500,2.50000,3.00000]Example 2:
Input: count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: [1.00000,4.00000,2.18182,2.00000,1.00000]?
Constraints:
題目大意:給你一個長度為256的數組count,count[i]代表i出現的次數,求最小值,最大值,平均值,中位數和眾數,然后依次添加到一個double數組中返回。
題目思路:基本上看清題目還有理解這些數學值怎么求,基本上不會錯。
代碼:
class Solution {public double[] sampleStats(int[] count) {int len = 0;double[] res = new double[5];double sum = 0;double minNum = Integer.MAX_VALUE;double maxNum = Integer.MIN_VALUE;double meanNum = 0.0;double modeNum = 0.0;int modeCount = 0;for(int i=0; i<256; i++) {len += count[i];if( count[i]!=0 ) {maxNum = i;sum += ( count[i] * i );if( count[i]>modeCount ) {modeCount = count[i];modeNum = i;}}if( count[i]!=0 && minNum==Integer.MAX_VALUE ) minNum = i;}int cnt = 0;boolean flag = false;for(int i=0; i<256; i++) {cnt += count[i];if( cnt >= len/2 && flag == false ) {meanNum += i;flag = true;if( len%2 == 1 ) break;}if( cnt >= len/2+1 ) {meanNum += i;meanNum /= 2;break;}}res[0] = minNum;res[1] = maxNum;res[2] = (double) sum/len;res[3] = meanNum;res[4] = modeNum;return res;} } View Code1094.?Car Pooling
You are driving a vehicle that?has?capacity?empty seats initially available for passengers.? The vehicle?only?drives east (ie. it?cannot?turn around and drive west.)
Given a list of?trips,?trip[i] = [num_passengers, start_location, end_location]?contains information about the?i-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.? The locations are given as the number of kilometers?due east from your vehicle's initial location.
Return?true?if and only if?it is possible to pick up and drop off all passengers for all the given trips.?
?
Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4 Output: falseExample 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5 Output: trueExample 3:
Input: trips = [[2,1,5],[3,5,7]], capacity = 3 Output: trueExample 4:
Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11 Output: true?
Constraints:
?題目大意:只能一個方向行駛的汽車中,給你一個上下車的數組和一個汽車容量,讓你求汽車可不可以將所有人都運到想要到達的地方。
題目思路:先按照起點排序,然后用一個map記錄下車點對應的人數,然后直接遍歷,先下車然后上車。之前看題目的The locations are given as the number of kilometers?due east from your vehicle's initial location.這句話,我以為是已經排好序了,然后wa了一次。
代碼: class Solution {public boolean carPooling(int[][] trips, int capacity) {int len = trips.length;int num = 0;Arrays.sort(trips, new Comparator<Object>() { public int compare(Object oObjectA, Object oObjectB) { int[] arTempOne = (int[])oObjectA; int[] arTempTwo = (int[])oObjectB; if (arTempOne[1] > arTempTwo[1]) { return 1; } else if (arTempOne[1] < arTempTwo[1]){ return -1;}return 0; }});Map<Integer, Integer> map = new HashMap<>();int last = 0;for(int i=0; i<len; i++) {int num_passengers = trips[i][0];int start_location = trips[i][1];int end_location = trips[i][2];for(int j=last; j<=start_location; j++ ) {Integer tem = map.get(j);if( tem != null ) {num -= tem;}}last = start_location+1;Integer sum = map.get(end_location);if( sum == null ) {sum = num_passengers;} else sum += num_passengers;map.put(end_location, sum);num += num_passengers;if( num > capacity ) return false;}return true;} } View Code1095.?Find in Mountain Array
(This problem is an?interactive problem.)You may recall that an array?A?is a?mountain array?if and only if:
- A.length >= 3
- There exists some?i?with?0 < i?< A.length - 1?such that:
- A[0] < A[1] < ... A[i-1] < A[i]
- A[i] > A[i+1] > ... > A[A.length - 1]
Given a mountain?array?mountainArr, return the?minimum?index?such that?mountainArr.get(index) == target.? If such an?index?doesn't exist, return?-1.
You can't access the mountain array directly.? You may only access the array using a?MountainArray?interface:
- MountainArray.get(k)?returns the element of the array at index?k?(0-indexed).
- MountainArray.length()?returns the length of the array.
Submissions making more than?100?calls to?MountainArray.get?will be judged?Wrong Answer.? Also, any solutions that attempt to circumvent the judge?will result in disqualification.
?
Example 1:
Input: array = [1,2,3,4,5,3,1], target = 3 Output: 2 Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.Example 2:
Input: array = [0,1,2,4,2,1], target = 3 Output: -1 Explanation: 3 does not exist in the array, so we return -1.?
Constraints:
題目大意:山數組,顧名思義像山一樣有一個極大值。給你一個山數組和一個目標值target,求在山數組中等于目標值的下標,如果有多個等于目標值的下標,返回最小的。注意山數組取值只能通過mountain_arr.get(index)方法,而使用這個方法超過100次就會被判錯。
題目思路:看完題目就知道是二分了,可惜沒時間了。用二分找到極大值的下標,然后二分找左邊和右邊。
代碼:
/*** // This is MountainArray's API interface.* // You should not implement it, or speculate about its implementation* interface MountainArray {* public int get(int index) {}* public int length() {}* }*/class Solution {public int findInMountainArray(int target, MountainArray mountainArr) {int n = mountainArr.length();int top = -1;// 找頂點 {int low = 0, high = n;while(high - low > 1){int h = high+low-1>>1;if(mountainArr.get(h) < mountainArr.get(h+1)){low = h+1;}else{high = h+1;}}top = low;}if(mountainArr.get(top) == target)return top;// 找左邊 {int low = 0, high = top+1;while(high - low > 0){int h = high+low>>1;int v = mountainArr.get(h);if(v == target)return h;if(v < target){low = h+1;}else{high = h;}}}// 找右邊 {int low = top, high = n;while(high - low > 0){int h = high+low>>1;int v = mountainArr.get(h);if(v == target)return h;if(v > target){low = h+1;}else{high = h;}}}return -1;}} View Code?
轉載于:https://www.cnblogs.com/Asimple/p/11078060.html
總結
以上是生活随笔為你收集整理的LeetCode Weekly Contest 142的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 研究一下识别验证码,。。。随笔记录
- 下一篇: mgy最新地址 mgyuser.com