题解 | #选数# Java 题解
选数
https://www.nowcoder.com/practice/50636077fde843c09bf915d0aef57a99
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int s = sc.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n;i++){ nums[i] = sc.nextInt(); } Arrays.sort(nums); int l = 0, r = n - 1; int res = -1; while(l < r){ int mid = (l + r) >> 1; long sum = 0; int tl = Math.max(0,mid - k + 1),tr = mid ; for(int i = tl ; i <= tr;i++){ sum += nums[i]; } if(sum >= s){ res = nums[mid]; r = mid; }else{ l = mid + 1; } } System.out.println(res); } }