find largest sub array

There are two ways to solve the problem of largest sub array:

1: divide-and-conquer method

2: linear search method

Now let me introduce them one by one.

1: divide-and-conquer method

The core idea of it is to divide the array to two sub arrays, you need to find out the largest sub array from the above two sub arrays, and the most important thing is you can not ignore the middle sub array cross the middle node. So at each level, you can find out 3 sub largest sub arrays, the left one, the right one and the middle one cross the middle node, and then choose the max one as the largest sub array of the above level. Recursively divide the sub array of each level until each sub array only has one node that is the leaf node.

The detail steps of this algorithm are as follow:

1).find out the middle element's position named m.

2).divide the array to tow sub arrays, the left one is from 0 until m, the right one is from m+1 until the end of the array.

3).make each sub array as the parameter and call the method itself recursively.

4).find out the middle largest sub array crosses the middle node which position is m.

5).compare the three sub array, find out the largest one

The recursion is such that:

    T(n) =  O(1)for n = 1

T(n) = 2(T(n/2))+O(n) for all n > 1

So the complexity of T(n) is O(nlgn).

The following is the implement of divide-and conquer method by java.

private int[] findLargestSubArray1 (int[] array, int l, int r){
    if(l >= r){
        return array;
    }else{
        int m = (l+r)/2;
        int[] l_result =findLargestSubArray1(array,l, m);
        int[] r_result = findLargestSubArray1(array,m+1,r);
        int[] m_result =middleLargetSubArray(array,l,r,m);
        if(l_result[2] > r_result[2]&& l_result[2] > m_result[2]){
            return l_result;
        }else if(r_result[2] >l_result[2] && r_result[2] > m_result[2]){
            return r_result;
        }else{
            return m_result;
        }
    }
}

private int[] middleLargetSubArray(int[] array, int l, int r,int m){
    int l_max = 0;
    int r_max = 0;
    int sum = 0;
    int l_el = 0;
    int r_el = 0;
    for(int i = m;i >= 0;i--){
        sum = sum + array[i];
        if(sum > l_max){
            l_max = sum;
            l_el = i;
        }
    }
    sum = 0;
    for(int i = m+1;i < r;i++){
        sum = sum + array[i];
        if(sum > r_max){
            r_max = sum;
            r_el = i;
        }
    }
    return new int[] {l_el,r_el,l_max+r_max};
}

2: linear search method

The core idea is iterate the array from left to right and sum them, and cache the max sum value and the current subarray after each calculation. If the result is larger than the cached max sum value then replace max sum value with the result. If the result is less than 0,then abandon the cached sub array, make next element as the first element of the new sub array and start the new calculation.

1).iterate the array from left to right.

2).sum the elements that has been iterated.

3).compare the result with the max sum value had been cached, and then to choose the larger one to replace the max sum value.

4).if you find the result has less than 0,you need to abandon the sub array you cache now. from the next element, to findthe new sum array.

The complexity is O(n).

The following is the implement of the linear search by java.

private int[] findLargestSubArray2 (int[] array){
    int new_l_el = 0;
    int l_el = 0;
    int r_el = 0;
    int sum = 0;
    int maxSum = 0;
    for(int i = 0; i < array.length;i++){
        sum = sum + array[i];
        if(sum > maxSum){
            maxSum = sum;
            l_el = new_l_el;
            r_el = i;
        }else if(sum <= 0){
            sum = 0;
            new_l_el = i+1;
        }
    }
    return new int[] {l_el,r_el,maxSum};
}

在JavaScript中,`Array.prototype.find()`是一个数组方法,用于返回数组中第一个满足指定条件的元素。它接受一个回调函数作为参数,该回调函数可以接受三个参数:当前循环的项、循环项的下标和当前循环的数组。在回调函数中,你可以使用条件语句或其他逻辑来判断当前项是否符合你的要求。如果找到了符合条件的元素,`find()`方法将立即返回该元素,并且不会继续循环数组。如果数组中没有符合条件的元素,则返回`undefined`。如果传入了第二个参数`thisValue`,在回调函数中的`this`关键字将指向`thisValue`。下面是几个示例: 示例1: 返回数组中第一个大于6的值 ```javascript var array = [1, 2, 3, 4, 5, 6, 6, 6]; var result = array.find(function(item, index, arr) { return item > 6; }); console.log(result); // 输出: undefined ``` 示例2: 返回数组中第一个大于5的值,并输出数组的变化 ```javascript var array = [1, 2, 3, 4, 5, 6, 6, 6]; var result = array.find(function(item, index, arr) { return item > 5; }); console.log(result); // 输出: 6 console.log(array); // 输出: [1, 2, 3, 4, 5, 6, 6, 6] ``` 示例3: 使用箭头函数简化代码,返回数组中第一个大于1的值 ```javascript let arr1 = [1, 2, 3, 4, 5]; let num = arr1.find(item => item > 1); console.log(num); // 输出: 2 ``` 总结来说,`Array.prototype.find()`方法是用于在数组中查找符合条件的第一个元素的方法,它非常灵活,可以根据不同的需求编写不同的回调函数来实现各种功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [js数组find方法详解](https://blog.csdn.net/qrbd4558/article/details/115656863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [JS中find方法的使用](https://blog.csdn.net/m0_59168984/article/details/121557906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值