📌  相关文章
📜  每位员工的最低加薪幅度,使任何员工都不会感到不公平

📅  最后修改于: 2021-05-17 19:15:06             🧑  作者: Mango

公司中有N名员工,每位员工都有一些等级。根据等级给员工加薪,即等级更高的员工加薪。员工只知道邻居的加薪幅度和等级,即员工的左侧和右侧。
给定N个正整数的arr []数组,该数组代表N个雇员的等级,任务是找到每个雇员应提高的最低加薪幅度,以使任何雇员都不会感到不公平。

注意:加息仅是正整数,并且评级始终大于零。

例子:

方法:此问题可以使用贪婪方法解决。由于员工只知道自己的邻居的加薪和等级,因此遵循以下条件将是在给定等级上成立的条件之一:

  1. 类型1: H i – 1 > H i i + 1
  2. 类型2: H i – 1 i i + 1
  3. 类型3: H i – 1 > H i > H i + 1
  4. 类型4: H i – 1 i > H i + 1

根据上述条件,为每位员工设置每位员工的加薪为:

  • 对于类型1:将加息设置为1
  • 对于类型2:将他的远足提升为H i-1 +1
  • 对于类型3:将他的远足次数提高H i + 1 +1
  • 对于类型4:将他的远足提高max(H i-1 ,H i + 1 )+ 1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
#define INF 1e9
 
