📌  相关文章
📜  具有最大元素的最小旋转,其值最多为它的索引

📅  最后修改于: 2022-05-13 01:56:10.014000             🧑  作者: Mango

具有最大元素的最小旋转,其值最多为它的索引

给定一个大小为N的数组arr[] ,该数组可以旋转任意次数,这样在旋转后,数组arr[]中小于或等于其索引的每个元素都将获得 1 分。任务是找到对应于最高分数的旋转索引K。如果有多个答案,则返回所有答案中最小的。

例子:

天真的方法:计算每次旋转的分数。然后在计算的总分中找出最高分,如果有多个分数,则返回最低分。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to rotate the array
void rotate(vector& arr, int N)
{
    int temp = arr[0], i;
    for (i = 0; i < N - 1; i++)
        arr[i] = arr[i + 1];
    arr[N - 1] = temp;
    return;
}
 
// Function to calculate the
// total score of a rotation
int calculate(vector& arr)
{
    int score = 0;
    for (int i = 0; i < arr.size(); i++) {
        if (arr[i] <= i) {
            score++;
        }
    }
    return score;
}
 
// Function to find the rotation index k
// that corresponds to the highest score
int bestIndex(vector& nums)
{
    int N = nums.size();
    int high_score = -1;
    int min_idx = 0;
 
    for (int i = 0; i < N; i++) {
        if (i != 0)
            // Rotates the array to left
            // by one position.
            rotate(nums, N);
 
        // Stores score of current rotation
        int cur_score = calculate(nums);
 
        if (cur_score > high_score) {
            min_idx = i;
            high_score = cur_score;
        }
    }
    return min_idx;
}
 
