识别一组数组中最小数据对应的索引

发布于 2024-10-21 20:42:40 字数 329 浏览 1 评论 0原文

我相信这是一个微不足道的算法问题,但我似乎无法找到有效而优雅的解决方案。

我们有 3 个 int 数组(Aa、Ab、Ac)和 3 个游标(Ca、Cb、Cc),它们指示相应数组中的索引。我想识别并增加指向最小值的光标。如果该游标已经位于数组的末尾,我将排除它并增加指向第二个最小值的游标。如果只有 1 个游标不在数组末尾,我们将增加该游标。

我能想到的唯一解决方案很复杂和/或不是最佳的。例如,我总是以一个巨大的 if...else... 结束,

有人看到这个问题的巧妙解决方案吗?

我正在使用 C++ 进行编程,但请随意使用伪代码或您喜欢的任何语言进行讨论。

谢谢

This is a trivial algorithmic question, I believe, but I don't seem to be able to find an efficient and elegant solution.

We have 3 arrays of int (Aa, Ab, Ac) and 3 cursors (Ca, Cb, Cc) that indicate an index in the corresponding array. I want to identify and increment the cursor pointing to the smallest value. If this cursor is already at the end of the array, I will exclude it and increment the cursor pointing to the second smallest value. If there is only 1 cursor that is not at the end of the array, we increment this one.

The only solutions I can come up are complicated and/or not optimal. For example, I always end up with a huge if...else...

Does anyone see a neat solution to this problem ?

I am programming in C++ but feel free to discuss it in pseudo-code or any language you like.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-10-28 20:42:40

伪java代码:

int[] values = new int[3];
values[0] = aa[ca];
values[1] = ab[cb];
values[2] = ac[cc];
Arrays.sort(values);

boolean done = false;
for (int i = 0; i < 3 && !done; i++) {
    if (values[i] == aa[ca] && ca + 1 < aa.length) {
        ca++;
        done = true;
    }
    else if (values[i] == ab[cb] && cb + 1 < ab.length) {
        cb++;
        done = true;
    }
    else if (cc + 1 < ac.length) {
        cc++;
        done = true;
    }
}
if (!done) {
    System.out.println("cannot increment any index");
    stop = true;
}

本质上,它执行以下操作:

  1. aa[ca]ab[cb]初始化数组valuesac[cc]

  2. 值进行排序

  3. 扫描并在可能的情况下递增(即尚未位于数组的末尾)相应值的索引

,排序最多是 O(n lg n),但我只对 3 个元素的数组进行排序。

Pseudo-java code:

int[] values = new int[3];
values[0] = aa[ca];
values[1] = ab[cb];
values[2] = ac[cc];
Arrays.sort(values);

boolean done = false;
for (int i = 0; i < 3 && !done; i++) {
    if (values[i] == aa[ca] && ca + 1 < aa.length) {
        ca++;
        done = true;
    }
    else if (values[i] == ab[cb] && cb + 1 < ab.length) {
        cb++;
        done = true;
    }
    else if (cc + 1 < ac.length) {
        cc++;
        done = true;
    }
}
if (!done) {
    System.out.println("cannot increment any index");
    stop = true;
}

Essentially, it does the following:

  1. initialize an array values with aa[ca], ab[cb] and ac[cc]

  2. sort values

  3. scan values and increment if possible (i.e. not already at the end of the array) the index of the corresponding value

I know, sorting is at best O(n lg n), but I'm only sorting an array of 3 elements.

趁年轻赶紧闹 2024-10-28 20:42:40

这个解决方案怎么样:

if (Ca != arraySize - 1) AND 
   ((Aa[Ca] == min(Aa[Ca], Ab[Cb], Ac[Cc]) OR 
    (Aa[Ca] == min(Aa[Ca], Ab[Cb]) And Cc == arraySize - 1) OR
    (Aa[Ca] == min(Aa[Ca], Ac[Cc]) And Cb == arraySize - 1) OR
    (Cc == arraySize - 1 And Cb == arraySize - 1))
{
    Ca++;
}
else if (Cb != arraySize - 1) AND 
        ((Ab[Cb] == min(Ab[Cb], Ac[Cc]) OR (Cc == arraySize - 1))
{
    Cb++;
}
else if (Cc != arraySize - 1) 
{
    Cc++;
}

what about this solution:

if (Ca != arraySize - 1) AND 
   ((Aa[Ca] == min(Aa[Ca], Ab[Cb], Ac[Cc]) OR 
    (Aa[Ca] == min(Aa[Ca], Ab[Cb]) And Cc == arraySize - 1) OR
    (Aa[Ca] == min(Aa[Ca], Ac[Cc]) And Cb == arraySize - 1) OR
    (Cc == arraySize - 1 And Cb == arraySize - 1))
{
    Ca++;
}
else if (Cb != arraySize - 1) AND 
        ((Ab[Cb] == min(Ab[Cb], Ac[Cc]) OR (Cc == arraySize - 1))
{
    Cb++;
}
else if (Cc != arraySize - 1) 
{
    Cc++;
}
堇色安年 2024-10-28 20:42:40

伪代码:编辑:整理了一下

class CursoredArray
{
    int index;
    std::vector<int> array;

    int val()
    {
        return array[index];
    }

    bool moveNext()
    {
        bool ret = true;
        if( array.size() > index )
            ++index;
        else
            ret = false;
        return ret;
    }   
}    

std::vector<CursoredArray> arrays;
std::vector<int> order = { 0, 1, 2 };//have a default order to start with

if( arrays[0].val() > arrays[1].val() )
    std::swap( order[0], order [1] );

if( arrays[2].val() < arrays[order[1]].val() )//if the third is less than the largest of the others
{
    std::swap( order[1], order [2] );
    if( arrays[2].val() < arrays[order[0]].val() )//if the third is less than the smallest of the others
        std::swap( order[0], order [1] );
}  
//else third pos of order is already correct

bool end = true;

for( i = 0; i < 3; ++i )
{   
    if( arrays[order[i]].MoveNext() )
    {
        end = false;
        break;
    }
}

if( end )//have gone through all the arrays

Pseudo code: EDIT : tidied it up a bit

class CursoredArray
{
    int index;
    std::vector<int> array;

    int val()
    {
        return array[index];
    }

    bool moveNext()
    {
        bool ret = true;
        if( array.size() > index )
            ++index;
        else
            ret = false;
        return ret;
    }   
}    

std::vector<CursoredArray> arrays;
std::vector<int> order = { 0, 1, 2 };//have a default order to start with

if( arrays[0].val() > arrays[1].val() )
    std::swap( order[0], order [1] );

if( arrays[2].val() < arrays[order[1]].val() )//if the third is less than the largest of the others
{
    std::swap( order[1], order [2] );
    if( arrays[2].val() < arrays[order[0]].val() )//if the third is less than the smallest of the others
        std::swap( order[0], order [1] );
}  
//else third pos of order is already correct

bool end = true;

for( i = 0; i < 3; ++i )
{   
    if( arrays[order[i]].MoveNext() )
    {
        end = false;
        break;
    }
}

if( end )//have gone through all the arrays
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文