Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 两个元素不相邻的最大和_Arrays_Algorithm_Data Structures - Fatal编程技术网

Arrays 两个元素不相邻的最大和

Arrays 两个元素不相邻的最大和,arrays,algorithm,data-structures,Arrays,Algorithm,Data Structures,现在每个地方可用的解决方案都是包含和排除和。最后,max这两个函数将给出输出 起初我很难理解这个算法,我想为什么不用一种简单的方法 算法: 通过一次增加两个数组指针来循环数组 计算数组中奇数位置的元素sum 计算偶数定位元素sum 最后,取这两个sum中的max 这样,我认为复杂性将减少一半O(n/2) 这个算法正确吗?这是动态规划的一个例子。算法是: 不要(汇总)任何非正面项目 对于正数,将问题分成两部分:尝试获取和跳过项目,并返回这些选项的最大值: 让我们展示第二步,想象我们得到: [1,

现在每个地方可用的解决方案都是包含和排除和。最后,
max
这两个函数将给出输出

起初我很难理解这个算法,我想为什么不用一种简单的方法

算法: 通过一次增加两个数组指针来循环数组

  • 计算数组中奇数位置的元素
    sum
  • 计算偶数定位元素
    sum
  • 最后,取这两个
    sum
    中的
    max

    这样,我认为复杂性将减少一半
    O(n/2)


    这个算法正确吗?

    这是动态规划的一个例子。算法是:

  • 不要(汇总)任何非正面项目
  • 对于正数,将问题分成两部分:尝试获取和跳过项目,并返回这些选项的最大值:
  • 让我们展示第二步,想象我们得到:

    [1, 2, 3, 4, 5, 6, 10, 125, -8, 9]
    
    1
    为正,这就是原因

    take_sum = max(1 + max_sum([3, 4, 5, 6, 10, 125, -8, 9])) // we take "1"
    skip_sum = max_sum([2, 3, 4, 5, 6, 10, 125, -8, 9])       // we skip "1" 
    max_sum =  max(take_sum, skip_sum)
    
    C#实现(为了展示赤裸裸的想法,最简单的代码,无需进一步优化):

    结果:

    Console.WriteLine(BestSum(new int[] { 1, -2, -3, 100 }));
    Console.WriteLine(BestSum(new int[] { 100, 8, 10, 20, 7 }))
    
    101        
    120
    
      using System.Linq;
    
      ...
    
      Random gen = new Random(0); // 0 - random, by repeatable (to reproduce the same result)
    
      int[] test = Enumerable
        .Range(1, 10000)
        .Select(i => gen.Next(100))
        .ToArray();
    
      int evenSum = test.Where((v, i) => i % 2 == 0).Sum();
      int oddSum = test.Where((v, i) => i % 2 != 0).Sum();
      int suboptimalSum = Math.Max(evenSum, oddSum); // <- Your initial algorithm
      int result = BestSum(test);
    
      Console.WriteLine(
        $"odd: {oddSum} even: {evenSum} suboptimal: {suboptimalSum} actual: {result}");
    
      odd: 246117 even: 247137 suboptimal: 247137 actual: 290856
    
    请检查初始算法是否返回次优和
    98
    117

    编辑:在现实生活中,您可能需要添加一些优化,例如记忆和特殊情况测试:

    private static Dictionary<int, int> s_Memo = new Dictionary<int, int>();
    
    private static int BestSum(int[] array, int index) {
      if (index >= array.Length)
        return 0;
    
      int result;
    
      if (s_Memo.TryGetValue(index, out result)) // <- Memoization
        return result;
    
      if (array[index] <= 0) 
        return BestSum(array, index + 1);
    
      // Always take, when the last item to choose or when followed by non-positive item
      if (index >= array.Length - 1 || array[index + 1] <= 0) {
        result = array[index] + BestSum(array, index + 2);
      }
      else {
        int take = array[index] + BestSum(array, index + 2);
        int skip = BestSum(array, index + 1);
    
        result = Math.Max(take, skip);
      }
    
      s_Memo.Add(index, result); // <- Memoization
    
      return result;
    }
    
    private static int BestSum(int[] array) {
      s_Memo.Clear();
    
      return BestSum(array, 0);
    }
    

    动态规划包含排除法是正确的,您的算法不适用于像3 2 7 10这样的测试用例。在这个测试用例中,我们采用的两个元素是3 10,和是13,而不是3,7或2,10。请您理解我的意思,为进一步澄清,代码如下

    Java实现

    public int maxSum(int arr[]) {   // array must contain +ve elements only
     int excl = 0;
     int incl = arr[0];
     for (int i = 1; i < arr.length; i++) {
       int temp = incl;
       incl = Math.max(excl + arr[i], incl);
       excl = temp;
     }
     return incl;
    }
    
    public int-maxSum(int-arr[]){//数组只能包含+ve元素
    int excl=0;
    int incl=arr[0];
    对于(int i=1;i
    不,这行不通。为什么?因为不相邻并不意味着所有的数字都必须只在奇数或偶数索引中。考虑<代码> [1,-2–3, 100 ] < /代码>。解决方案是
    1+100
    ,它是奇数和偶数index@LalitVerma我实现了,我来到这里,因为我没有找到解决方案,因为Subhadepi非常喜欢这个答案!你会推荐更多关于动态编程示例的资源吗?@simo:动态编程(有时称为智能递归)是众所周知的编程方法;有很多关于它的书,比如说,谢谢德米特里,我会检查那本书
    public int maxSum(int arr[]) {   // array must contain +ve elements only
     int excl = 0;
     int incl = arr[0];
     for (int i = 1; i < arr.length; i++) {
       int temp = incl;
       incl = Math.max(excl + arr[i], incl);
       excl = temp;
     }
     return incl;
    }