// Function that print minimum hike
void findMinHike(vector arr,
                 int n)
{
 
    // Insert INF at begin and
    // end of array
    arr.insert(arr.begin(), INF);
    arr.push_back(INF);
 
    // To store hike of each employee
    vector hike(n + 2, 0);
 
    // for Type 1 employee
    for (int i = 1; i <= n; i++) {
 
        if (arr[i - 1] >= arr[i]
            && arr[i] <= arr[i + 1]) {
            hike[i] = 1;
        }
    }
 
    // for Type 2 employee
    for (int i = 1; i <= n; i++) {
        if (arr[i - 1] < arr[i]
            && arr[i] <= arr[i + 1]) {
            hike[i] = hike[i - 1] + 1;
        }
    }
 
    // for Type 3 employee
    for (int i = 1; i <= n; i++) {
        if (arr[i - 1] >= arr[i]
            && arr[i] > arr[i + 1]) {
            hike[i] = hike[i + 1] + 1;
        }
    }
 
    // for Type 4 employee
    for (int i = 1; i <= n; i++) {
        if (arr[i - 1] < arr[i]
            && arr[i] > arr[i + 1]) {
            hike[i] = max(hike[i - 1],
                          hike[i + 1])
                      + 1;
        }
    }
 
    // Print the min hike for each employee
    for (int i = 1; i <= n; i++) {
        cout << hike[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array of rating of employees
    vector arr = { 5, 3, 4, 2, 1, 6 };
 
    // Function Call
    findMinHike(arr, arr.size());
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
   
static final int INF = 10000009;
 
// Function that print minimum hike
static void findMinHike(Vector arr,
                        int n)
{
  // Insert INF at begin and
  // end of array
  arr.add(0, INF);
  arr.add(INF);
 
  // To store hike of
  // each employee
  int []hike = new int[n + 2];
 
  // for Type 1 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr.get(i - 1) >= arr.get(i) &&
        arr.get(i) <= arr.get(i + 1))
    {
      hike[i] = 1;
    }
  }
 
  // For Type 2 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr.get(i - 1) < arr.get(i) &&
        arr.get(i) <= arr.get(i + 1))
    {
      hike[i] = hike[i - 1] + 1;
    }
  }
 
  // For Type 3 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr.get(i - 1) >= arr.get(i) &&
        arr.get(i) > arr.get(i + 1))
    {
      hike[i] = hike[i + 1] + 1;
    }
  }
 
  // For Type 4 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr.get(i - 1) < arr.get(i) &&
        arr.get(i) > arr.get(i + 1))
    {
      hike[i] = Math.max(hike[i - 1],
                         hike[i + 1]) + 1;
    }
  }
 
  // Print the min hike for each employee
  for (int i = 1; i <= n; i++)
  {
    System.out.print(hike[i] + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array of rating of employees
  Vector arr = new Vector<>();
   
  arr.add(5);
  arr.add(3);
  arr.add(4);
  arr.add(2);
  arr.add(1);
  arr.add(6);
   
  // Function Call
  findMinHike(arr, arr.size());
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
INF = 1e9
 
# Function that prminimum hike
def findMinHike(arr,n):
 
    # Insert INF at begin and
    # end of array
    arr.insert(0, INF)
    arr.append(INF)
 
    # To store hike of each employee
    hike = [0] * (n + 2)
 
    # For Type 1 employee
    for i in range(1, n + 1):
        if (arr[i - 1] >= arr[i] and
            arr[i] <= arr[i + 1]):
            hike[i] = 1
 
    # For Type 2 employee
    for i in range(1, n + 1):
        if (arr[i - 1] < arr[i] and
            arr[i] <= arr[i + 1]):
            hike[i] = hike[i - 1] + 1
 
    # For Type 3 employee
    for i in range(1, n + 1):
        if (arr[i - 1] >= arr[i] and
            arr[i] > arr[i + 1]):
            hike[i] = hike[i + 1] + 1
             
    # For Type 4 employee
    for i in range(1, n + 1):
        if (arr[i - 1] < arr[i] and
            arr[i] > arr[i + 1]):
            hike[i] = max(hike[i - 1],
                          hike[i + 1]) + 1
                           
    # Print the min hike for each employee
    for i in range(1, n + 1):
        print(hike[i], end = " ")
         
# Driver Code
if __name__ == '__main__':
     
    # Given array of rating of employees
    arr = [ 5, 3, 4, 2, 1, 6 ]
 
    # Function Call
    findMinHike(arr, len(arr))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
   
static readonly int INF = 10000009;
 
// Function that print minimum hike
static void findMinHike(List arr,
                        int n)
{
  // Insert INF at begin and
  // end of array
  arr.Insert(0, INF);
  arr.Add(INF);
 
  // To store hike of
  // each employee
  int []hike = new int[n + 2];
 
  // for Type 1 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr[i - 1] >= arr[i] &&
        arr[i] <= arr[i + 1])
    {
      hike[i] = 1;
    }
  }
 
  // For Type 2 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr[i - 1] < arr[i] &&
        arr[i] <= arr[i + 1])
    {
      hike[i] = hike[i - 1] + 1;
    }
  }
 
  // For Type 3 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr[i - 1] >= arr[i] &&
        arr[i] > arr[i + 1])
    {
      hike[i] = hike[i + 1] + 1;
    }
  }
 
  // For Type 4 employee
  for (int i = 1; i <= n; i++)
  {
    if (arr[i - 1] < arr[i] &&
        arr[i] > arr[i + 1])
    {
      hike[i] = Math.Max(hike[i - 1],
                         hike[i + 1]) + 1;
    }
  }
 
  // Print the min hike for
  // each employee
  for (int i = 1; i <= n; i++)
  {
    Console.Write(hike[i] + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array of rating of employees
  List arr = new List();
   
  arr.Add(5);
  arr.Add(3);
  arr.Add(4);
  arr.Add(2);
  arr.Add(1);
  arr.Add(6);
   
  // Function Call
  findMinHike(arr, arr.Count);
}
}
 
// This code is contributed by gauravrajput1


C++
// C++ program to find the minimum hike of each
// employee such that no adjacent employee feels
// unfair
#include 
using namespace std;
 
