数组A中给定可以使用的1~9的数,返回由A数组中的元素组成的小于n的最大数。
例如A={1, 2, 4, 9},x=2533,返回2499
暴力解法
import java.util.Scanner;
class Main {
public static int res = Integer.MIN_VALUE;
public static int getMax(int[] array, int target) {
dfs(target, array, 0, 0);
return res;
}
private static void dfs(int target, int[] array, int curVal, int index) {
res = Math.max(res, curVal);
if (index == array.length) return;
for (int i = 0; i < array.length; i++) {
curVal = curVal * 10 + array[i];
if (curVal >= target) {
continue;
}
index++;
dfs(target, array, curVal, index);
index--;
curVal /= 10;
}
}
public static void main(String[] args){
int[] nums= {1, 2, 4, 9};
int maxVal = 2533;
System.out.println(getMax(nums, maxVal));
}
}
// 给你一个会议时间安排的数组 intervals
// 每个会议时间都会包括开始和结束的时间 intervals[i] = [starti, endi]
// 为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排
import java.util.*;
public class Main{
public static int helper(int[][] intervals){
if(intervals == null || intervals.length == 0){
return 0;
}
Arrays.sort(intervals, (v1, v2) -> (v1[0] - v2[0]));
PriorityQueue<Integer> heap = new PriorityQueue<>();
int cnt = 0;
for(int[] meeting: intervals){
while(!heap.isEmpty() && meeting[0] >= heap.peek()){
heap.poll();
}
heap.offer(meeting[1]);
cnt = Math.max(cnt, heap.size());
}
return cnt;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] intervals = new int[n][2];
for(int i = 0; i < n; i++){
int l = sc.nextInt();
int r = sc.nextInt();
intervals[i][0] = l;
intervals[i][1] = r;
}
System.out.println(helper(intervals));
}
}
deepseek写的双指针贪心: