今日头条,编程题一,数组保存

今晚头条的编程:
P为给定的一个二维平面,定义P中的 x 点,如果 x 点满足P中任意点都不在 x 的右上方区域内(横坐标都大于 x ),则称 x “最大”,求出所有
最大的点集合。
输入:N(点集的个数),输出(“最大”的集合)
测试用例:
5
1 2
5 3
4 6
7 5
9 0
输出:
4 6
7 5
9 0
 问:我下面的代码本地ac,提交20%,报:时间超出。问:保存一组坐标的最好的方法是什么,我用list确实耗时了。
 
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNextInt()) {
			// 输入n组数
			int n = sc.nextInt();
			int[] x = new int[n];// 横坐标
			int[] y = new int[n];// 纵坐标
			
			// 索引相同,代表是一组坐标
			for (int i = 0; i < n; i++) {
				x[i] = sc.nextInt();
				y[i] = sc.nextInt();
			}
			
			// list存放一组坐标list.add(x);list.add(y)
			ArrayList<Integer> list = new ArrayList<Integer>();
			// list1存放多组坐标list1.add(list)
			ArrayList<ArrayList<Integer>> list1 = new ArrayList<ArrayList<Integer>>();

			// 筛选符合要去的一组坐标,放入list,将改组坐标放入list1
			for (int i = 0; i < n; i++) {
				int flag = 0;
				for (int j = 0; j < n; j++) {
					if (x[i] > x[j] || y[i] > y[j] && i != j) {
						flag++;
					}
				}
				if (flag == n - 1) {
					list.add(x[i]);
					list.add(y[i]);
					list1.add(list);
					list = new ArrayList<Integer>();
				}
			}
			
			// 遍历list1,取出筛选的坐标
	   for (int i = 0; i < list1.size(); i++) {
		System.out.println(list1.get(i).get(0) + " " + list1.get(i).get(1));
	   }
	}
   }
}




#字节跳动#
全部评论
将输入的x,y对按照x顺序排序 然后从最后一个y开始依次找更大的y 记录下这样坐标对输出。
点赞 回复
分享
发布于 2017-08-22 22:09
同学你好!首先感谢你参加今日头条笔试,如果在笔试过程中遇到任何问题,可以通过申诉通道与我们联系。情况核对属实后,可以有二次笔试的机会,成绩以最后一次考试为准。【申诉通道】campushr@bytedance.com,请在正文简要说明笔试遇到的问题,邮件标题为: 笔试申诉+岗位+姓名+***话,我们会尽快回复~
点赞 回复
分享
发布于 2017-08-22 22:00
滴滴
校招火热招聘中
官网直投
二维数据吧
点赞 回复
分享
发布于 2017-08-22 22:06
nonono
点赞 回复
分享
发布于 2017-08-22 22:20
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<iomanip> #include<string> #include<bitset> #include<algorithm> #include<queue> #include<deque> #include<list> #include<map> #include<set> #include<stack> #include<vector> #include<array> #include <numeric> #include <limits> #define ll long long; using namespace std; struct point { int x,y; }; bool operator<(const point&p1,const point&p2) { if(p1.x<p2.x)return 1; if(p1.x==p2.x&&p1.y<p2.y)return 1; return 0; } int main(){ int n,i; cin>>n; vector<point>points(n); vector<bool>isMax(n,0); i=n; while(i--) { scanf("%d%d",&points[i].x,&points[i].y); } sort(points.begin(),points.end()); int f=n-1; for(i=n-2;i>=0;--i) { if(points[i].y>points[f].y){f=i;continue;} isMax[i]=1; } for(int i=0;i<n;++i) { if(!isMax[i]) printf("%d %d\n",points[i].x,points[i].y); } } 用的c++,主要是输入输出,java的输入输出效率实在是难受
点赞 回复
分享
发布于 2017-08-22 22:23
import java.util.*; public class Main{         public static void main(String[] args){     Scanner scanner = new Scanner(System.in);         int n = scanner.nextInt();         int[] arr = new int[n*2];         int count = 0;         for(int i=0;i<n*2;i++){               arr[i] = scanner.nextInt();                           }       for (int i = 0; i < n * 2; i = i + 2) { for (int j = i + 2; j < n * 2; j = j + 2) { if (arr[i] < arr[j]) { if (arr[i + 1] > arr[j + 1]) { count++; if (count == ((n * 2 - i) / 2)) { System.out.println(arr[i] + " " + arr[i + 1]); count = 0; } } } } }       System.out.println(arr[n*2-2]+" "+arr[n*2-1]);     } } //这样写如何
点赞 回复
分享
发布于 2017-08-22 22:56

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务