📌  相关文章
📜  数组连续元素之间的最小差异之和

📅  最后修改于: 2021-04-29 16:17:25             🧑  作者: Mango

给定一个成对的数组,其中每个对代表一个范围,任务是找到数组的连续元素之间的最小差之和,该数组按以下方式填充:

  • 数组的每个元素都位于范围数组中其对应索引处给定的范围内。
  • 形成的阵列中连续元素的差的最终总和最小。

例子:

方法:采用贪婪的方法来解决上述问题。最初,我们必须以这样一种方式填充数组,使得所获得的差异之和最小。贪婪的方法如下:

  • 如果先前索引的范围与当前索引的范围相交,则在这种情况下,最小差异将为0,并存储相交范围的最大值和最小值。
  • 如果先前索引范围的最小值大于当前索引的最大值,则在这种情况下,最小可能的和是先前范围的最小值与当前范围存储的最大值之差。
  • 如果先前索引的最高范围低于当前范围的最小值,则最小总和为当前索引存储的最小值与先前索引的最高范围之差。

下面是上述方法的实现:

C++
// C++ program for finding the minimum sum of
// difference between consecutive elements
#include 
using namespace std;
  
// function to find minimum sum of
// difference of consecutive element
int solve(pair v[], int n)
{
    // ul to store upper limit
    // ll to store lower limit
    int ans, ul, ll;
  
    // storethe lower range in ll
    // and upper range in ul
    ll = v[0].first;
    ul = v[0].second;
  
    // inititalize the answer with 0
    ans = 0;
  
    // iterate for all ranges
    for (int i = 1; i < n; i++) {
  
        // case 1, in this case the difference will be 0
        if ((v[i].first <= ul && v[i].first >= ll) ||
           (v[i].second >= ll && v[i].second <= ul)) {
  
            // change upper limit and lower limit
            if (v[i].first > ll) {
                ll = v[i].first;
            }
            if (v[i].second < ul) {
                ul = v[i].second;
            }
        }
  
        // case 2
        else if (v[i].first > ul) {
  
            // store the difference
            ans += abs(ul - v[i].first);
            ul = v[i].first;
            ll = v[i].first;
        }
  
        // case 3
        else if (v[i].second < ll) {
  
            // store the difference
            ans += abs(ll - v[i].second);
            ul = v[i].second;
            ll = v[i].second;
        }
    }
    return ans;
}
// Driver code
int main()
{
    // array of range
  
    pair v[] = { { 1, 3 }, { 2, 5 }, 
                { 6, 8 }, { 1, 2 }, { 2, 3 } };
    int n = sizeof(v) / sizeof(v[0]);
    cout << solve(v, n) << endl;
  
    return 0;
}


Java
// Java program for finding the 
// minimum sum of difference
// between consecutive elements
import java.io.*;
  
class GFG
{
      
// function to find minimum 
// sum of difference of
// consecutive element
static int solve(int[][] v, int n)
{
    // ul to store upper limit
    // ll to store lower limit
    int ans, ul, ll;
    int first = 0;
    int second = 1;
  
    // storethe lower range 
    // in ll and upper range 
    // in ul
    ll = v[0][first];
    ul = v[0][second];
  
    // inititalize the
    // answer with 0
    ans = 0;
  
    // iterate for all ranges
    for (int i = 1; i < n; i++) 
    {
  
        // case 1, in this case 
        // the difference will be 0
        if ((v[i][first] <= ul && 
              v[i][first] >= ll) ||
            (v[i][second] >= ll &&
             v[i][second] <= ul)) 
        {
  
            // change upper limit 
            // and lower limit
            if (v[i][first] > ll)
            {
                ll = v[i][first];
            }
            if (v[i][second] < ul) 
            {
                ul = v[i][second];
            }
        }
  
        // case 2
        else if (v[i][first] > ul) 
        {
  
            // store the difference
            ans += Math.abs(ul - v[i][first]);
            ul = v[i][first];
            ll = v[i][first];
        }
  
        // case 3
        else if (v[i][second] < ll)
        {
  
            // store the difference
            ans += Math.abs(ll - v[i][second]);
            ul = v[i][second];
            ll = v[i][second];
        }
    }
    return ans;
}
  
// Driver code
public static void main(String []args)
{
    // array of range
  
    int[][] v = {{ 1, 3 }, { 2, 5 }, 
                 { 6, 8 }, { 1, 2 }, 
                 { 2, 3 }};
    int n = 5;
    System.out.println(solve(v, n));
}
}
  
// This code is contributed
// by chandan_jnu


Python
# Python program for finding 
# the minimum sum of difference
# between consecutive elements
  
