forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest48.java
More file actions
36 lines (33 loc) · 1.06 KB
/
test48.java
File metadata and controls
36 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package byteDance;
import java.util.Arrays;
import java.util.HashMap;
/**
* Created by lizeyang on 2020/5/16.
* 一辆公交车,有m站,最多坐n人,输入一路上票的信息(即上车下车站),输出会不会超载
*/
public class test48 {
public static boolean test(int[][] nums,int m,int n){
HashMap<Integer,Integer> map = new HashMap<>();
//按照二维数组第一个元素排序
Arrays.sort(nums,(v1, v2)->v1[0]-v2[0]);
for(int i=0;i<nums.length;i++){
for(int j = nums[i][0];j<=nums[i][1];j++){
if(!map.containsKey(j)){
map.put(j,1);
}else{
if(map.get(j)+1>n){
return false;
}
map.put(j,map.get(j)+1);
}
}
}
return true;
}
public static void main(String[] args) {
int m = 5;
int n = 5;
int[][] nums = {{1,5},{2,3},{2,4},{2,5},{4,5},{3,4},{1,2}};
System.out.println(test(nums,m,n));
}
}