找到数组中的最小'对'

时间:2014-12-13 03:24:17

标签: ruby-on-rails arrays sorting

我有一系列数组':

[[time, distance], [time, distance], [time, distance], [time, distance], ...]

我想找到'对的索引。在最短的时间内,如果有多对'在相同的最小时间内,以最小距离对。

e.g。如果我有:

[[5, 5], [1,7], [2,6], [1,6]]

我想返回3.(即第4个元素的索引)。

我该怎么做?

3 个答案:

答案 0 :(得分:0)

Array#sort完全符合您的要求。

myarray = [[5, 5], [1,7], [2,6], [1,6]]
myarray.sort
#=> [[1, 6], [1, 7], [2, 6], [5, 5]]

因此,我们只需要找到原始数组中与排序数组中第一个元素匹配的元素。

ndx = myarray.index myarray.sort[0]
#=> 3
编辑:我最初使用太空飞船运营商,但意识到这是不必要的。

答案 1 :(得分:0)

我不熟悉Ruby on Rails,但我写了一些PHP代码来实现你的目标。我希望你能从中提取这个想法。

<?php

$timesAndDistances = [['t' => t1, 'd' => d1], ['t' => t2, 'd' => d2], ..., ['t' => tn, 'd' => dn]];

$lowestTime_withDistance = [['t' => null, 'd' => null, 'i' => null]];

foreach($timesAndDistances as $index => $timeAndDistance)
{
    $time = $timeAndDistance['t'];
    $distance = $timeAndDistance['d'];

    if($lowestTime_withDistance[0]['t'] == null)
    {
        $lowestTime_withDistance[0] = ['t' => $time, 'd' => $distance, 'i' => $index];
    }
    else 
    {
        if($lowestTime_withDistance[0]['t'] > $time){
            $lowestTime_withDistance[0] = ['t' => $time, 'd' => $distance, 'i' => $index];
        }
        elseif ($lowestTime_withDistance[0]['t'] == $time)
        {
            $lowestTime_withDistance[] = ['t' => $time, 'd' => $distance, 'i' => $index];
        }
    }
}

if(count($lowestTime_withDistance) == 1)
{
    echo 'Index of element with minimum time is ' . $lowestTime_withDistance[0]['i'];
}
elseif(count($lowestTime_withDistance) > 1){
    $lowestDistance = [['d' => null, 'i' =>null]];

    foreach($lowestTime_withDistance as $timeWithDistance)
    {
        if($lowestDistance[0]['d'] == null)
        {
            $lowestDistance[0] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
        }
        else
        {
            if($lowestDistance[0]['d'] > $timeWithDistance['d'])
            {
                $lowestDistance[0] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
            }
            elseif ($lowestDistance[0]['d'] == $timeWithDistance['d'])
            {
                $lowestDistance[] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
            }
        }
    }

    if(count($lowestDistance) == 1)
    {
        echo 'Index of element with minimum time and distance is ' . $lowestDistance[0]['i'];
    }
    elseif (count($lowestDistance) > 1)
    {
        foreach($lowestDistance as $aLowestDistance)
        {
            echo 'Index of an element with minimum time and distance is ' . $aLowestDistance['i'];
        }
    }
}

答案 2 :(得分:0)

编写快速代码来实现这一目标:

  1. Say Array a包含以下元素

      

    [[5,5],[1,7],[2,6],[1,6]]

  2. 在b:

    中创建数组副本
      

    b = a

         

    b = [[5,5],[1,7],[2,6],[1,6]]

  3. 排序b

      

    b = b.sort

         

    b = [[1,6],[1,7],[2,6],[5,5]]

  4. 迭代嵌套循环

      

    最小= b [0]

         

    index = 0

         

    a.each do | iter_orig |除非a.nil?

         

    如果iter_orig.eql?最小

         

    中断;

         

         

    指数++;

         

         

         

    put&#34;指数最小的时间/距离 - &#34; + index