class pair:
    first = 0
    second = 0
      
    def __init__(self, a, b):
        self.first = a
        self.second = b
  
# function to find minimum 
# sum of difference of 
# consecutive element
def solve(v, n):
      
    # ul to store upper limit
    # ll to store lower limit
    ans = 0; ul = 0; ll = 0;
  
    # storethe lower range 
    # in ll and upper range
    # in ul
    ll = v[0].first
    ul = v[0].second
  
    # inititalize the 
    # answer with 0
    ans = 0
  
    # iterate for all ranges
    for i in range(1, n):
          
        # case 1, in this case
        # the difference will be 0
        if (v[i].first <= ul and 
            v[i].first >= ll) or \
           (v[i].second >= ll and
            v[i].second <= ul):
              
            # change upper limit 
            # and lower limit
            if v[i].first > ll:
                ll = v[i].first
              
            if v[i].second < ul:
                ul = v[i].second;
  
        # case 2
        elif v[i].first > ul:
              
            # store the difference
            ans += abs(ul - v[i].first)
            ul = v[i].first
            ll = v[i].first
  
        # case 3
        elif v[i].second < ll:
              
            # store the difference
            ans += abs(ll - v[i].second);
            ul = v[i].second;
            ll = v[i].second;
    return ans
  
# Driver code
  
# array of range
v = [pair(1, 3), pair(2, 5), 
     pair(6, 8), pair(1, 2), 
                 pair(2, 3) ]
n = len(v)
print(solve(v, n))
  
# This code is contributed
# by Harshit Saini


C#
// C# program for finding the 
// minimum sum of difference
// between consecutive elements
using System;
  
class GFG
{
// function to find minimum 
// sum of difference of
// consecutive element
static int solve(int[,] v, int n)
{
    // ul to store upper limit
    // ll to store lower limit
    int ans, ul, ll;
    int first = 0;
    int second = 1;
  
    // storethe lower range 
    // in ll and upper range 
    // in ul
    ll = v[0, first];
    ul = v[0, second];
  
    // inititalize the
    // answer with 0
    ans = 0;
  
    // iterate for all ranges
    for (int i = 1; i < n; i++) 
    {
  
        // case 1, in this case 
        // the difference will be 0
        if ((v[i, first] <= ul && 
             v[i, first] >= ll) ||
            (v[i, second] >= ll &&
             v[i, second] <= ul)) 
        {
  
            // change upper limit 
            // and lower limit
            if (v[i, first] > ll)
            {
                ll = v[i, first];
            }
            if (v[i, second] < ul) 
            {
                ul = v[i, second];
            }
        }
  
        // case 2
        else if (v[i, first] > ul) 
        {
  
            // store the difference
            ans += Math.Abs(ul - v[i, first]);
            ul = v[i, first];
            ll = v[i, first];
        }
  
        // case 3
        else if (v[i, second] < ll)
        {
  
            // store the difference
            ans += Math.Abs(ll - v[i, second]);
            ul = v[i, second];
            ll = v[i, second];
        }
    }
    return ans;
}
  
// Driver code
static void Main()
{
    // array of range
  
    int[,] v = new int[5,2]{ { 1, 3 }, { 2, 5 }, 
                             { 6, 8 }, { 1, 2 }, 
                             { 2, 3 } };
    int n = 5;
    Console.WriteLine(solve(v, n));
}
}
  
// This code is contributed
// by chandan_jnu


PHP
= $ll) or
        ($v[$i][$second] >= $ll and
         $v[$i][$second] <= $ul)) 
    {
  
        // change upper limit 
        // and lower limit
        if ($v[$i][$first] > $ll)
        {
            $ll = $v[$i][$first];
        }
        if ($v[$i][$second] < $ul) 
        {
            $ul = $v[$i][$second];
        }
    }
  
    // case 2
    else if ($v[$i][$first] > $ul) 
    {
  
        // store the difference
        $ans += abs($ul - $v[$i][$first]);
        $ul = $v[$i][$first];
        $ll = $v[$i][$first];
    }
  
    // case 3
    else if ($v[$i][$second] < $ll)
    {
  
        // store the difference
        $ans += abs($ll - $v[$i][$second]);
        $ul = $v[$i][$second];
        $ll = $v[$i][$second];
    }
}
return $ans;
}
  
// Driver code
  
// array of range
$v = array(array( 1, 3 ), 
           array( 2, 5 ), 
           array( 6, 8 ), 
           array( 1, 2 ), 
           array( 2, 3 ));
$n = 5;
echo(solve($v, $n));
  
// This code is contributed
// by chandan_jnu
?>


输出:
7

时间复杂度: O(N)
辅助空间: O(1)