#完美世界
刚做完完美世界的笔试,就想吐槽一下,这编程平台太垃圾了吧。算法题能运行半天还不出结果。
我真是汗颜
2022-08-27 14:00:00的笔试
//第一题合并区间 力扣原题
public int[][] merge(int[][] intervals) {
//排序
Arrays.sort(intervals,new Comparator<int[]>(){
public int compare(int[] o1,int[] o2){
return o1[0] - o2[0];
}
});
List<int[]> list = new ArrayList<int[]>();
for (int i = 0; i < intervals.length; i++){
int L= intervals[i][0];
int R = intervals[i][1];
if (list.size() != 0 && list.get(list.size()-1)[1] >= L){
if(list.get(list.size()-1)[1] < R)
list.get(list.size()-1)[1] = R;
}else{
list.add(new int[]{intervals[i][0],intervals[i][1]});
}
}
return list.toArray(new int[list.size()][2]);
}
//第二题
01背包
public static void main(String[] args) {
//int[] garbages = {30,45,12,23,8}; //4
int[] garbages = {2,1,9,8,4,2,5}; //1
int sum = 0;
for(int garbage : garbages) sum += garbage;
int target = sum / 2;
int[] dp = new int[target + 1];
for(int i = 0 ;i < garbages.length ;i++){
for(int j = target ;j >= garbages[i] ;j--) dp[j] = Math.max(dp[j] , dp[j - garbages[i]] + garbages[i]);
}
System.out.println(sum - 2 * dp[target]);
}
#完美世界笔试##java#