简体   繁体   中英

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.

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

for the array 5, 21, 3, 27, 12, 24, 7, 6, 4 the result will be 23 (27 - 4)

for the array 5, 21, 3, 22, 12, 7, 26, 14 the result will be 18 (21 - 3)

I think I write it in time complexity O(N) is my code ok? 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.

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.

Yes, your code has a time complexity of O(N) , but it's not readable and was probably hard to write.

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:

  • The current number is a new low:

    1. low = current number.
    2. Update the maximalDrop if necessary.
  • 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM