As an engineer, I decided to start solving the past questions of ** AtCoder **, thinking that it was necessary to study voluntarily to gain strength. However, when I submitted the following code due to the problem of ** Japan sinking **, I got angry at ** Time Limit Exceeded **.
JapanSinksTle.java
import java.io.*;
import java.util.*;
import static java.lang.System.in;
class Main{
public static void main(String[] args)throws IOException{
Scanner sc = new Scanner(System.in);
int field = sc.nextInt();
int jRyouiki[] = new int[field];
int high = 0;
for(int i = 0; i < field; i++){
jRyouiki[i] = sc.nextInt();
if(high < jRyouiki[i]){
high = jRyouiki[i];
}
}
int umi = 0;
int island = 0;
int islandcnt = 0;
int maxIsland = 0;
while(high > umi){
for(int i = 0; i < field; i++){
if(umi < jRyouiki[i]){
island = i + 1;
if(i == (field-1)){
islandcnt++;
}
}else{
if(island != 0 ){
islandcnt++;
}
island = 0;
}
}
if(islandcnt > maxIsland){
maxIsland = islandcnt;
}
umi++;
islandcnt = 0;
island = 0;
}
System.out.println(maxIsland);
}
}
To be clear, it's probably ** a long run time due to monotonous looping **. Until now, I thought it would be good if it just worked, but I will try to write smart code that does not put a lot of processing load.
Continue…
Recommended Posts