gpt4 book ai didi

c - 面试问题...试图解决,但无法得到有效的解决方案

转载 作者:太空狗 更新时间:2023-10-29 16:32:04 25 4
gpt4 key购买 nike

我被困在一个面试问题中。问题是,

*given two arrays A and B. A has integers unsorted. B has the same length as A and its values are in the set {-1,0,1}

you have to return an array C with the following processing on A.

if B[i] has 0 then C[i] must have A[i]
if B[i] has -1 then A[i] must be in C within the sub array C[0] - C[i-1] ie. left subarray
if B[i] has 1 then A[i] must be in C within the sub array C[i+1] - C[length(A)] ie right subarray.

if no such solution exists then printf("no solution");*

我应用了以下逻辑:-

int indMinus1 = n-1;
int indPlus1 = 0;

//while(indPlus1 < n && indMinus1 > 0)
while(indPlus1 < indMinus1)
{
while(b[indMinus1] != -1) {
if(b[indMinus1] == 0)
c[indMinus1] = a[indMinus1];
indMinus1--;
}
while(b[indPlus1] != +1) {
if(b[indPlus1] == 0)
c[indPlus1] = a[indPlus1];
indPlus1++;
}

c[indMinus1] = a[indPlus1];
c[indPlus1] = a[indMinus1];
b[indMinus1] = 0;
b[indPlus1] = 0;
indMinus1--;
indPlus1++;
}

但是这不会起作用,对于某些情况,例如 {1,2,3} >> {1,-1,-1}... 一个输出是可能的,即 {2,3,1};

请帮忙....他们有任何算法技术可以解决这个问题吗?

正确的解决方案代码

int arrange(int a[], int b[], int c[], int n)
{

for (int i = 0; i < n; ++i) {
if(b[i] == 0)
c[i] = a[i];
}

int ci = 0;
for (int i = 0; i < n; ++i) {
if(b[i] == -1) {
while(c[ci] != 0 && ci < i)
ci ++;
if(c[ci] != 0 || ci >= i)
return -1;
c[ci] = a[i];
ci++;
}
}

for (int i = 0; i < n; ++i) {
if(b[i] == 1) {
while(c[ci] != 0 && ci < n)
ci ++;
if(c[ci] != 0 || ci <= i)
return -1;
c[ci] = a[i];
ci++;
}
}
return 0;
}

最佳答案

我建议使用以下算法:
1. 最初考虑所有C[ i ]作为空巢。
2. 对于每个 i,其中 B[ i ] = 0我们把 C[ i ] = A[ i ]
3. 从左到右遍历数组,对于每个i其中 B[ i ] = -1
C[ j ] = A[ i ] , 其中0 <= j < iC[ j ]最小 索引还是空的。如果不存在这样的索引,则答案是不可能的。
4. 从右到左遍历数组,对于每个i其中 B[ i ] = 1
C[ j ] = A[ i ] , 其中i < j < nC[ j ]最大 索引还是空的。如果不存在这样的索引,则答案是不可能的。

为什么我们在步骤 2 中将 A[ i ] 放在最左边的位置?嗯,我们知道我们必须把它放在某个位置 j < i。另一方面,将它放在最左边会增加我们的更改,以免卡在第 3 步中。请参阅此示例进行说明:

A: [ 1, 2, 3 ]
B: [ 1, 1,-1 ]

最初 C 是空的:C:[ _, _, _ ]
我们没有 0-s,所以让我们转到第 2 步。
我们必须选择是否放置元素 A[ 2 ]C[ 0 ]C[ 1 ] .
如果我们把它放在最左边,我们会得到以下情况:
C: [ _, 3, _ ]
还有...糟糕,我们无法排列元素A[ 0 ]A[ 1 ]由于位置不够:(
但是,如果我们把A[ 2 ]放在最左边,我们会得到
C: [ 3, _, _ ] ,并且很可能用
完成算法 C: [ 3, 1, 2 ] :)

复杂度:
我们所做的是沿数组传递三次,因此复杂度为 O(3n) = O(n) - 线性。

进一步的例子:

A: [ 1, 2, 3 ]
B: [ 1, -1, -1 ]

让我们一步步过一遍这个算法:
1. C: [ _, _, _ ]
2. 为空,因为 B 中没有 0-s
3. 放A[ 1 ]A[ 2 ]到最左边的空位置:

C: [ 2, 3, _ ]

4.放A[ 0 ]到最右边的空闲位置(在这个例子中是唯一一个)空闲位置:

C: [ 2, 3, 1 ]

这就是答案。

源代码:

#include <iostream>
#include <string>
#include <vector>

using namespace std;


vector< int > a;
vector< int > b;
vector< int > c;
int n;

bool solve ()
{
int i;
for( i = 0; i < n; ++i )
c[ i ] = -1;
for( i = 0; i < n; ++i )
if( b[ i ] == 0 )
c[ i ] = a[ i ];
int leftmost = 0;
for( i = 0; i < n; ++i )
if( b[ i ] == -1 )
{
for( ; leftmost < i && c[ leftmost ] != -1; ++leftmost ); // finding the leftmost free cell
if( leftmost >= i )
return false; // not found
c[ leftmost++ ] = a[ i ];
}
int rightmost = n - 1;
for( i = n - 1; i >= 0; --i )
if( b[ i ] == 1 )
{
for( ; rightmost > i && c[ rightmost ] != -1; --rightmost ); // finding the rightmost free cell
if( rightmost <= i )
return false; // not found;
c[ rightmost-- ] = a[ i ];
}
return true;
}


int main ()
{
cin >> n;
a.resize(n);
b.resize(n);
c.resize(n);
int i;
for( i = 0; i < n; ++i )
cin >> a[ i ];
for( i = 0; i < n; ++i )
cin >> b[ i ];
if( !solve() )
cout << "Impossible";
else
for( i = 0; i < n; ++i )
cout << c[ i ] << ' ';
cout << endl;
return 0;
}

关于c - 面试问题...试图解决,但无法得到有效的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com