void findMinimumHike(int ratings[],int employees)
{
    int hikes[employees];
     
    // As hikes are positive integers, keeping
    // minimum value
    for(int i = 0; i < employees; i++)
    {
        hikes[i] = 1;
    }
     
    // Pass-1: compare with previous employee
    for(int i = 1; i < employees; i++)
    {
        if (ratings[i - 1] < ratings[i] &&
              hikes[i - 1] >= hikes[i])
        {
            hikes[i] = hikes[i - 1] + 1;
        }
    }
     
    // Pass-2: compare with Next employee
    for(int i = employees - 2; i >= 0; i--)
    {
        if (ratings[i] > ratings[i + 1] &&
              hikes[i + 1] >= hikes[i])
        {
            hikes[i] = hikes[i + 1] + 1;
        }
    }
     
    // Result
    cout << "[";
    int i;
    for(i = 0; i < employees - 1; i++)
    {
        cout << hikes[i] << ", ";
    }
    cout << hikes[i] << "]" << endl;
}
 
// Driver Code
int main()
{
    int data[] = { 5, 3, 4, 2, 1, 6 };
     
    // Function Call
    findMinimumHike(data, sizeof(data)/sizeof(data[0]));
     
    int data1[] = { 1, 3, 5, 4 };
     
    // Function Call
    findMinimumHike(data1, sizeof(data1)/sizeof(data1[0]));
     
    int data2[] = { 1, 4 };
     
    // Function Call
    findMinimumHike(data2, sizeof(data2)/sizeof(data2[0]));
    return 0;
}
 
// This code is contributed by rag2127


Java
// Java Program to find the minimum hike of each employee
// such that no adjacent employee feels unfair
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    public static int[] findMinimumHike(int[] ratings,
                                        int employees)
    {
        int[] hikes = new int[employees];
 
        // As hikes are positive integers, keeping minimum
        // value
        for (int i = 0; i < employees; i++)
            hikes[i] = 1;
 
        // Pass-1: compare with previous employee
        for (int i = 1; i < employees; i++) {
            if (ratings[i - 1] < ratings[i]
                && hikes[i - 1] >= hikes[i])
                hikes[i] = hikes[i - 1] + 1;
        }
 
        // Pass-2: compare with Next employee
        for (int i = employees - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]
                && hikes[i + 1] >= hikes[i])
                hikes[i] = hikes[i + 1] + 1;
        }
 
        return hikes;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] data = new int[] { 5, 3, 4, 2, 1, 6 };
 
        // Function Call
        int[] result = findMinimumHike(data, data.length);
        // result -> [2, 1, 3, 2, 1, 2]
        System.out.println(Arrays.toString(result));
 
        data = new int[] { 1, 3, 5, 4 };
 
        // Function Call
        result = findMinimumHike(data, data.length);
        // result -> [1, 2, 3, 1]
        System.out.println(Arrays.toString(result));
 
        data = new int[] { 1, 4 };
 
        // Function Call
        result = findMinimumHike(data, data.length);
        // result -> [1, 2]
        System.out.println(Arrays.toString(result));
    }
}


Python3
# Python Program to find the minimum hike of each employee
# such that no adjacent employee feels unfair
def findMinimumHike(ratings, employees):
   
    # As hikes are positive integers, keeping minimum
    # value
    hikes = [1 for i in range(employees)]
 
    # Pass-1: compare with previous employee
    for i in range(1,employees):
        if(ratings[i - 1] < ratings[i] and hikes[i - 1] >= hikes[i]):
            hikes[i] = hikes[i - 1] + 1;
             
    # Pass-2: compare with Next employee
    for i in range(employees - 2, -1, -1):
        if(ratings[i] > ratings[i + 1] and hikes[i + 1] >= hikes[i]):
            hikes[i] = hikes[i + 1] + 1;
    return hikes
 
# Driver Code
data = [ 5, 3, 4, 2, 1, 6 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [2, 1, 3, 2, 1, 2]
print(result)
data = [1, 3, 5, 4 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2, 3, 1]
print(result)
data = [1, 4]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2]
print(result)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to find the minimum hike
// of each employee such that no
// adjacent employee feels unfair
using System;
 
