我一直在尝试解决HackerEarth问题,但最后几个测试用例似乎总是超时
我是一年级学生,所以我真的不知道该如何优化
我尝试查看java解决方案,但是它只是将大写的案例放入代码中,有效地消除了对其进行优化的需要
import java.io.*;
import java.util.*;
public class police {
static int t,n,k;
static char[][] test;
static int max = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
t = Integer.parseInt(st.nextToken());
for (int i = 0; i < t; i++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
test = new char[n][n];
int ret = 0;
for (int b = 0; b < n; b++) {
st = new StringTokenizer(br.readLine());
for (int a = 0; a < n; a++) {
test[b][a] = st.nextToken().charAt(0);
}
}
for (int b = 0; b < n; b++) {
ret += solve(test[b]); //calculate each row
}
System.out.println(ret);
}
}
static int solve(char[] a) { //given a row, calculate the maximum number of catches
int ret = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 'P') {
for (int b = i - k; b <= i + k; b++) { //scan area to see if police can catch a thief
if (b >= 0 && b < n && a[b] == 'T') {
a[b] = 'C'; //caught
break;
}
}
a[i] = 'U'; //used
}
}
for (int i = 0; i < n; i++) //count
if (a[i] == 'C')
ret++;
return ret;
}
}
我很确定,如果有人可以帮助我,那将与解决方法有关
答案 0 :(得分:2)
是的,在solve
方法中使用的方法是造成超时的原因。
首先,您需要了解algorithm complexity和Big O notation
问题约束是:
1 <= N <= 1000
1 <= K <= N * N
这些表明您的解决方案的复杂度最多应为O(N * N)
。换句话说,最多有两个嵌套的for循环,每个循环的复杂度为O(N)。
在解决方案中,您将执行以下操作:
for (int b = 0; b < n; b++) {
ret += solve(test[b]); //calculate each row
}
好的,此循环至关重要,因为您必须遍历网格中的所有行。复杂度:O(N)
然后在您的求解方法中:
for (int i = 0; i < n; i++) {
if (a[i] == 'P') {
for (int b = i - k; b <= i + k; b++) { //scan area to see if police can catch a thief
if (b >= 0 && b < n && a[b] == 'T') {
a[b] = 'C'; //caught
break;
}
}
a[i] = 'U'; //used
}
}
这些嵌套的for循环是问题的真正原因,对于较高的O(N)
,外部循环的复杂度为O(N)
,内部循环的复杂度也可能为K
。因此,三个for循环的总复杂度可能达到O(N * N * N)
,这肯定会超时。
这是我解决这个问题的方法,我只修改了solve
方法:
static int solve(char[] a) { //given a row, calculate the maximum number of catches
int ret = 0;
ArrayDeque<Integer> policeMenQueue = new ArrayDeque<>(); // queue for holding positions of policemen
ArrayDeque<Integer> thievesQueue = new ArrayDeque<>(); // queue for positions of thieves
for (int i = 0; i < n; i++) {
if(!policeMenQueue.isEmpty()) { // check if the leftmost policeman can catch a thief at current position (i)
Integer mostLeftPoliceMan = policeMenQueue.getFirst();
if(i - mostLeftPoliceMan > k) { // if he cannot then we must remove him as he will no longer be able to catch any thieves
policeMenQueue.removeFirst();
}
}
if(!thievesQueue.isEmpty()) { // check if the leftmost thief can be caught be a policeman at current position (i)
Integer mostLeftThief = thievesQueue.getFirst();
if(i - mostLeftThief > k) { // if not then we must remove him as he will no longer be caught by any policemen
thievesQueue.removeFirst();
}
}
if(a[i] == 'P') {
if(!thievesQueue.isEmpty()) { // the leftmost thief can be caught by current policeman
ret++;
thievesQueue.removeFirst(); // ok this thief is caught, remove him
} else {
policeMenQueue.addLast(i); // just add the policeman to keep his position in the queue
}
}
if(a[i] == 'T') {
if(!policeMenQueue.isEmpty()) { // the current thief can be caught by the leftmost policeman
ret++;
policeMenQueue.removeFirst(); // ok this policeman has already caught a thief (used), remove him
} else {
thievesQueue.addLast(i); // just add the thief to keep his position in the queue
}
}
}
return ret;
}
我的想法是:从左到右在每一行上循环播放,并根据问题指出:每个警察只能抓到一个小偷,我们希望最大程度地捕获被抓的小偷,因此,每个小偷都被抓到是对我们有利的最左边的警察(我们从左到右)。
例如考虑以下行:
P P T T
想象一下K = 2
我们赞成将3
位置的小偷捕获到1
位置的警察,因为该警察无法捕获4
位置的盗贼,当然也不能捕获2
位置的警察1}}在这种情况下可以同时捕获两个小偷,但是请记住,我们必须最大化捕获的小偷的数量,每个警察只能捕获一个小偷,因此我们希望每个小偷都由可以捕获他的最左边的警察捕获。 >
我的解决方案取决于queue
数据结构(Java中的ArrayDeque
),如果您不知道它是如何工作的或者我为什么使用它,请看这里:https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm