英文:
Which test case causes the Runtime Error?
问题
import java.util.*;
import java.io.*;
// Parenting Partnering Returns
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = in.nextInt();
for (int ijk = 1; ijk <= t; ++ijk) {
int N = in.nextInt();
String ans;
StringBuilder sb = new StringBuilder();
boolean[] cal = new boolean[1442];
boolean[] jal = new boolean[1442];
int si, se;
boolean canC, canJ, isPossible = true;
for (int i = 0; i < N; i++) {
si = in.nextInt();
se = in.nextInt();
canC = false;
canJ = false;
if (cal[si + 1] == false && cal[se] == false || cal[si] == false && cal[se - 1] == false) {
for (int j = si; j <= se; j++) cal[j] = true;
sb.append('C');
canC = true;
continue;
} else if (jal[si + 1] == false && jal[se] == false || jal[si] == false && jal[se - 1] == false) {
for (int k = si; k <= se; k++) jal[k] = true;
sb.append('J');
canJ = true;
continue;
} else {
isPossible = false;
break;
}
}
ans = sb.toString();
if (isPossible)
System.out.println("Case #" + ijk + ": " + ans);
else
System.out.println("Case #" + ijk + ": " + "IMPOSSIBLE");
}
//in.close();
}
}
英文:
This was one of coding jam 2020 questions with the link attached below (round is complete), and while I could not reproduce it, the google engine reported that this was a runtime error even for the small test case. It could pretty much handle any test case I threw at it. Perhaps I am missing an edge case, but not sure what it is.
My understanding is that this is O(N) performance. All it really does is to check if the boolean sub array is free(false) and if so, assign the task to that sub array(true) with may be 2N compares.
https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020bdf9
import java.util.*;
import java.io.*;
//Parenting Partnering Returns
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
for (int ijk = 1; ijk <= t; ++ijk) {
int N = in.nextInt();
String ans;
StringBuilder sb = new StringBuilder();
boolean[] cal = new boolean[1442];
boolean[] jal = new boolean[1442];
int si, se;
boolean canC,canJ, isPossible = true;
for (int i = 0 ; i < N ; i++) {
si = in.nextInt();
se = in.nextInt();
canC = false; canJ = false;
if (cal[si + 1] == false && cal[se] == false || cal[si] == false && cal[se -1] == false) {
for (int j = si ; j <= se; j++) cal[j] = true;
sb.append('C'); canC = true; continue;
} else if (jal[si + 1] == false && jal[se] == false || jal[si] == false && jal[se -1] == false) {
for (int k = si ; k <= se; k++) jal[k] = true;
sb.append('J'); canJ = true; continue;
} else {
isPossible = false;
break;
}
}
ans = sb.toString();
if(isPossible)
System.out.println("Case #" + ijk + ": " + ans);
else System.out.println("Case #" + ijk + ": " + "IMPOSSIBLE");
}
//in.close();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论