📌  相关文章
📜  最小化所需的奖品数量,以便较小的价值在相邻对中获得较少的奖品

📅  最后修改于: 2021-09-17 07:48:13             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是找到所需的最小奖品数量,这样如果两个元素相邻,则值较小的元素与其相邻的值较大的元素相比,获得的奖品数量较少。
注:每个元素将获得至少一个奖品。

例子:

朴素的方法:遍历数组的元素,并为数组的每个元素找到元素左侧的连续较小元素,并在该索引的右侧找到连续的较小元素。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
 
#include 
 
using namespace std;
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
int findMinPrizes(int arr[], int n)
{
    int totalPrizes = 0, j, x, y;
 
    // Loop to iterate over every
    // elements of the array
    for (int i = 0; i < n; i++) {
        x = 1;
        j = i;
 
        // Loop to find the consecutive
        // smaller elements at left
        while (j > 0 && arr[j] > arr[j - 1]) {
            x++;
            j--;
        }
        j = i;
        y = 1;
 
        // Loop to find the consecutive
        // smaller elements at right
        while (j < n - 1 && arr[j] > arr[j + 1]) {
            y++;
            j++;
        }
 
        totalPrizes += max({ x, y });
    }
    cout << totalPrizes << endl;
 
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    findMinPrizes(arr, n);
}


Java
// Java implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
import java.util.*;
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int findMinPrizes(int arr[], int n)
{
    int totalPrizes = 0, j, x, y;
 
    // Loop to iterate over every
    // elements of the array
    for (int i = 0; i < n; i++)
    {
        x = 1;
        j = i;
 
        // Loop to find the consecutive
        // smaller elements at left
        while (j > 0 && arr[j] > arr[j - 1])
        {
            x++;
            j--;
        }
        j = i;
        y = 1;
 
        // Loop to find the consecutive
        // smaller elements at right
        while (j < n - 1 && arr[j] > arr[j + 1])
        {
            y++;
            j++;
        }
 
        totalPrizes += Math.max(x, y );
    }
    System.out.print(totalPrizes + "\n");
 
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3 };
    int n = arr.length;
 
    findMinPrizes(arr, n);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation to find the
# minimum prizes required such
# that adjacent smaller elements
# gets less number of prizes
 
# Function to find the minimum
# number of required such that
# adjacent smaller elements gets
# less number of prizes
def findMinPrizes(arr, n):
     
    totalPrizes = 0
     
    # Loop to iterate over every
    # elements of the array
    for i in range(n):
        x = 1
        j = i
         
        # Loop to find the consecutive
        # smaller elements at left
        while (j > 0 and arr[j] > arr[j - 1]):
            x += 1
            j -= 1
         
        j = i
        y = 1
         
        # Loop to find the consecutive
        # smaller elements at right
        while (j < n - 1 and arr[j] >
                             arr[j + 1]):
            y += 1
            j += 1
             
        totalPrizes += max(x, y)
         
    print(totalPrizes)
 
# Driver code
arr = [ 1, 2, 2, 3 ]
n = len(arr)
 
findMinPrizes(arr, n)
 
# This code is contributed by stutipathak31jan


C#
// C# implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
using System;
 
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int findMinPrizes(int []arr, int n)
{
    int totalPrizes = 0, j, x, y;
 
    // Loop to iterate over every
    // elements of the array
    for(int i = 0; i < n; i++)
    {
        x = 1;
        j = i;
 
        // Loop to find the consecutive
        // smaller elements at left
        while (j > 0 && arr[j] > arr[j - 1])
        {
            x++;
            j--;
        }
        j = i;
        y = 1;
 
        // Loop to find the consecutive
        // smaller elements at right
        while (j < n - 1 &&
          arr[j] > arr[j + 1])
        {
            y++;
            j++;
        }
        totalPrizes += Math.Max(x, y);
    }
    Console.Write(totalPrizes + "\n");
 
    return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3 };
    int n = arr.Length;
 
    findMinPrizes(arr, n);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


C++
// C++ implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
 
#include 
 
