字节跳动之算法(三):二维平面整数点集最大值问题(50%-80%准确率)

题目:

P为给定的二维平面整数点集。定义 P 中某点x,如果x满足 P 中任意点都不在 x 的右上方区域内(横纵坐标都大于x),则称其为“最大的”。求出所有“最大的”点的集合。(所有点的横坐标和纵坐标都不重复, 坐标轴范围在[0, 1e9) 内)

如下图:实心点为满足条件的点的集合。请实现代码找到集合 P 中的所有 ”最大“ 点的集合并输出。

输入描述:第一行输入点集的个数 N, 接下来 N 行,每行两个数字代表点的 X 轴和 Y 轴。
对于 50%的数据,  1 <= N <= 10000;
对于 100%的数据, 1 <= N <= 500000;
输出描述:输出“最大的” 点集合, 按照 X 轴从小到大的方式输出,每行两个数字分别代表点的 X 轴和 Y轴。

输入例子1:

5
1 2
5 3
4 6
7 5
9 0

输出例子1:

4 6
7 5
9 0

输入例子2:

10
298498081 747278511
427131847 460128162
939984059 817455089
911902081 683024728
474941318 6933274
140954425 607811211
336122540 629431445
208240456 458323237
646203300 469339106
106410694 436340495

输出例子:

939984059 817455089

Java代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;


public class Main {
	public static List<Integer[]> list = new ArrayList<Integer[]>();
	public static List<Integer[]> listLast = new ArrayList<Integer[]>();//全局数组链表,最后的max值
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Scanner sc = new Scanner(System.in);
			int n = sc.nextInt();
			List<Integer[]> test = new ArrayList<Integer[]>();//数组链表
			for(int i = 0; i < n ;i++){
				int a = sc.nextInt();
				int b = sc.nextInt();
				Integer []arr = {a,b,a+b};
				test.add(i,arr);
			}
			findMaxCollections(test);
			resetList(list);
			for(int i = listLast.size()-1;i>=0;i--){
				System.out.println( listLast.get(i)[0] +" "+ listLast.get(i)[1] );
			}

	}
	
	public static void findMaxCollections(List<Integer[]> test){
		//当前的x和y
		int current_X = 0;
		int current_Y = 0;
		for(int i = 0;i < test.size();i++){
			current_X = test.get(i)[0];
			current_Y = test.get(i)[1];
			for(int j = 0;j<test.size();j++){
				if(current_X < test.get(j)[0] &&current_Y <test.get(j)[1] ){
					break;
				}
				if(j == test.size()-1){
					Integer [] arr = new Integer [2];
					//横纵坐标
					arr[0] = current_X;
					arr[1] = current_Y;
					list.add(arr);
				}
			}
		}
	}
	
	public static void resetList(List<Integer[]> test){
			int[] yPoint = new int [test.size()];
			int[] yPointLast = new int [test.size()];
			for(int i = 0;i<test.size();i++){
				yPoint[i] = test.get(i)[1];
			}
			Arrays.sort(yPoint);
		
			for(int i = test.size()-1;i>=0;i--){
				yPointLast[i] = yPoint[i];
			}
			for(int i = 0;i<test.size();i++){
				for(int j= 0;j<test.size();j++){
					if(yPointLast[i] == test.get(j)[1]){
						Integer [] arr = new Integer[2];
						arr[0] = test.get(j)[0];
						arr[1] = test.get(j)[1];
						listLast.add(arr);
					}
				}
			}
	}
}

解析:这个题主要是要寻找元素点是否是最大点,制约条件就是右上角是否还存在元素点大于他的存在。其实就是以要假设元素该点为原点进行坐标建立,且第一象限中的不存在任何元素点即可。不存在x>x0,y>y0即可。不满足该条件一律不存在。

感想: 博主在算法设计中走了很多弯路,感觉还有很多的便捷曲径去走的,但是还是基于对数据结构的把握太浅。。像这类问题是要多点进行思考就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值