leetcode 24th round

public class Solution {
    public int maxSubArray(int[] A) {

       return totalMax(A, 0, A.length-1);

    }

    public int totalMax(int[] A, int low, int high)
    {
        if(low == high)
        {
            return A[low];
        }
        int mid = (low+high)/2;
        int leftMax = totalMax(A, low, mid);
        int rightMax = totalMax(A, mid+1, high);
        int crossMax = MidMax(A, low, mid, high);
        if((leftMax > rightMax) && (leftMax > crossMax))
        {
            return leftMax;
        }
        else if ((rightMax > leftMax) && (rightMax > crossMax))
        {
            return rightMax;
        }
        else
        {
            return crossMax;
        }

    }

    public int MidMax(int[] A, int low, int mid, int high)
    {
        int left_sum = Integer.MIN_VALUE;
        int sum = 0;
        int left_ind = mid;
        for(int i = mid; i >= low; i--)
        {
            sum = sum + A[i];
            if(sum > left_sum)
            {
                left_sum = sum;
                left_ind = i;
            }
        }
        int right_sum = Integer.MIN_VALUE;
        sum = 0;
        int right_ind = mid;
        for (int i = mid+1; i <= high; i++)
        {
            sum = sum + A[i];
            if(sum > right_sum)
            {
                right_sum = sum;
                right_ind = i;
            }
        }
        return (left_sum + right_sum);
    }
}

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

这道题的解我没有想出来,但隐约觉得应该是dynamic programming

我为自己感到羞愧,这道题是算法书上的原题!这道题被用来讲divide and conquer方法

solution:from beginning to i, find the contiguous subarray ending with i which has the largest sum

public class Solution {
    public int maxSubArray(int[] A) {

        int max = A[0];
        int[] sum = new int[A.length];
        sum[0] = A[0];

        for (int i = 1; i < A.length; i++) {
            sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
            max = Math.max(max, sum[i]);
        }

        return max;

    }
}

这个解法的复杂度是O(n)

More practice: divide and conquer

时间: 2024-10-08 19:52:10

leetcode 24th round的相关文章

LeetCode Guess Number Higher or Lower II

原题链接在这里:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether

【Leetcode】Guess Number Higher or Lower II

题目链接: 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a

Leetcode: Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r

[LeetCode 题解]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

[LeetCode] Dota2 Senate 刀塔二参议院

In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-bas

[LeetCode][JavaScript]Power of Three

Power of Three Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? https://leetcode.com/problems/power-of-three/ 要求判断一个数是不是3的幂次方. 最直观的做法是不停除以3,看余数是否为零. 题目要求不能用递归和循环,

[LeetCode][JavaScript]Bulb Switcher

Bulb Switcher There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My Submissions Total Accepted: 1546 Total Submissions: 5408 Difficulty: Medium We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess w

Python LeetCode(七)

不会做 1. Search in Rotated Sorted Array 这道题的目的不在于解决它,而是如何更快的解决,这要求更低的时间复杂度,因此任何时间复杂度为n的方案应该都要被否决掉,只有n/2的解决方案才能被留下来. 这题不能简单的用二分查找的方式来做,因为它并不知道那个旋转的点在哪,因此并不知道应该向哪个方向移动,所以我们要确定target和mid是哪个方向的,像在二分查找中,如果 mid > target,则会将 right 移动到 mid 的位置,但其实不对,因为你并不知道此时m