简体   繁体   English

超出圆形阵列旋转时间限制(TLE)

[英]Circular Array Rotation Time Limit Exceeded(TLE)

I am really confused why my java code is not working it is giving TLE on Code Monks on Hacker Earth.我真的很困惑为什么我的 Java 代码不起作用它在 Hacker Earth 上的 Code Monks 上给出了 TLE。 Here is the link to the question - Link to Question the first question MONK AND ROTATION这是问题的链接 -链接到问题第一个问题僧侣和旋转

import java.util.Scanner;
class TestClass {
    static int[] ar=new int[100001];
    public static void main(String args[] ){
        Scanner in=new Scanner(System.in);
        byte t=in.nextByte();
        while(--t>0){
            int n=in.nextInt();
            int k=in.nextInt()%n;
                for(int i=0;i<n-k;i++)
                    ar[i]=in.nextInt();
                for(int i=0;i<k;i++)
                    System.out.print(in.nextInt()+" ");
                for(int i=0;i<n-k;i++)
                    System.out.print(ar[i]+" ");
            System.out.println();
        }
    }
}

I don't know why is it giving TLE I think there is some infinite loop going.我不知道为什么它会给 TLE 我认为有一些无限循环。

the question at the site is-该网站的问题是-

Monk and Rotation Monk loves to perform different operations on arrays, and so being the principal of HackerEarth School, he assigned a task to his new student Mishki. Monk 和 Rotation Monk 喜欢对数组进行不同的操作,因此作为 HackerEarth School 的校长,他为他的新学生 Mishki 分配了一项任务。 Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. Mishki 将获得一个大小为 N 的整数数组 A 和一个整数 K ,她需要将数组以正确的方向旋转 K 步,然后打印结果数组。 As she is new to the school, please help her to complete the task.由于她是新来的学校,请帮助她完成任务。

Input: The first line will consists of one integer T denoting the number of test cases.输入:第一行将由一个整数 T 组成,表示测试用例的数量。 For each test case:对于每个测试用例:

  1. The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.第一行由两个整数 N 和 K 组成,N 是数组中元素的数量,K 表示旋转的步数。
  2. The next line consists of N space separated integers , denoting the elements of the array A. Output: Print the required array.下一行由 N 个空格分隔的整数组成,表示数组 A 的元素。输出:打印所需的数组。

Constraints:约束:

1<=T<=20
1<=N<=10^5
0<=K<=10^6
0<=A[i]<=10^6

Sample Input样本输入

1 1

5 2 5 2

1 2 3 4 5 1 2 3 4 5

Sample Output样本输出

4 5 1 2 3 4 5 1 2 3

Explanation解释

Here T is 1, which means one test case.这里 T 是 1,这意味着一个测试用例。

denoting the number of elements in the array and , denoting the number of steps of rotations.表示数组中元素的数量,表示旋转的步数。

The initial array is: In first rotation, 5 will come in the first position and all other elements will move to one position ahead from their current position.初始数组是:在第一次旋转中,5 将位于第一个位置,所有其他元素将从当前位置移动到一个位置。 Now, the resultant array will be现在,结果数组将是

In second rotation, 4 will come in the first position and all other elements will move to one position ahead from their current position.在第二次旋转中,4 将进入第一个位置,所有其他元素将从当前位置移动到一个位置。 Now, the resultant array will be现在,结果数组将是

Time Limit: 1.0 sec(s) for each input file Memory Limit: 256 MB Source Limit: 1024 KB时间限制:每个输入文件 1.0 秒内存限制:256 MB 源限制:1024 KB

Problem is here问题在这里

for(int i=0;i<n-k;i++) //this loop you are using for storing input elements in array
    ar[i]=in.nextInt();

for(int i=0;i<k;i++) // here you again taking the input you don't need this loop
    System.out.print(in.nextInt()+" ");

for(int i=0;i<n-k;i++)
    System.out.print(ar[i]+" ");

You also need to change the condition in while loop while(--t>0) to while(--t >= 0) .您还需要将 while 循环中的条件while(--t>0)更改为while(--t >= 0) It should be >= 0 other wise it will not work.它应该>= 0否则它将不起作用。 Other solution is to use post decrement while(t-- > 0)其他解决方案是使用 post decrement while(t-- > 0)

You are trying to print right rotation of the array.您正在尝试打印阵列的右旋转。 So you need to start printing the elements from index n - k .所以你需要从索引n - k开始打印元素。 Then you need to calculate the end index it is (n - k) + n this is because array has n elements.然后你需要计算结束索引它是(n - k) + n这是因为数组有n元素。 Then you can access array elements like this arr[i % n] .然后你可以访问这样的数组元素arr[i % n]

Here is the complete working solution这是完整的工作解决方案

class TestClass {
    static int[] ar = new int[100001];

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        byte t = in.nextByte();
        while (--t >= 0) {
            int n = in.nextInt();
            int k = in.nextInt() % n;
            
            for (int i = 0; i < n; i++)
                ar[i] = in.nextInt();
            
            for (int i = n - k; i < n + (n - k); i++)
                System.out.print(ar[i % n] + " ");
            System.out.println();
        }
    }
}

Think from a different perspective.从不同的角度思考。 Instead of splitting the string and converting it into an array and applying the iterative logic, we can apply a different logic.我们可以应用不同的逻辑,而不是拆分字符串并将其转换为数组并应用迭代逻辑。

The trick is you just need to find the position of the input string where we have to split only once.诀窍是你只需要找到我们只需要拆分一次的输入字符串的位置。 By that I mean,我的意思是,
input=>输入=>
6 2 //4 is the length of numbers and 2 is the index of rotation 6 2 //4是数字的长度,2是旋转的索引
1 2 3 4 5 6 //array (take input as a string using buffered reader) 1 2 3 4 5 6 //数组(使用缓冲读取器将输入作为字符串)

Here, we just need to split the array string at the 2nd last space ie 4th space.在这里,我们只需要在倒数第二个空格,即第四个空格处拆分数组字符串。 So the output can be achieved by just splitting the string once-因此,只需将字符串拆分一次即可实现输出-
5 6 1 2 3 4 5 6 1 2 3 4
first split- 5 6 + space + second split- 1 2 3 4第一次分裂 - 5 6 + 空格 + 第二次分裂 - 1 2 3 4
This logic worked for me and all the test cases passed.这个逻辑对我有用,所有的测试用例都通过了。

Also don't forget to cover the corner case scenario when array input string is just one number.当数组输入字符串只是一个数字时,也不要忘记涵盖极端情况。

Code Snippet-代码片段-

int count=0;
for(int k=0; k<arr.length(); k++) {
    if(arr.charAt(k)==' ')
        count++;
    if(count==size-rot) {
         System.out.println(arr.substring(k+1,arr.length())
             + " " + arr.substring(0,k));
         break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM