https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424
始终超时,Java还是太慢
import java.io.*;
import java.util.StringTokenizer;
public class Main{
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
init(System.in);
int n = nextInt();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = nextInt() + arr[i - 1];
}
int m = nextInt();
for (int i = 0; i < m; i++) {
int left = nextInt();
int right = nextInt();
if (left > right) {
int temp = right;
right = left;
left = temp;
}
out.println(Math.min(arr[right - 1] - arr[left - 1], arr[n] - arr[right - 1] + arr[left - 1]));
}
out.flush();
out.close();
}
private static void init(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = new StringTokenizer("");
}
private static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
本文介绍了一个Java程序在处理数组求最小值问题时遇到的性能瓶颈,并提供了具体的代码实现。通过分析可以发现,作者试图解决的问题是在给定数组区间内找到一个特定的最小值,但是由于方法的实现方式,导致了效率问题。
6万+





