Git Product home page Git Product logo

Comments (3)

monika113 avatar monika113 commented on May 4, 2024 1

我又看了一下,确实是优化前缀和。
但0不是最小值,我的解法的唯一区别其实就是没有维护一个最小值。
事实上,tmpSum清零操作和更新最小值是等价的。当出现更小的S(i)的时候,tmpSum才会<0,tmpSum = S(i) - min。

非常有趣,我思考这道题的时候并没有意识到我用到了最小前缀和:)

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024

我的理解你这个是前缀和。 但是你的0是哪里来的?难道是最小值么?但是0也不一定是最小值吧?如果把你的0换成min,就和我的代码一样了。

function LSS(list) {
  const len = list.length;
  let max = list[0];
  let min = 0;
  let sum = 0;
  for (let i = 0; i < len; i++) {
    sum += list[i];
    if (sum - min > max) max = sum - min;
    if (sum < min) {
      min = sum;
    }
  }

  return max;
}

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024

我觉得你只是用了对整体”有益与否“这个**吧?如果大于0,那么对整体有利,则保留,否则对整体不利,我们就舍去。 然后用一个变量维护最大值即可

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.