I've been trying to do this HackerEarth problem but the last few test cases always seem to time out
Problem Statement: https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/joker-and-thieves-53e59f4a/
I'm a first year student, so I don't really know how to optimize that well
I tried looking at the java solution, but it just put the bigger cases into the code, effectively removing the need to optimize it
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;
}
}
I'm pretty sure it has something to do with the solve method, if anyone could help me that would be amazing