📌  相关文章
📜  最大化索引的数量,使得元素大于其左侧的元素

📅  最后修改于: 2021-10-25 09:16:05             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是最大化索引的数量,使得元素大于其左侧的元素,即在重新排列数组后arr[i+1] > arr[i]
例子:

方法:这个问题可以使用贪心方法来解决。以下是步骤:

  1. 要获得索引的最大数量(比如i ),使得arr[i+1] > arr[i] ,排列arr[]的元素,使得所有唯一元素的集合首先出现,然后出现下一组唯一元素在第一组之后,直到所有元素都排列好。
    例如:
  1. 经过上述排列后,具有较高值的元素将不会成为给定条件的一部分,因为它后面跟着一个小于其自身的数字。
  2. 因此,满足给定条件的对的总数可以由下式给出:

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to find the maximum pairs
// such that arr[i+1] > arr[i]
void countPairs(int arr[], int N)
{
 
    // To store the frequency of the
    // element in arr[]
    unordered_map M;
 
    // Store the frequency in map M
    for (int i = 0; i < N; i++) {
        M[arr[i]]++;
    }
 
    int maxFreq = 0;
 
    // To find the maximum frequency
    // store in map M
    for (auto& it : M) {
        maxFreq = max(maxFreq,
                      it.second);
    }
 
    // Print the maximum number of
    // possible pairs
    cout << N - maxFreq << endl;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 8, 5, 9, 8, 8, 7,
                  7, 5, 7, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    countPairs(arr, N);
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
 
class GFG{
  
// Function to find the maximum pairs
// such that arr[i+1] > arr[i]
static void countPairs(int arr[], int N)
{
  
    // To store the frequency of the
    // element in arr[]
    HashMap mp = new HashMap();
  
    // Store the frequency in map M
    for (int i = 0; i < N; i++) {
        if(mp.containsKey(arr[i])){
            mp.put(arr[i], mp.get(arr[i])+1);
        }else{
            mp.put(arr[i], 1);
    }
    }
  
    int maxFreq = 0;
  
    // To find the maximum frequency
    // store in map M
    for (Map.Entry it : mp.entrySet()) {
        maxFreq = Math.max(maxFreq,
                      it.getValue());
    }
  
    // Print the maximum number of
    // possible pairs
    System.out.print(N - maxFreq +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
  
    int arr[] = { 1, 8, 5, 9, 8, 8, 7,
                  7, 5, 7, 7 };
    int N = arr.length;
  
    countPairs(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the above approach
 
# Function to find the maximum pairs
# such that arr[i + 1] > arr[i]
def countPairs(arr, N) :
 
    # To store the frequency of the
    # element in arr[]
    M = dict.fromkeys(arr, 0);
 
    # Store the frequency in map M
    for i in range(N) :
        M[arr[i]] += 1;
 
    maxFreq = 0;
 
    # To find the maximum frequency
    # store in map M
    for it in M.values() :
        maxFreq = max(maxFreq,it);
 
    # Print the maximum number of
    # possible pairs
    print(N - maxFreq);
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 8, 5, 9, 8, 8, 7, 7, 5, 7, 7 ];
    N = len(arr);
 
    countPairs(arr, N);
     
    # This code is contributed by AnkitRai01


C#
// C# program of the above approach
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to find the maximum pairs
// such that arr[i+1] > arr[i]
static void countPairs(int []arr, int N)
{
   
    // To store the frequency of the
    // element in []arr
    Dictionary mp = new Dictionary();
   
    // Store the frequency in map M
    for (int i = 0; i < N; i++) {
        if(mp.ContainsKey(arr[i])){
            mp[arr[i]] = mp[arr[i]]+1;
        }else{
            mp.Add(arr[i], 1);
    }
    }
   
    int maxFreq = 0;
   
    // To find the maximum frequency
    // store in map M
    foreach (KeyValuePair it in mp) {
        maxFreq = Math.Max(maxFreq,
                      it.Value);
    }
   
    // Print the maximum number of
    // possible pairs
    Console.Write(N - maxFreq +"\n");
}
   
// Driver Code
public static void Main(String[] args)
{
   
    int []arr = { 1, 8, 5, 9, 8, 8, 7,
                  7, 5, 7, 7 };
    int N = arr.Length;
   
    countPairs(arr, N);
}
}
  
// This code is contributed by Rajput-Ji


Javascript


输出:
7

时间复杂度: O(N),其中 N 是数组中元素的数量。
辅助空间: O(N),其中 N 是数组中元素的数量。