又是两道题目,感觉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

转载于:https://www.cnblogs.com/Asimple/p/11078060.html

LeetCode Weekly Contest 142相关推荐

  1. LeetCode Weekly Contest 25 之 545.Boundary of Binary Tree

    LeetCode Weekly Contest 25 赛题 本次周赛主要分为以下4道题: 507 Perfect Number (3分) 537 Complex Number Multiplicati ...

  2. LeetCode weekly contest 190 周赛

    5/23/20 第二次打周赛.全部通过AC.纪念一下.最后一题DP用的不是最优解,写了2次bug version. 可以去Github直接看我其他leetcode代码. Q1455_Check If ...

  3. LeetCode Weekly Contest 185

    5388. 重新格式化字符串 给你一个混合了数字和字母的字符串 s,其中的字母均为小写英文字母. 请你将该字符串重新格式化,使得任意两个相邻字符的类型都不同.也就是说,字母后面应该跟着数字,而数字后面 ...

  4. LeetCode Weekly Contest 199

    5472. 重新排列字符串 题目难度Easy 给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的 ...

  5. LeetCode——Weekly Contest 314

    LeetCode周赛第314场记录 2432. 处理用时最长的那个任务的员工 周赛第一题,根据题意写出代码即可.首先根据logs计算出每项工作的实际用时,然后根据用时长短对员工的ID号进行排序即可.给 ...

  6. LeetCode Weekly Contest 194

    1486. 数组异或操作 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length . 请返回 ...

  7. LeetCode Weekly Contest 27

    Reverse Words in a String III 简单暴力翻转 class Solution { public:string rev(string s){int len=s.length() ...

  8. LeetCode笔记:Weekly Contest 280

    LeetCode笔记:Weekly Contest 280 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4 ...

  9. LeetCode之Weekly Contest 90

    LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回  ...

最新文章

  1. 面试题编程题06-python 输入一个字符串,反转输出
  2. git warning: LF will be replaced by CRLF in 解决办法 1
  3. jquery的disabled
  4. Arduino终于支持代码补全了!小白们终于可以愉快的写代码了!Arduino IDE 2.0beta功能简介...
  5. 【spring boot】 禁用/关闭数据源/DataSource
  6. 下一代CC++测试框架TestNG++入门指导【转】
  7. mariadb数据库增删改查
  8. 网络监控工具--ntop
  9. 自学dapp开发资料
  10. centos7系统使用杀毒软件clamav
  11. python selenium加速_selenium2.0环境搭建(一)
  12. QT 监控串口中文乱码解决
  13. 易语言html截图,易语言如何指定区域截图;易语言怎么才能全屏截图
  14. 面试常见逻辑题小整理
  15. 解决Win7的svchost进程占内存过大,计算机运行过慢的方法
  16. 用最科学的方法展示最形象的图表——前端数据可视化实践
  17. Socks5代理IP技术详解:如何选择和使用最佳IP代理服务
  18. 用python写海明校验码
  19. linux如何关闭硬件加速,启用硬件加速是什么意思?如何关闭【详解】
  20. 【python-tips】非root权限安装pip、其他包

热门文章

  1. 字符串匹配算法_4月16日活动预告|字符串匹配算法解析
  2. P5713 【深基3.例5】洛谷团队系统(python3实现)
  3. 十二届蓝桥杯C++ 1月 中 高级组试题 第三题 换算
  4. CAN总线技术 | 物理层04 - 终端电阻与双绞线(特性阻抗120欧)
  5. coco showanns不显示_coco奶茶加盟好不好?【5月官网最新公布】加盟费用+加盟流程...
  6. Qt笔记-窗口程序在任务栏中隐藏及永久置顶
  7. Nginx文档阅读笔记-Reverse Proxy vs. Load Balancer(反向代理与负载均衡)
  8. Python工作笔记-解决python使用nohup后台运行重定向不输出问题
  9. C++设计模式-抽象工厂模式
  10. Qt杂记-QQuick之Android隐藏状态栏以及状态栏透明(QQuick项目)