题解 | #合唱队#
合唱队
https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), i = 0, j; int[] heights = new int[n]; while (i < n) { heights[i++] = in.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, 1); Arrays.fill(right, 1); for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { if (heights[i] > heights[j]) { left[i] = Math.max(left[i], left[j] + 1); } } } for (i = n - 1; i >= 0; i--) { for (j = n - 1; j > i; j--) { if (heights[i] > heights[j]) { right[i] = Math.max(right[i], right[j] + 1); } } } int max = Integer.MIN_VALUE; for (i = 0; i < n; i++) { max = Math.max(max, right[i] + left[i] - 1); } System.out.println(n - max); } }
#华为笔试#