• 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:

    1. count.length == 256
    2. 1 <= sum(count) <= 10^9
    3. The mode of the sample that count represents is unique.
    4. Answers within 10^-5 of the true value will be accepted as correct.

    题目大意:给你一个长度为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 Code

    1094. 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 tripstrip[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: false
    

    Example 2:

    Input: trips = [[2,1,5],[3,3,7]], capacity = 5
    Output: true
    

    Example 3:

    Input: trips = [[2,1,5],[3,5,7]], capacity = 3
    Output: true
    

    Example 4:

    Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
    Output: true
    
     

    Constraints:

    1. trips.length <= 1000
    2. trips[i].length == 3
    3. 1 <= trips[i][0] <= 100
    4. 0 <= trips[i][1] < trips[i][2] <= 1000
    5. 1 <= capacity <= 100000

     题目大意:只能一个方向行驶的汽车中,给你一个上下车的数组和一个汽车容量,让你求汽车可不可以将所有人都运到想要到达的地方。

    题目思路:先按照起点排序,然后用一个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 Code

    1095. 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:

    1. 3 <= mountain_arr.length() <= 10000
    2. 0 <= target <= 10^9
    3. 0 <= mountain_arr.get(index) <= 10^9

    题目大意:山数组,顾名思义像山一样有一个极大值。给你一个山数组和一个目标值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
  • 相关阅读:
    【转】高级爬虫
    python-基于遗传算法的多三角形拟合图像实例
    python-文件处理
    python-函数式编程与内置函数
    Python-变量、函数及递归
    Python-字符串的拼接与函数
    Python-集合
    Python-列表、元组、字典
    Python-字符串2
    Python-字符串
  • 原文地址:https://www.cnblogs.com/Asimple/p/11078060.html
Copyright © 2020-2023  润新知