WorldQuant第一轮OA, 就像地里说的,题目确实没变, 第一题最后忘记改了一个小bug, 有点可惜.....不过代码可以大家看一下
class Result {
/*
* Complete the 'bioHazard' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER_ARRAY allergic
* 3. INTEGER_ARRAY poisonous
*/
public static long bioHazard(int n, List<Integer> allergic, List<Integer> poisonous) {
// Write your code here
for(int i = 0; i < allergic.size(); i++) {
allergic.set(i,allergic.get(i) - 1);
}
for(int i = 0; i < poisonous.size(); i++) {
poisonous.set(i,poisonous.get(i) - 1);
}
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < allergic.size(); i++) {
int left = Math.max(allergic.get(i), poisonous.get(i));
int right = Math.min(allergic.get(i), poisonous.get(i));
if(map.containsKey(right)) {
int cur = map.get(right);
map.put(right, Math.max(left, cur));
} else {
map.put(right,left);
}
}
long res = 0;
int end = 0;
for(int i = 0; i < n -1; i++) {
if (i == 0) end = 0;
while(end < n -1 && (!map.containsKey(end) || map.get(end) > i)) {
end++;
}
res += end - i;
}
long ans = (long)res;
return ans;
}
|