Complexity of the challenge

What is the complexity of the runtime / memory Problem with maximum subarrah using brute force?

Can they be optimized? Especially memory complexity?

Thank,

0
source share
3 answers

Brute force - Omega (n ^ 2). Using Divide and Conquer, you can do it with Theta (n lg n) complexity. Further details are available in many books, such as Introduction to Algorithms , or in various resources on the Internet, such as this lecture .

+1
source

, , , , i, . O (n) .

, , , .

Java, :

public int[] kadanesAlgorithm (int[] array) {
        int start_old = 0;
        int start = 0;
        int end = 0;
        int found_max = 0;

        int max = array[0];

        for(int i = 0; i<array.length; i++) {
            max = Math.max(array[i], max + array[i]);
            found_max = Math.max(found_max, max);
            if(max < 0)
                start = i+1;
            else if(max == found_max) {
                start_old=start;
                end = i;
                }
        }

        return Arrays.copyOfRange(array, start_old, end+1);
    }
0

, , O (n). Java:

public int[] kadanesAlgorithm (int[] array) {
        int start_old = 0;
        int start = 0;
        int end = 0;
        int found_max = 0;

        int max = array[0];

        for(int i = 0; i<array.length; i++) {
            max = Math.max(array[i], max + array[i]);
            found_max = Math.max(found_max, max);
            if(max < 0)
                start = i+1;
            else if(max == found_max) {
                start_old=start;
                end = i;
                }
        }

        return Arrays.copyOfRange(array, start_old, end+1);
    }
0

Source: https://habr.com/ru/post/1668067/


All Articles