AtCoder is called TLE and talks about how to write beautiful code

The beginning of the matter

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);
    }
}

Cause and future

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

AtCoder is called TLE and talks about how to write beautiful code
How to write good code
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
[Java] How to output and write files!
Write code that is difficult to test
How to write and explain Dockerfile, docker-compose
Code that is difficult to debug and parse
JDBC promises and examples of how to write
Write code that is easy to maintain (Part 1)
How to write code that thinks object-oriented Ruby
Write code that is easy to maintain (Part 4)
How to write test code with Basic authentication
Write code that is easy to maintain (Part 3)
[Java] Types of comments and how to write them
To implement, write the test and then code the process
How to check CircleCI code and automatically deploy to Heroku
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to specify character code and line feed code in JAXB
[Code golf] Deflate the code and submit it to AtCoder [Compressed golf]
How to set character code and line feed code in Eclipse
How to write and notes when migrating from VB to JAVA
Think about how to divide MVC into M and V
Bit Tetris (how to write)
How to write java comments
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
[RSpec on Rails] How to write test code for beginners by beginners
About TestSize advocated by Google and how to realize TestSize by Java and Maven
Basics of Java development ~ How to write programs (variables and types) ~
Offline real-time how to write F03 ruby and C implementation example
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
I tested how to use Ruby's test / unit and rock-paper-scissors code.
Let's write a code that is easy to maintain (Part 2) Name
How to write the view when Vue is introduced in Rails?