using namespace std;
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
int minPrizes(int arr[], int n)
{
    int dpLeft[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for (int i = 1; i < n; i++) {
 
        if (arr[i] > arr[i - 1]) {
 
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else {
 
            dpLeft[i] = 1;
        }
    }
 
    int dpRight[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for (int i = n - 2; i >= 0; i--) {
 
        if (arr[i] > arr[i + 1]) {
 
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else {
 
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for (int i = 0; i < n; i++) {
 
        totalPrizes += max(dpLeft[i],
                           dpRight[i]);
    }
    cout << totalPrizes << endl;
 
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    minPrizes(arr, n);
 
    return 0;
}


Java
// Java implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
import java.util.*;
 
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int minPrizes(int arr[], int n)
{
    int []dpLeft = new int[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for(int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
        {
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else
        {
            dpLeft[i] = 1;
        }
    }
 
    int []dpRight = new int[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for(int i = n - 2; i >= 0; i--)
    {
        if (arr[i] > arr[i + 1])
        {
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else
        {
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for(int i = 0; i < n; i++)
    {
        totalPrizes += Math.max(dpLeft[i],
                               dpRight[i]);
    }
    System.out.print(totalPrizes + "\n");
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3 };
    int n = arr.length;
 
    minPrizes(arr, n);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation to find the
# minimum prizes required such
# that adjacent smaller elements
# gets less number of prizes
 
# Function to find the minimum
# number of required such that
# adjacent smaller elements gets
# less number of prizes
def minPrizes(arr, n):
     
    dpLeft = [0] * n
    dpLeft[0] = 1
     
    # Loop to compute the smaller
    # elements at the left
    for i in range(n):
        if arr[i] > arr[i - 1]:
            dpLeft[i] = dpLeft[i - 1] + 1
             
        else:
            dpLeft[i] = 1
         
    dpRight = [0] * n
    dpRight[-1] = 1
     
    # Loop to find the smaller
    # elements at the right
    for i in range(n - 2, -1, -1):
        if arr[i] > arr[i + 1]:
            dpRight[i] = dpRight[i + 1] + 1
             
        else:
            dpRight[i] = 1
     
    totalPrizes = 0
     
    # Loop to find the minimum
    # prizes required
    for i in range(n):
        totalPrizes += max(dpLeft[i],
                           dpRight[i])
         
    print(totalPrizes)
 
# Driver code
arr = [ 1, 2, 2, 3 ]
n = len(arr)
     
minPrizes(arr, n)
 
# This code is contributed by stutipathak31jan


C#
// C# implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
using System;
 
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int minPrizes(int []arr, int n)
{
    int []dpLeft = new int[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for(int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
        {
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else
        {
            dpLeft[i] = 1;
        }
    }
 
    int []dpRight = new int[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for(int i = n - 2; i >= 0; i--)
    {
        if (arr[i] > arr[i + 1])
        {
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else
        {
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for(int i = 0; i < n; i++)
    {
        totalPrizes += Math.Max(dpLeft[i],
                               dpRight[i]);
    }
    Console.Write(totalPrizes + "\n");
    return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3 };
    int n = arr.Length;
 
    minPrizes(arr, n);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
6

性能分析:

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

有效的方法:这个想法是预先计算数组中每个元素左侧和右侧的连续较小元素的数量。这意味着,如果左侧的元素较小,则该元素左侧的所有较小元素也将小于当前元素。 IE

if (arr[i-1] < arr[i])
    smallerLeft[i] = smallerLeft[i-1] + 1

类似地,可以使用以下事实来计算连续的较小元素:如果右侧的元素大于当前元素,则右侧的连续较大元素也将大于当前元素。 IE

if (arr[i] < arr[i+1])
    smallerRight[i] = smallerRight[i+1] + 1

下面是上述方法的实现:

C++

// C++ implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
 
#include 
 
using namespace std;
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
int minPrizes(int arr[], int n)
{
    int dpLeft[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for (int i = 1; i < n; i++) {
 
        if (arr[i] > arr[i - 1]) {
 
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else {
 
            dpLeft[i] = 1;
        }
    }
 
    int dpRight[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for (int i = n - 2; i >= 0; i--) {
 
        if (arr[i] > arr[i + 1]) {
 
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else {
 
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for (int i = 0; i < n; i++) {
 
        totalPrizes += max(dpLeft[i],
                           dpRight[i]);
    }
    cout << totalPrizes << endl;
 
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    minPrizes(arr, n);
 
    return 0;
}

Java

// Java implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
import java.util.*;
 
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int minPrizes(int arr[], int n)
{
    int []dpLeft = new int[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for(int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
        {
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else
        {
            dpLeft[i] = 1;
        }
    }
 
    int []dpRight = new int[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for(int i = n - 2; i >= 0; i--)
    {
        if (arr[i] > arr[i + 1])
        {
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else
        {
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for(int i = 0; i < n; i++)
    {
        totalPrizes += Math.max(dpLeft[i],
                               dpRight[i]);
    }
    System.out.print(totalPrizes + "\n");
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3 };
    int n = arr.length;
 
    minPrizes(arr, n);
}
}
 
// This code is contributed by Amit Katiyar

蟒蛇3

# Python3 implementation to find the
# minimum prizes required such
# that adjacent smaller elements
# gets less number of prizes
 
# Function to find the minimum
# number of required such that
# adjacent smaller elements gets
# less number of prizes
def minPrizes(arr, n):
     
    dpLeft = [0] * n
    dpLeft[0] = 1
     
    # Loop to compute the smaller
    # elements at the left
    for i in range(n):
        if arr[i] > arr[i - 1]:
            dpLeft[i] = dpLeft[i - 1] + 1
             
        else:
            dpLeft[i] = 1
         
    dpRight = [0] * n
    dpRight[-1] = 1
     
    # Loop to find the smaller
    # elements at the right
    for i in range(n - 2, -1, -1):
        if arr[i] > arr[i + 1]:
            dpRight[i] = dpRight[i + 1] + 1
             
        else:
            dpRight[i] = 1
     
    totalPrizes = 0
     
    # Loop to find the minimum
    # prizes required
    for i in range(n):
        totalPrizes += max(dpLeft[i],
                           dpRight[i])
         
    print(totalPrizes)
 
# Driver code
arr = [ 1, 2, 2, 3 ]
n = len(arr)
     
minPrizes(arr, n)
 
# This code is contributed by stutipathak31jan

C#

// C# implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
using System;
 
class GFG{
 
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
static int minPrizes(int []arr, int n)
{
    int []dpLeft = new int[n];
 
    dpLeft[0] = 1;
 
    // Loop to compute the smaller
    // elements at the left
    for(int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
        {
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else
        {
            dpLeft[i] = 1;
        }
    }
 
    int []dpRight = new int[n];
 
    dpRight[n - 1] = 1;
 
    // Loop to find the smaller
    // elements at the right
    for(int i = n - 2; i >= 0; i--)
    {
        if (arr[i] > arr[i + 1])
        {
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else
        {
            dpRight[i] = 1;
        }
    }
 
    int totalPrizes = 0;
 
    // Loop to find the minimum
    // prizes required
    for(int i = 0; i < n; i++)
    {
        totalPrizes += Math.Max(dpLeft[i],
                               dpRight[i]);
    }
    Console.Write(totalPrizes + "\n");
    return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3 };
    int n = arr.Length;
 
    minPrizes(arr, n);
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
6

性能分析:

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