Question
在一家跨国公司,员工被要求根据他们的身高排成一排。他们总是随意选择自己的位置来对经理不满。一天晚上,员工得知他们严格的经理从那天早上就秘密记录了他们的座位,并且他将在第二天早上检查他们的位置,以确保他们完全一样。
每个员工只记得那天早上的一件事:他左边的人数就是他的人。
有n名员工,每名员工的身高在1到n之间。使用此信息,您必须从那天早上重建座位安排。
您将获得一个int [],其中第i个元素表示员工左侧高度为i的较高员工数(其中i为基于1的索引)。返回包含员工从左到右的高度的int []。
注意:输入保证产生有效且唯一的输出。
输入规格: 你的功能应该接受以下的输入: 输入1:n 输入2:n个整数的数组(左[])
输出规格: 你需要在行中从左到右返回包含员工高度的int []。
Example :
Input:
input 1: 4
input 2: {2,1,1,0}
Output: {4,2,1,3}
Explanation:
Employee of height 1 remembered there were 2 employees taller than him to his left.
Employee of height 2 remembered there were 1 employees taller than him to his left.
Employee of height 3 remembered there were 1 employees taller than him to his left.
Employee of height 4 remembered there were no employees taller than him to his left.
从左到右的原始顺序必须是4,2,1,3。这种排序满足所有四个条件。例如,员工左侧正好有两名员工,身高1比他高(身高4和2)。不同的顺序,如4,3,1,2
,满足一些但不是全部的四个条件。 in this incorrect ordering, there are two employees to the left of employee with height 2 that are taller then him(height 4 and 3), but input states that there was only one.
My understanding:
如果我正确地理解了这个问题,
Input1 = 4 (Value of n is 4)
Input2 = {2, 1, 1, 0},
One of the output would be 4 2 1 3
这意味着,
The Employee1 has height 1 and there were 2 people left to him [4 2]
The Employee2 has height 2 and there was 1 people left to him [4]
The Employee3 has height 3 and there was 1 people left to him [1] but how? height 3 is taller than 1
The Employee4 has height 4 and there was 0 people left to him[No one is left of 4]
所以一个排序是4 2 1 3
,它符合我们的条件。
但是当我们尝试使用4 3 1 2
时,这也满足了所有条件。但我无法从解释中理解in this incorrect ordering, there are two employees to the left of employee with height 2 that are taller then him(height 4 and 3), but input states that there was only one.
但输入说,有2名员工留给他而不是一个......
我对问题解释感到困惑。
如果我在任何地方出错,请帮助我。
因为对我来说4 3 2 1
满足所有条件
请帮我理解这个问题。一旦我理解了这一点,我就会为此编写逻辑。
这个问题与
you are given a int[], the ith element of which represents the number of taller employees to the left of the employee with height i
为什么不能4 3 1 2
序列?
答案 0 :(得分:1)
问题1)问题与
有什么关系给你一个int [],其中第i个元素表示数字 高度为员工左侧的较高员工
让我们尝试从你给出的例子中理解它
Example :
Input:
input 1: 4
input 2: {2,1,1,0}
并附有相应的解释 -
Explanation:
Employee of height 1 remembered there were 2 employees taller than him to his left.
Employee of height 2 remembered there were 1 employees taller than him to his left.
Employee of height 3 remembered there were 1 employees taller than him to his left.
Employee of height 4 remembered there were no employees taller than him to his left.
这里,输入2是基于1的索引数组。索引对应于员工身高。这些数值对应于他记得左边有多少高级员工。数组的第一个值是2.这意味着身高1的员工记得他左边有2名员工比他高。同样,第二个值1表示身高2的员工记得他左边有一名比他高的员工。第三个值1表示高度为3的员工,他的左边有一名比他高的员工。最后,第四个和最大的索引值为0.这意味着在这种情况下,身高4的最高雇员记住,他左边没有比他高的雇员。我基本上讲了解释中写的相同内容。这个阵列基本上是:从最短的员工(身高1)到最高的员工(这里的身高4),告诉你他们记得的左边更高的员工人数。
Q.2)为什么
4 3 1 2
序列不可能?
输出应该是员工原始订单的高度。在4 3 1 2
输出中,您将高度为2的员工放在最右侧的位置。从他的位置,他会看到两个人(身高4的员工和身高3的员工)比他左边高。但根据他记得的输入,他左边有一名比他高的雇员。因此,4 3 1 2
不能是原始订单。
答案 1 :(得分:0)
我理解这个问题
The Employee3 has height 3 and there was 1 people left to him [1] but how? height 3 is taller than 1
您的上述陈述不正确。问题说How many people in your left side is taller than i number
Why 4 3 1 2 sequence is not possible?
这不是有效序列,因为 1 左侧有两个大于 1 的数字
希望它可以帮到你
答案 2 :(得分:0)
最后我可以编写相同的代码
# input1 n = no. of employees 4
# input2 array of n elements {2, 1, 1, 0}
# Output [4, 2, 1, 3]
# 0 [2, 1, 1, 0] [4, 2, 1, 3]
# 1 [2, 1, 1, 0] [4, 2, 1, 3]
# 2 [2, 1, 1, 0] [4, 2, 1, 3]
# 3 [2, 1, 1, 0] [4, 2, 1, 3]
import copy
def findLogic(temp, input2):
for i, (k, v) in enumerate(zip(temp, input2)):
count = 0
if i == 0:
countList = []
j = i + 1
#print(i, temp, input2)
if j in input2:
#print(i, temp, input2, input2.index(j))
for l in range(input2.index(j), -1, -1):
if l >= 0 and j <= (len(input2)-1):
if j < input2[l]:
count += 1
countList.append(count)
if countList == temp:
#print(input2)
print("{", end="")
for i in input2:
print(i, end="")
if input2.index(i) != len(input2) - 1:
print(",", end="")
print("}", end="")
return 1
def PlaceNumbs(input2, l, r, temp):
if l == r:
if findLogic(temp, input2):
return
for i in range(l, r):
input2[l], input2[i] = input2[i], input2[l]
PlaceNumbs(input2, l + 1, r, temp)
input2[l], input2[i] = input2[i], input2[l]
#def FinalOrder(n, input2):
def uniqueValue(input1,input2):
n = input1
temp = copy.deepcopy(input2)
input3 = []
for i in range(1, n+1):
input3.append(int(i))
PlaceNumbs(input3, 0, n, temp)
uniqueValue(4, [2, 1, 1, 0])
print("\n")
uniqueValue(8, [1, 2, 1, 1, 1, 1, 0, 0])
答案 3 :(得分:0)
我们需要按顺序返回员工的高度,使其满足数组中给出的条件。
例如:
数组中的第一个元素表示更多员工的数量 在员工左侧,身高等于1.同样 第二个元素表示左侧更多员工的数量 高度等于2的员工,依此类推。
这是我在c ++中的代码
#include<stdio.h>
#include<string.h>
int* uniqueValue(int input1,int input2[])
{
int *out = new int[input1];
int i,j,k, count;
for(i=0; i<input1; i++)
out[i] = input1;
for(i=0; i< (input1-1); i++)
{
count = 0;
for(j=0; j< (input1-1); j++)
{
if(out[j] > (i+1))
count++;
if(count == input2[i])
{
for(k = j+1; k<input1; k++)
if(out[k] == input1)
{
out[k] = i+1;
break;
}
break;
}
else if(count > input2[i])
{
for(k = j; k<input1; k++)
if(out[k] == input1)
{
out[k] = i+1;
break;
}
break;
}
}
}
return out;
}