这段代码可以进一步优化吗?

时间:2017-06-10 21:59:20

标签: java time complexity-theory

鉴于this challenge

challenge

我尝试了什么:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        int A, B;
        long K;
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int M = input.nextInt();

        long[] array = new long[N];

        for(long i=1; i<=M; i++) {
            A = input.nextInt();
            B = input.nextInt();
            K = input.nextInt();
            for(int j=A; j<=B; j++) {
                array[j-1] += K;
            }
        }
       long max = 0;
       for(int i=1; i<array.length; i++) {
            if(max < array[i]) {
                max = array[i];
            }
        }
        System.out.println(max);
    }
}  

问题是:前7个案例是正确的,除了最后7个案例 - “因超时而终止

3 个答案:

答案 0 :(得分:0)

使K成为一个int,而不是一个long ...它将不会被赋予任何大于int的值... K = input.nextInt()该方法返回一个int ...

答案 1 :(得分:0)

在Java 8中使用IntStream会加速操作。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class StreamUse {

    public static void main(String[] args) {

        // some file that contains the input
        File file = new File("C:\\Users\\Yahya\\Desktop\\input.txt");
        Scanner input;

        long startTime, endTime; // just to test how long time it will take

        try {
            input = new Scanner(file);

            // read the first line which contains the number of elements and operations
            String firstLine = input.nextLine(); 
            // parse the line (the first is N)
            int N = Integer.parseInt(firstLine.split(" ")[0]);
            // you don't really need M because it'll read the entire file
            //int M = Integer.parseInt(firstLine.split(" ")[1]);

             startTime = System.currentTimeMillis();
             long[] array = new long[N+2];

             // fill the array with zeros
             IntStream.range(0, N).forEach(i -> array[i]=0);

             // while the file contains more input
             while(input.hasNextLine()){
                 // read every line and parse it
                 String[] line = input.nextLine().split(" ");

                 int A = Integer.parseInt(line[0]);
                 int B =Integer.parseInt(line[1]);
                 long K = Long.parseLong(line[2]);

                 //increment the elements that specified by range
                 // of indices by K
                 IntStream.rangeClosed(A, B).forEach(i -> array[i]+=K); 
             }
             // return the max if any
             Arrays.stream(array).max().ifPresent(System.out::println);

             endTime = System.currentTimeMillis();

             System.out.println("Time Consumed: " + (endTime - startTime) + " Millisecond");

        } catch (FileNotFoundException e) {
            System.out.println("Could not find the file");
        }   
    }
}

鉴于此输入格式:

  

第一行将包含由单个分隔的两个整数N和M.   空间。   接下来的M行将包含三个整数a,b和k分开   一个空间。

使用文件内容(注意数字有多大):

10000 3
1 2000 100000
1500 5000 200000
4000 9900 999999 

<强>测试

1199999
Time Consumed: 281 Millisecond

答案 2 :(得分:-1)

朋友我接受了你的问题,现在终于解决了: 检查优化代码,我对此代码采取了不同的方法。 我所做的是存储给出的更改,然后应用它们,如果您不理解,请发表评论。 它的工作我已经测试过了。

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

  public static void main(String[] args) {

      Scanner scn = new Scanner (System.in);

      int N = scn.nextInt();
      int M = scn.nextInt();

      Long [] hello = new Long [N+2];

      for (int pro = 0; pro < M ; pro++){
         int a = scn.nextInt();
         int b = scn.nextInt();
         long k = scn.nextInt();
         if (hello[a] == (Long) null) hello[a] = (long)0;
         if (hello[b+1] == (Long) null) hello [b+1] = (long)0;
         hello[a] += k;
         hello [b+1] += k * -1;

      }

    //compile the arry and find the largest
      boolean editLargest = true;
      Long largest = hello[0];
      long eK = 0;
      for (int pro = 0; pro < hello.length-1 ; pro++){
          if (hello [pro] == (Long)null)continue;
          if (editLargest){
                largest = hello[pro];
                editLargest = false;
          }
          eK += hello[pro];
          if (largest < eK) largest = eK;
      }
      System.out.println (largest);

  }
}

结帐屏幕   https://i.stack.imgur.com/lzqAq.png