class GFG{
 
public static int[] findMinimumHike(int[] ratings,
                                    int employees)
{
    int[] hikes = new int[employees];
 
    // As hikes are positive integers, keeping
    // minimum value
    for(int i = 0; i < employees; i++)
        hikes[i] = 1;
 
    // Pass-1: compare with previous employee
    for(int i = 1; i < employees; i++)
    {
        if (ratings[i - 1] < ratings[i] &&
              hikes[i - 1] >= hikes[i])
            hikes[i] = hikes[i - 1] + 1;
    }
 
    // Pass-2: compare with Next employee
    for(int i = employees - 2; i >= 0; i--)
    {
        if (ratings[i] > ratings[i + 1] &&
              hikes[i + 1] >= hikes[i])
            hikes[i] = hikes[i + 1] + 1;
    }
    return hikes;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] data = new int[]{ 5, 3, 4, 2, 1, 6 };
 
    // Function Call
    int[] result = findMinimumHike(data, data.Length);
     
    // result -> [2, 1, 3, 2, 1, 2]
    Console.WriteLine("[" + String.Join(",", result) + "]");
 
    data = new int[]{ 1, 3, 5, 4 };
 
    // Function Call
    result = findMinimumHike(data, data.Length);
     
    // result -> [1, 2, 3, 1]
    Console.WriteLine("[" + String.Join(",", result) + "]");
 
    data = new int[]{ 1, 4 };
 
    // Function Call
    result = findMinimumHike(data, data.Length);
    // result -> [1, 2]
    Console.WriteLine("[" + String.Join(",", result) + "]");
}
}
 
// This code is contributed by Amit Katiyar


输出
2 1 3 2 1 2 

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

方法2 :(使用2次通过)

  1. 最初为每位员工分配最小的加薪幅度,即1
  2. 现在我们不能根据相邻员工的加薪来减少当前的员工加薪,而是总是寻找增加的案例。
  3. Pass1(从左至>右)与以前的员工加薪进行比较,并根据需要增加当前员工的加薪。
  4. Pass2(从右至>左)与下一个员工加薪进行比较,并根据需要增加当前员工的加薪。

下面是上述方法的实现:

C++

// C++ program to find the minimum hike of each
// employee such that no adjacent employee feels
// unfair
#include 
using namespace std;
 
void findMinimumHike(int ratings[],int employees)
{
    int hikes[employees];
     
    // As hikes are positive integers, keeping
    // minimum value
    for(int i = 0; i < employees; i++)
    {
        hikes[i] = 1;
    }
     
    // Pass-1: compare with previous employee
    for(int i = 1; i < employees; i++)
    {
        if (ratings[i - 1] < ratings[i] &&
              hikes[i - 1] >= hikes[i])
        {
            hikes[i] = hikes[i - 1] + 1;
        }
    }
     
    // Pass-2: compare with Next employee
    for(int i = employees - 2; i >= 0; i--)
    {
        if (ratings[i] > ratings[i + 1] &&
              hikes[i + 1] >= hikes[i])
        {
            hikes[i] = hikes[i + 1] + 1;
        }
    }
     
    // Result
    cout << "[";
    int i;
    for(i = 0; i < employees - 1; i++)
    {
        cout << hikes[i] << ", ";
    }
    cout << hikes[i] << "]" << endl;
}
 
// Driver Code
int main()
{
    int data[] = { 5, 3, 4, 2, 1, 6 };
     
    // Function Call
    findMinimumHike(data, sizeof(data)/sizeof(data[0]));
     
    int data1[] = { 1, 3, 5, 4 };
     
    // Function Call
    findMinimumHike(data1, sizeof(data1)/sizeof(data1[0]));
     
    int data2[] = { 1, 4 };
     
    // Function Call
    findMinimumHike(data2, sizeof(data2)/sizeof(data2[0]));
    return 0;
}
 
// This code is contributed by rag2127

Java