// Driver code
int main()
{
    vector arr = { 2, 3, 1, 4, 0 };
    cout << bestIndex(arr);
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
  // Function to rotate the array
  static void rotate(int[] arr, int N) {
    int temp = arr[0], i;
    for (i = 0; i < N - 1; i++)
      arr[i] = arr[i + 1];
    arr[N - 1] = temp;
    return;
  }
 
  // Function to calculate the
  // total score of a rotation
  static int calculate(int[] arr) {
    int score = 0;
    for (int i = 0; i < arr.length; i++) {
      if (arr[i] <= i) {
        score++;
      }
    }
    return score;
  }
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums) {
    int N = nums.length;
    int high_score = -1;
    int min_idx = 0;
 
    for (int i = 0; i < N; i++) {
      if (i != 0)
 
        // Rotates the array to left
        // by one position.
        rotate(nums, N);
 
      // Stores score of current rotation
      int cur_score = calculate(nums);
 
      if (cur_score > high_score) {
        min_idx = i;
        high_score = cur_score;
      }
    }
    return min_idx;
  }
 
  // Driver code
  public static void main(String args[]) {
    int[] arr = { 2, 3, 1, 4, 0 };
    System.out.println(bestIndex(arr));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# Python program for the above approach
 
# Function to rotate the array
def rotate(arr, N):
 
    temp = arr[0]
    for i in range(0, N-1):
        arr[i] = arr[i + 1]
    arr[N - 1] = temp
    return
 
# Function to calculate the
# total score of a rotation
def calculate(arr):
 
    score = 0
    for i in range(0, len(arr)):
        if (arr[i] <= i):
            score = score + 1
 
    return score
 
# Function to find the rotation index k
# that corresponds to the highest score
def bestIndex(nums):
 
    N = len(nums)
    high_score = -1
    min_idx = 0
 
    for i in range(0, N):
        if (i != 0):
           
            # Rotates the array to left
            # by one position.
            rotate(nums, N)
 
        # Stores score of current rotation
        cur_score = calculate(nums)
 
        if (cur_score > high_score):
            min_idx = i
            high_score = cur_score
 
    return min_idx
 
# Driver code
arr = [2, 3, 1, 4, 0]
print(bestIndex(arr))
 
# This code is contributed by Taranpreet


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to rotate the array
  static void rotate(int[] arr, int N)
  {
    int temp = arr[0], i;
    for (i = 0; i < N - 1; i++)
      arr[i] = arr[i + 1];
    arr[N - 1] = temp;
    return;
  }
 
  // Function to calculate the
  // total score of a rotation
  static int calculate(int[] arr)
  {
    int score = 0;
    for (int i = 0; i < arr.Length; i++) {
      if (arr[i] <= i) {
        score++;
      }
    }
    return score;
  }
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums)
  {
    int N = nums.Length;
    int high_score = -1;
    int min_idx = 0;
 
    for (int i = 0; i < N; i++) {
      if (i != 0)
        // Rotates the array to left
        // by one position.
        rotate(nums, N);
 
      // Stores score of current rotation
      int cur_score = calculate(nums);
 
      if (cur_score > high_score) {
        min_idx = i;
        high_score = cur_score;
      }
    }
    return min_idx;
  }
 
  // Driver code
  public static void Main()
  {
    int[] arr = { 2, 3, 1, 4, 0 };
    Console.Write(bestIndex(arr));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the rotation index k
// that corresponds to the highest score
int bestIndex(vector& nums)
{
    int N = nums.size();
    int gain = 0;
    int idx;
    priority_queue, greater > q;
 
    for (int i = 0; i < N; i++) {
        int diff = i - nums[i];
        if (diff >= 0) {
            gain++;
            q.push(diff);
        }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
        while (!q.empty() && (i > q.top())) {
            q.pop();
            current--;
        }
        if (nums[i - 1] <= (N - 1)) {
            current++;
            q.push(i + (N - 1) - nums[i - 1]);
        }
        if (current > gain) {
            idx = i;
            gain = current;
        }
    }
    return idx;
}
 
// Driver Code
int main()
{
    vector arr = { 2, 3, 1, 4, 0 };
    cout << bestIndex(arr);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
public class GFG
{
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums)
  {
    int N = nums.length;
    int gain = 0;
    int idx = 0;
    PriorityQueue q
      = new PriorityQueue();
 
    for (int i = 0; i < N; i++) {
      int diff = i - nums[i];
      if (diff >= 0) {
        gain++;
        q.add(diff);
      }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
      while (q.isEmpty() == false && (i > q.peek())) {
        q.remove();
        current--;
      }
      if (nums[i - 1] <= (N - 1)) {
        current++;
        q.add(i + (N - 1) - nums[i - 1]);
      }
      if (current > gain) {
        idx = i;
        gain = current;
      }
    }
    return idx;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int[] arr = { 2, 3, 1, 4, 0 };
    System.out.print(bestIndex(arr));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


C#
// C# program of the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums)
  {
    int N = nums.Length;
    int gain = 0;
    int idx = 0;
    List q = new List();
 
    for (int i = 0; i < N; i++) {
      int diff = i - nums[i];
      if (diff >= 0) {
        gain++;
        q.Add(diff);
        q.Sort();
        q.Reverse();
      }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
      while (i > q[0]) {
        q.RemoveAt(0);
        current--;
      }
      if (nums[i - 1] <= (N - 1)) {
        current++;
        q.Add(i + (N - 1) - nums[i - 1]);
        q.Sort();
        q.Reverse();
      }
      if (current > gain) {
        idx = i;
        gain = current;
      }
    }
    return idx-1;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 2, 3, 1, 4, 0 };
    Console.Write(bestIndex(arr));
  }
}
 
// This code is contributed by sanjoy_62.



输出
3

时间复杂度: O(N 2 )
辅助空间: O(1)

有效的方法:在这种方法中,

  • 首先计算输入的分数,然后按升序排列优先级队列( q )。
  • 然后将i – arr[i]的所有非负差异,比如diff放入优先级队列。
  • 之后,从1遍历到(N -1) 。每次移位完成,因为是左移,所以arr[i-1]变成 arr 的尾部, arr [i-1]变成arr[N-1]
  • 此外,所有diff[ i] 通过减少映射索引(由左移引起)变为diff[i] -1
  • 然后每次轮换后分数将改变2个案例:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the rotation index k
// that corresponds to the highest score
int bestIndex(vector& nums)
{
    int N = nums.size();
    int gain = 0;
    int idx;
    priority_queue, greater > q;
 
    for (int i = 0; i < N; i++) {
        int diff = i - nums[i];
        if (diff >= 0) {
            gain++;
            q.push(diff);
        }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
        while (!q.empty() && (i > q.top())) {
            q.pop();
            current--;
        }
        if (nums[i - 1] <= (N - 1)) {
            current++;
            q.push(i + (N - 1) - nums[i - 1]);
        }
        if (current > gain) {
            idx = i;
            gain = current;
        }
    }
    return idx;
}
 
// Driver Code
int main()
{
    vector arr = { 2, 3, 1, 4, 0 };
    cout << bestIndex(arr);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
public class GFG
{
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums)
  {
    int N = nums.length;
    int gain = 0;
    int idx = 0;
    PriorityQueue q
      = new PriorityQueue();
 
    for (int i = 0; i < N; i++) {
      int diff = i - nums[i];
      if (diff >= 0) {
        gain++;
        q.add(diff);
      }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
      while (q.isEmpty() == false && (i > q.peek())) {
        q.remove();
        current--;
      }
      if (nums[i - 1] <= (N - 1)) {
        current++;
        q.add(i + (N - 1) - nums[i - 1]);
      }
      if (current > gain) {
        idx = i;
        gain = current;
      }
    }
    return idx;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int[] arr = { 2, 3, 1, 4, 0 };
    System.out.print(bestIndex(arr));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

C#

// C# program of the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the rotation index k
  // that corresponds to the highest score
  static int bestIndex(int[] nums)
  {
    int N = nums.Length;
    int gain = 0;
    int idx = 0;
    List q = new List();
 
    for (int i = 0; i < N; i++) {
      int diff = i - nums[i];
      if (diff >= 0) {
        gain++;
        q.Add(diff);
        q.Sort();
        q.Reverse();
      }
    }
 
    int current = gain;
    idx = 0;
 
    for (int i = 1; i <= N - 1; i++) {
      while (i > q[0]) {
        q.RemoveAt(0);
        current--;
      }
      if (nums[i - 1] <= (N - 1)) {
        current++;
        q.Add(i + (N - 1) - nums[i - 1]);
        q.Sort();
        q.Reverse();
      }
      if (current > gain) {
        idx = i;
        gain = current;
      }
    }
    return idx-1;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 2, 3, 1, 4, 0 };
    Console.Write(bestIndex(arr));
  }
}
 
// This code is contributed by sanjoy_62.


输出
3

时间复杂度: O(N * logN)
辅助空间: O(N)