简体   繁体   English

如何使程序帐户中的数组列表中不存在的索引

[英]How do I make the program account for indexes which don't exist in the array-list

My program is supposed to be passed 2 array-lists, arrivals and duration, and should return the number of events that can basically take place without overlap. 我的程序应该传递2个数组列表,到达时间和持续时间,并且应该返回基本上可以发生而不会重叠的事件数。 However, a-lot of testcases are not being passed because the program does not count the duration after the final arrival of the arraylist. 但是,由于该程序不计算arraylist最终到达后的持续时间,因此未通过大量测试用例。

Below is what I have so far: 以下是我到目前为止的内容:

class Results {
    public static int maxEvents(List<Integer> arrival, List<Integer> duration) {
        int counter = 0;
        if (arrival.size() == 0) {
            counter = 0;
        } else {
            for (int i = 0; i < arrival.size() - 1; i++) {
                if (arrival.get(i) + duration.get(i) <= arrival.get(i + 1)) {
                    counter++;
                } else if (arrival.get(i) == arrival.get(i + 1)) {
                    counter++;
                    i++;
                }
            }
        }
        return counter;
    }
}

I have tried to the following but not only did it not pass the current failed testcases but also caused alot of the ones that were successful to fail as well: 我尝试了以下操作,但它不仅没有通过当前失败的测试用例,还导致很多成功的测试用例也失败了:

else if (i == arrival.size()-2) {
    counter++;
}

To show the expected testcase is the following: 显示预期的测试用例如下:

arrivals = [1,3,3,5,7]
duration = [2,2,1,2,1]

The first person arrives at 1, presents for 2 hours, then leaves. 第一人称到达1,出现2小时,然后离开。 2 people arrive at 3 but only one is allowed to present for 2 or 1 hours. 2人到达3点,但只允许一个人出现2或1个小时。 The next person arrives at 5, presents for 2 hours. 下一个人到达5点,持续2小时。 The final person arrives at 7, and presents for 1 hour. 最后一个人到达7点,并出席1小时。 The answer output should be 4 as 4 people are able to present. 答案输出应为4,因为4个人可以发言。

Assuming your input looks something like this: 假设您的输入看起来像这样:

List<Integer> arrival = new ArrayList<Integer>();
arrival.add(10);
arrival.add(12);
arrival.add(15);
arrival.add(17);
arrival.add(20);

List<Integer> duration = new ArrayList<Integer>();
duration.add(3);
duration.add(1);
duration.add(2);
duration.add(4);
duration.add(3);

I would recommend adding a value which stores the closest available time so you don't need to access two elements of your list at each step: 我建议添加一个值,该值存储最近的可用时间,因此您无需在每个步骤中访问列表的两个元素:

public static int maxEvents(List<Integer> arrival, List<Integer> duration)
{
    int counter = 0;
    Integer nextTime = 0;

    for (int i = 0; i < arrival.size(); i++)
    {
        if(nextTime <= arrival.get(i))
        {
            counter++;
            nextTime = arrival.get(i) + duration.get(i);
        }
    }

    return counter;
}

This would make sure that each element in your list gets properly tested. 这样可以确保列表中的每个元素都经过正确测试。

The rules seem to be: 规则似乎是:

  • An arrival must end at most at the next arrival. 到达必须在下一次到达时最多结束。
  • Or the next arrival must be the same, then the next arrival is skipped (not considering its duration). 或下一次到达必须相同,然后跳过下一次到达(不考虑持续时间)。

Expected: - The data is sorted, by arrival. 预期:-数据按到达时间排序。

These rules are slighty weird. 这些规则有些古怪。 For instance your algorithm does not take into account when an arrival is repeated more than two times - but that is your homework. 例如,重复到达两次以上时,您的算法不会考虑在内-但这是您的作业。

What is easiest to handle the last case after the loop. 最简单的方法是处理循环的最后一种情况。

      int i;
      for (i = 0; i < arrival.size()-1; i++) {
          if (arrival.get(i) + duration.get(i) <= arrival.get(i+1)) {
              counter++;
          } else if (arrival.get(i).intValue() == arrival.get(i+1)) {
              counter++;
              i++;
          } 
      }
      if (i < arrival.size()) {
         counter++;
      }

When get(i) returns an Integer <= will ensure the int value is taken (is called automatic unboxing and uses Integer.intValue() . For == simply the Integer are compared, and in general only the integers -128 .. 127 will be cached as unique object. Probably Integer.valueOf(200) != Integer.valueOf(200) . get(i)返回Integer <=将确保采用int值(称为自动拆箱,并使用Integer.intValue() 。对于==仅比较Integer ,通常仅比较整数-128 .. 127将被缓存为唯一对象,可能是Integer.valueOf(200) != Integer.valueOf(200)

To prevent such a pitfall, use plural names for the lists: arrivals and durations , and use help variables: 为避免此类陷阱,请为列表使用复数名称: arrivalsdurations ,并使用帮助变量:

int arrival = arrivals.get(i);
int nextArrival = arrivals.get(i+1);

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

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