// Java Program to find the minimum hike of each employee
// such that no adjacent employee feels unfair
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    public static int[] findMinimumHike(int[] ratings,
                                        int employees)
    {
        int[] hikes = new int[employees];
 
        // As hikes are positive integers, keeping minimum
        // value
        for (int i = 0; i < employees; i++)
            hikes[i] = 1;
 
        // Pass-1: compare with previous employee
        for (int i = 1; i < employees; i++) {
            if (ratings[i - 1] < ratings[i]
                && hikes[i - 1] >= hikes[i])
                hikes[i] = hikes[i - 1] + 1;
        }
 
        // Pass-2: compare with Next employee
        for (int i = employees - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]
                && hikes[i + 1] >= hikes[i])
                hikes[i] = hikes[i + 1] + 1;
        }
 
        return hikes;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] data = new int[] { 5, 3, 4, 2, 1, 6 };
 
        // Function Call
        int[] result = findMinimumHike(data, data.length);
        // result -> [2, 1, 3, 2, 1, 2]
        System.out.println(Arrays.toString(result));
 
        data = new int[] { 1, 3, 5, 4 };
 
        // Function Call
        result = findMinimumHike(data, data.length);
        // result -> [1, 2, 3, 1]
        System.out.println(Arrays.toString(result));
 
        data = new int[] { 1, 4 };
 
        // Function Call
        result = findMinimumHike(data, data.length);
        // result -> [1, 2]
        System.out.println(Arrays.toString(result));
    }
}

Python3

# Python Program to find the minimum hike of each employee
# such that no adjacent employee feels unfair
def findMinimumHike(ratings, employees):
   
    # As hikes are positive integers, keeping minimum
    # value
    hikes = [1 for i in range(employees)]
 
    # Pass-1: compare with previous employee
    for i in range(1,employees):
        if(ratings[i - 1] < ratings[i] and hikes[i - 1] >= hikes[i]):
            hikes[i] = hikes[i - 1] + 1;
             
    # Pass-2: compare with Next employee
    for i in range(employees - 2, -1, -1):
        if(ratings[i] > ratings[i + 1] and hikes[i + 1] >= hikes[i]):
            hikes[i] = hikes[i + 1] + 1;
    return hikes
 
# Driver Code
data = [ 5, 3, 4, 2, 1, 6 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [2, 1, 3, 2, 1, 2]
print(result)
data = [1, 3, 5, 4 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2, 3, 1]
print(result)
data = [1, 4]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2]
print(result)
 
# This code is contributed by avanitrachhadiya2155

C#

// C# program to find the minimum hike
// of each employee such that no
// adjacent employee feels unfair
using System;
 
class GFG{
 
public static int[] findMinimumHike(int[] ratings,
                                    int employees)
{
    int[] hikes = new int[employees];
 
    // As hikes are positive integers, keeping
    // minimum value
    for(int i = 0; i < employees; i++)
        hikes[i] = 1;
 
    // Pass-1: compare with previous employee
    for(int i = 1; i < employees; i++)
    {
        if (ratings[i - 1] < ratings[i] &&
              hikes[i - 1] >= hikes[i])
            hikes[i] = hikes[i - 1] + 1;
    }
 
    // Pass-2: compare with Next employee
    for(int i = employees - 2; i >= 0; i--)
    {
        if (ratings[i] > ratings[i + 1] &&
              hikes[i + 1] >= hikes[i])
            hikes[i] = hikes[i + 1] + 1;
    }
    return hikes;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] data = new int[]{ 5, 3, 4, 2, 1, 6 };
 
    // Function Call
    int[] result = findMinimumHike(data, data.Length);
     
    // result -> [2, 1, 3, 2, 1, 2]
    Console.WriteLine("[" + String.Join(",", result) + "]");
 
    data = new int[]{ 1, 3, 5, 4 };
 
    // Function Call
    result = findMinimumHike(data, data.Length);
     
    // result -> [1, 2, 3, 1]
    Console.WriteLine("[" + String.Join(",", result) + "]");
 
    data = new int[]{ 1, 4 };
 
    // Function Call
    result = findMinimumHike(data, data.Length);
    // result -> [1, 2]
    Console.WriteLine("[" + String.Join(",", result) + "]");
}
}
 
// This code is contributed by Amit Katiyar
输出
[2, 1, 3, 2, 1, 2]
[1, 2, 3, 1]
[1, 2]

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