Java 如何优化初学者黑客代码
我一直在尝试解决这个黑客问题,但最后几个测试用例似乎总是超时 问题陈述: 我是一年级的学生,所以我真的不知道如何优化它 我试着研究java解决方案,但它只是将更大的案例放入代码中,有效地消除了优化它的需要Java 如何优化初学者黑客代码,java,algorithm,optimization,data-structures,Java,Algorithm,Optimization,Data Structures,我一直在尝试解决这个黑客问题,但最后几个测试用例似乎总是超时 问题陈述: 我是一年级的学生,所以我真的不知道如何优化它 我试着研究java解决方案,但它只是将更大的案例放入代码中,有效地消除了优化它的需要 import java.io.*; import java.util.*; public class police { static int t,n,k; static char[][] test; static int max = 0; public sta
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;
}
}
import java.io.*;
导入java.util.*;
公营警察{
静态int t,n,k;
静态字符[][]测试;
静态int max=0;
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
StringTokenizer st=新的StringTokenizer(br.readLine());
t=Integer.parseInt(st.nextToken());
for(int i=0;i
我很确定这与solve方法有关,如果有人能帮助我,那将是一件令人惊讶的事你是对的,你在
solve
方法中使用的方法是导致超时的原因
首先,你需要有一个关于和的想法
问题的制约因素包括:
1k){//如果他抓不到,我们必须把他带走,因为他再也抓不到小偷了
policmenqueue.removeFirst();
}
}
如果(!thievesquee.isEmpty()){//检查最左边的小偷是否是当前位置的警察(i)
整数mostleftthive=thievesquee.getFirst();
如果(i-mostleft小偷>k){//如果不是,那么我们必须把他带走,因为他将不再被任何警察抓住
thievesque.removeFirst();
}
}
如果(a[i]=“P”){
如果(!thievesquee.isEmpty()){//最左边的小偷会被现在的警察抓住
ret++;
thievesquee.removeFirst();//好的,这个小偷被抓住了,把他带走
}否则{
policimenqueue.addLast(i);//只需添加警察以保持其在队列中的位置
}
}
如果(a[i]=“T”){
如果(!policmenqueue.isEmpty()){//最左边的警察可以抓住当前的小偷
ret++;
policeMenQueue.removeFirst();//好的,这个警察已经抓到一个小偷(用过),把他带走
}否则{
thievesquee.addLast(i);//只要加上小偷就可以保持他在队列中的位置
}
}
}
返回ret;
}
我的想法是:从左到右循环每一行,正如问题所述:每个警察只能抓到一个小偷,我们希望最大限度地增加抓到的小偷数量,因此每个小偷都被最左边的警察抓到是对我们有利的(因为我们从左到右)
例如,考虑这行:
ptt
想象一下,K=2
我们希望3号位置的小偷被1号位置的警察抓住,因为这个警察不能抓住4号位置的小偷,当然2号位置的警察可以抓住这两个小偷,但请记住,我们必须最大限度地增加被抓到的小偷数量,每个警察只能抓到一个小偷,因此我们希望每个小偷都被能抓到他的最左边的警察抓到
我的解决方案依赖于queue
数据结构(Java中的ArrayDeque
),如果您不知道它是如何工作的,或者我为什么要使用它,请看这里:JC Denton-好时光!我能给你的唯一提示是,取一行并计算最大cougt thievs。然后继续下一步嗨-我想我的代码可以做到这一点,但它似乎真的很慢。当你有时间了解如何改进我的“解决”方法时,你能检查一下吗?很好的解决方案!非常感谢您为我详细解释并评论您的代码,我想我现在正在研究队列数据结构,使用两个索引(指向最后一个未使用但可用的policimen/thieve可能更简单),但我非常喜欢您的解决方案。
for (int b = 0; b < n; b++) {
ret += solve(test[b]); //calculate each row
}
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
}
}
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;
}