简体   繁体   English

找出数组中两个数字之间的最大落差。 这段代码的时间复杂度是多少?

[英]Find the largest drop between two numbers in an array. What is the time complexity of this code?

I have an array full of numbers.我有一个充满数字的数组。 I need to find the maximum difference between 2 numbers but the biggest number is before the smallest number in the array.我需要找到 2 个数字之间的最大差异,但最大的数字在数组中的最小数字之前。

public static int maximalDrop(int[] a) For example: public static int maximalDrop(int[] a)例如:

for the array 5, 21, 3, 27, 12, 24, 7, 6, 4 the result will be 23 (27 - 4)对于数组5, 21, 3, 27, 12, 24, 7, 6, 4结果将为23 (27 - 4)

for the array 5, 21, 3, 22, 12, 7, 26, 14 the result will be 18 (21 - 3)对于数组5, 21, 3, 22, 12, 7, 26, 14结果将为18 (21 - 3)

I think I write it in time complexity O(N) is my code ok?我想我是用时间复杂度O(N)写的,我的代码可以吗? What is that time complexity of my code?我的代码的时间复杂度是多少? Here is the method that I write:这是我写的方法:

public static int maximalDrop(int[] a) {
    int i = 0;
    int temp = 0;
    int result = -1;
    int k = a.length - 1;
    boolean finishCheckLoop = false;
    while (k > i) {
        if (a[i] < a[i + 1] || finishCheckLoop == true) {
            i++;
            finishCheckLoop = false;
        } else if (a[i] >= a[k] || a[i] <= a[k]) {
            if (a[k] < 0)
                temp = Math.abs(a[k]) + a[i];
            else if (a[k] > 0)
                temp = a[i] - a[k];
            result = Math.max(temp, result);
            k--;
        }
        if (i == k) {
            k = a.length - 1;
            finishCheckLoop = true;
        }
    }
    return result;
}

Your code seems to be O(N) , but it is quite complex for the task.您的代码似乎是O(N) ,但对于该任务来说非常复杂。

A simpler (and faster) version:一个更简单(更快)的版本:

public static int maximalDrop(int[] a) {
  int maxDrop = 0;
  int drop = 0;
  int max = Integer.MIN_VALUE;
  int cur;
  for (int i = 0; i < a.length; i++) {
    cur = a[i];
    if (cur > max) {
      max = cur;
    }
    drop = max - cur;
    if (drop > maxDrop) {
      maxDrop = drop;
    }
  }
  return maxDrop;
}

Disclaimer, I don't really write java, so I'm not sure if the Integer.MIN_VALUE is correct.免责声明,我不是真的写 java,所以我不确定Integer.MIN_VALUE是否正确。

Yes, your code has a time complexity of O(N) , but it's not readable and was probably hard to write.是的,您的代码的时间复杂度为O(N) ,但它不可读并且可能难以编写。

Almost all of us have this bad tendency to start coding before we know how to solve the problem.几乎我们所有人都有这种不好的倾向,在我们知道如何解决问题之前就开始编码。 The code presented here seems like a product of this technique.这里提供的代码似乎是这种技术的产物。

To improve the code, we should pick up a pen and paper and think simple.要改进代码,我们应该拿起笔和纸,简单地思考。 Note that we need to return the maximal drop from left to right so there are quite a few cases请注意,我们需要返回从左到右的最大下降,所以有很多情况

Let's keep high , low and maximalDrop variables:让我们保留highlowmaximalDrop变量:

  • The current number is a new low:目前的数字是新低:

    1. low = current number. low = 当前数字。
    2. Update the maximalDrop if necessary.如有必要,更新maximalDrop
  • The current number is a new high - the previous low is irrelevant.当前数字是新高 - 之前的低点无关紧要。

  • Else - do nothing.否则 - 什么都不做。

Now it's easy to code:现在很容易编码:

public static int maximalDrop(int[] a) {
    if (a.length == 0) {
        return -1;
    }

    int h = a[0], l = a[0];
    int maxDrop = -1;

    for (int curr : a) {
        if (curr < l) {
            l = curr;
            maxDrop = Math.max(h - l, maxDrop);
        } else if (curr > h) {
            h = curr;
            l = Integer.MAX_VALUE;
        }
    }

    return maxDrop;
}

Output examples: Output 示例:

System.out.println(maximalDrop(new int[]{5, 21, 3, 27, 12, 24, 7, 6, 4})); // 23
System.out.println(maximalDrop(new int[]{5, 21, 3, 22, 12, 7, 26, 14}));  // 18 
System.out.println(maximalDrop(new int[]{5, 6}));  // -1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于查找数组中最大数的递归算法的运行时间复杂度是多少。 比较它与迭代循环代码 - What will be the run time complexity for recursive algorithm for finding the largest number in an array. Compare it vs iterative loop code 最大的未排序数字数组的以下时间复杂度 - Time complexity for the following for Largest in array of unsorted numbers 以最佳时间复杂度 [Java] 在未排序的整数数组中找到两个相同数字之间的最大差异 - Find the Maximum difference between two Same Numbers in an unsorted array of integers in best Time Complexity [Java] 在不使用Array的情况下查找列表中的两个最大数字 - Find Two largest numbers in a list without using Array 这段代码的时间复杂度是多少? 二维数组 - What is the time complexity of this code? 2d Array 查找数组中所有数字的O时间复杂度大 - Big O Time complexity for Find All Numbers Disappeared in an Array 编写一个程序来查找随机数组中的最小和最大数。 程序逻辑正确,但执行不正确。 解决方法是什么? - Writing a program to find smallest and largest number in a random array. Logic of the program is correct, but not executing properly. What's the fix? 第 k 个最大的时间复杂度 - Time Complexity of kth largest 减少代码的时间复杂度,以从N * N查找数组中的重复项 - Reduce the time complexity of the code to find duplicates in an Array from N*N 在给定的数字数组中找到最大的序列 - Find the largest sequence in a given array of numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM