[英]Why does this code gives me timeout error?
I was solving this problem.我正在解决这个问题。
Given two sorted arrays arr1[] and arr2[] in non-decreasing order with size n and m.给定两个排序的 arrays arr1[] 和 arr2[] 以非递减顺序,大小为 n 和 m。 The task is to merge the two sorted arrays into one sorted array (in non-decreasing order).任务是将两个已排序的 arrays 合并为一个已排序的数组(按非递减顺序)。
Note: Expected time complexity is O((n+m) log(n+m)).注意:预期时间复杂度为 O((n+m) log(n+m))。 DO NOT use extra space.不要使用额外的空间。
The below code has the time complexity of O(n log m).以下代码的时间复杂度为 O(n log m)。 Still, it gives me a timeout error.尽管如此,它还是给了我一个超时错误。
Where am I going wrong?我哪里错了?
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0){
StringTokenizer st = new StringTokenizer(br.readLine());
int len1 = Integer.parseInt(st.nextToken());
int len2 = Integer.parseInt(st.nextToken());
int[] nums1 = new int[len1];
int[] nums2 = new int[len2];
st = new StringTokenizer(br.readLine());
for(int i = 0; i<len1; i++)
nums1[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
for(int i = 0; i<len2; i++)
nums2[i] = Integer.parseInt(st.nextToken());
int temp;
for(int i =0; i<len1; i++){
if (nums1[i] > nums2[0]){
temp = nums1[i];
nums1[i] = nums2[0];
nums2[0] = temp;
Heapify(nums2,0,len2);
}
}
for(int i = 0; i<len1; i++)
System.out.print(nums1[i]+" ");
Arrays.sort(nums2);
for(int i = 0; i<len2; i++)
System.out.print(nums2[i]+" ");
System.out.println();
}
}
static void Heapify(int[] nums, int i, int len){
int l = 2 * i+1;
int r = 2 * i + 2;
int smallest = i;
if (l < len && nums[l] < nums[i] ){
smallest = l;
}
if (r < len && nums[r] < nums[smallest] ){
smallest = r;
}
if (smallest != i){
int temp = nums[i];
nums[i] = nums[smallest];
nums[smallest] = temp;
Heapify(nums,smallest,len);
}
}
}
The website has some problems.网站有些问题。 Submitted same solution for many times.多次提交相同的解决方案。
The timeout was due to there were a lot of read and write operations involved.超时是由于涉及到大量的读写操作。
You optimised it for read operations using BufferedReader.您使用 BufferedReader 为读取操作优化了它。
I optimised it for write operations using BufferedWriter in the following implementation:我在以下实现中使用 BufferedWriter 为写操作优化了它:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0){
StringTokenizer st = new StringTokenizer(br.readLine());
int len1 = Integer.parseInt(st.nextToken());
int len2 = Integer.parseInt(st.nextToken());
int[] nums1 = new int[len1];
int[] nums2 = new int[len2];
st = new StringTokenizer(br.readLine());
for(int i = 0; i<len1; i++)
nums1[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
for(int i = 0; i<len2; i++)
nums2[i] = Integer.parseInt(st.nextToken());
int temp;
for(int i =0; i<len1; i++){
if (nums1[i] > nums2[0]){
temp = nums1[i];
nums1[i] = nums2[0];
nums2[0] = temp;
Heapify(nums2,0,len2);
}
}
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0; i<len1; i++)
log.write(nums1[i]+" ");
Arrays.sort(nums2);
for(int i = 0; i<len2; i++)
log.write(nums2[i]+" ");
log.write("\n");
log.flush();
}
}
static void Heapify(int[] nums, int i, int len){
int l = 2 * i+1;
int r = 2 * i + 2;
int smallest = i;
if (l < len && nums[l] < nums[i] ){
smallest = l;
}
if (r < len && nums[r] < nums[smallest] ){
smallest = r;
}
if (smallest != i){
int temp = nums[i];
nums[i] = nums[smallest];
nums[smallest] = temp;
Heapify(nums,smallest,len);
}
}
}
The Geeks for Geeks verdict is Accepted for the above code:上述代码接受Geeks for Geeks 判决:
PS: Try to submit this same solution for multiple times. PS:尝试多次提交相同的解决方案。 You will get correct answer as verdict.您将得到正确的答案作为判决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.