网易互娱开发第四题

求大佬帮看下代码!
package 笔试;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] data = new int[n];
		for (int i = 0; i < n; i++) {
			data[i] = sc.nextInt();
		}
		int count = sc.nextInt();
		int q[] = new int[count];
		for (int i = 0; i < count; i++) {
			q[i] = sc.nextInt();
		}
		for (int j = 0; j < count; j++) {
			int res = solu(q[j], data);
			System.out.println(res);
		}

	}
/*
10
6 12 20 14 15 15 7 19 18 13
6 
15 23 19 1 17 24
 */
	public static int solu(int num, int[] arr) {
		int len = arr.length;
		int dp[] = new int[len];
		int end[] = new int[len];
		for (int k = 0; k < len; k++) {
			end[k] = -1;
		}
		if (arr[0] > num) {
			dp[0] = 1;
			end[0] = 0;
		}
		for (int i = 1; i < len; i++) {
			if ((i - 1) != end[i - 1]) {
				if (arr[i] > num) {
					dp[i] = dp[i - 1] + 1;
					end[i] = i;
				} else {
					dp[i] = dp[i - 1];
					end[i] = end[i - 1];
				}
			}
			else{
				if (arr[i] > num) {
					dp[i] = dp[i - 1];
					end[i] = i;
				} else {
					dp[i] = dp[i - 1];
					end[i] = end[i - 1];
				}
			}
		}
		return dp[len - 1];
	}
}


#网易互娱##笔试题目#
全部评论
这是100么,大佬有没有第二题的ac代码,求教
点赞 回复 分享
发布于 2019-08-11 18:06
这样不超时?
点赞 回复 分享
发布于 2019-08-11 18:10
这个o(nq)吗,那要超时
点赞 回复 分享
发布于 2019-08-11 19:30

相关推荐

评论
2
6
分享

创作者周榜

更多
牛客网
牛客企业服务