一个公司有N个员工,每个员工都有一些评分。员工根据他们的评级获得加薪,即评级较高的员工将获得更高的加薪。员工只知道其邻居的加息和评级,即在员工的左侧和右侧。
给定一个由N 个正整数组成的数组arr[]表示N 个员工的评级,任务是找到每个员工应该提高的最低加薪,这样没有员工会感到不公平。
注意:加息仅为正整数,评级始终大于零。
例子:
Input: arr[] = {1, 3, 5, 4}
Output: 1 2 3 1
Explanation:
The distribution of minimum hike for each employee must be:
1 + 2 + 3 + 1 = 6
Input: arr[] = {5, 3, 4, 2, 1, 6}
Output: 2 1 3 2 1 2
Explanation:
The distribution of minimum hike for each employee must be:
2 + 1 + 3 + 2 + 1 + 2 = 11
方法:这个问题可以使用贪心方法来解决。由于员工只知道他们的邻居的加息和评级,只有以下情况才是对给定评级适用的条件之一:
- 类型 1: H i – 1 > H i < H i + 1
- 类型 2: H i – 1 < H i < H i + 1
- 类型 3: H i – 1 > H i > H i + 1
- 类型 4: H i – 1 < H 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
- 现在我们不能根据相邻员工的加薪来减少当前员工的加薪,总是寻找增加的情况。
- Pass1(从左到右)与以前的员工加薪进行比较,并在需要时增加当前员工的加薪。
- 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));
}
}
蟒蛇3
# 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live