Giter Club home page Giter Club logo

Comments (5)

snowan avatar snowan commented on May 5, 2024 2

我试试这个吧.. 😁
认领

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

我试试这个吧..
认领

done

from leetcode.

xiongcaihu avatar xiongcaihu commented on May 5, 2024
/**
 * @param {number[]} nums
 * @return {number}
 * 
 * F(n) = F(n-1) + nums[n] when F(n-1)>0
 * or
 * F(n) = nums[n] when F(n-1)<=0
 */
// var maxSubArray = function (nums) {
//     var dp = [nums[0]];
//     for (var i = 1; i < nums.length; i++) {
//         if (dp[i - 1] > 0) {
//             dp[i] = dp[i - 1] + nums[i];
//         } else {
//             dp[i] = nums[i];
//         }
//     }

//     return Math.max(...dp);
// };
var maxSubArray = function (nums) {
    var dp = nums[0],
        max = dp;
    for (var i = 1; i < nums.length; i++) {
        if (dp > 0) {
            dp += nums[i]
        } else {
            dp = nums[i];
        }
        max = Math.max(dp, max);
    }

    return max;
};

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

我试试这个吧.. 😁
认领

leetcode 原题 53号题目

from leetcode.

raof01 avatar raof01 commented on May 5, 2024

分治法:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        auto l=0,r=0,m=0,s=0;
        helper(nums, 0, nums.size() - 1, l, r, m, s);
        return m;
    }
private:
    void helper(vector<int>& v, size_t s, size_t e, int& l, int& r, int& m, int& sum) {
        if (s == e) {
            l = r = m = sum = v[s];
            return;
        }
        auto mid = s + (e - s) / 2;
        int l1, r1, m1, s1, l2, r2, m2, s2;
        helper(v, s, mid, l1, r1, m1, s1);
        helper(v, mid + 1, e, l2, r2, m2, s2);
        l = max(l1, s1 + l2);
        r = max(r2, s2 + r1);
        m = max(max(m1, m2), r1 + l2);
        sum = s1 + s2;
    }
};

from leetcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.