简体   繁体   English

如何优化代码(java)

[英]How optimize code (java)

I have a task. 我有任务 When a user registers on the site, his id can be his favorite number. 用户在网站上注册时,其ID可以是他最喜欢的号码。 But if there is already a user with such an id, then the id of this user becomes the smallest free number following his favorite number. 但是,如果已经有一个具有此ID的用户,则该用户的ID将成为其喜欢的号码之后的最小空闲号码。 But when the tourist leaves the site, his identifier ceases to be occupied, and the next user can occupy his id. 但是当游客离开该站点时,其标识符将不再被占用,并且下一个用户可以占用其ID。 For example: 例如:

Input:
first line value `n` is amount of users
the next `n` lines contain two values: first value can be 1  if user register on site or 2 if user leave site
 second value is favorite number (1<=x<=1000000000)
Input:
7
1 10
1 10
1 10
1 11
1 20
2 11
1 11

Output:
10
11
12
13
20
11

Here is my code: 这是我的代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
 public static final Scanner in= new Scanner(System.in);
 public static final int SIZE = 2;
 public static final int MAXNUMBER = 1000000000;
    public static void main(String[] args) {
        int t = Integer.parseInt(in.nextLine());
        Map<Integer, Integer> favoriteNumbers = new HashMap<>();
        int []a = new int[SIZE];
        while(t-- > 0) {
            for(int i=0; i<SIZE; i++) {
                a[i] = in.nextInt();
            }
            if(a[0] == 1 && !favoriteNumbers.containsValue(a[1])) {
                System.out.println(a[1]);
                favoriteNumbers.put(a[1], a[1]);
            }
            else if (a[0] == 1 && favoriteNumbers.containsValue(a[1])) {
                for(int i= (a[1] + 1); i<MAXNUMBER; ) {
                    if(!favoriteNumbers.containsValue(i)) {
                        System.out.println(i);
                        favoriteNumbers.put(i, i);
                        break;
                    }
                    i++;
                }
            } else if(a[0] == 2 && favoriteNumbers.containsValue(a[1])){
                favoriteNumbers.remove(a[1], a[1]);
            }
        }
    }
}

It's work correctly, but I get run time exceeded. 它可以正常工作,但是我超出了运行时间。 The maximum execution time should be 1 second. 最大执行时间应为1秒。 Please help me how I can optimize code and speed up runtime. 请帮助我如何优化代码并加快运行速度。

favoriteNumbers.containsValue(a[1]) takes linear time (since finding a value in a HashMap requires iterating over all the values). favoriteNumbers.containsValue(a[1])采用线性时间(因为在HashMap找到值需要迭代所有值)。 favoriteNumbers.containsKey(a[1]) would take constant time. favoriteNumbers.containsKey(a[1])将花费固定时间。

Of course, since the key and value of your map are identical, you should use HashSet instead of HashMap , and once you change favoriteNumbers to a HashSet , favoriteNumbers.contains(a[1]) would take constant time. 当然,由于地图的键和值相同,因此应使用HashSet而不是HashMap ,并且一旦将favoriteNumbers更改为HashSetfavoriteNumbers.contains(a[1])将花费固定的时间。

Here's an optimized version using HashSet . 这是使用HashSet的优化版本。 Edit: 编辑:

import java.util.*;

public class Main {
    public static final Scanner in= new Scanner(System.in);
    public static final int SIZE = 2;
    public static final int MAXNUMBER = 1000000000;

    public static void main(String[] args) {
        int t = Integer.parseInt(in.nextLine());
        StringBuilder sb = new StringBuilder("");
        Set<Integer> favoriteNumbers = new HashSet<>();
        int []a = new int[SIZE];
        while(t-- > 0) {
            for(int i=0; i<SIZE; i++) {
                a[i] = in.nextInt();
            }
            if(a[0] == 1) {
                for(int i = a[1]; i < MAXNUMBER; i++) {
                    if (favoriteNumbers.add(i)) {
                        sb.append(i + "\n");
                        break;
                    }
                }
            } else if(a[0] == 2) {
                favoriteNumbers.remove(a[1]);
            }
        }
        System.out.println(sb.toString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM