Upgrade to Pro — share decks privately, control downloads, hide ads and more …

算法初步

Thomas
February 02, 2017

 算法初步

该 PPT 来自,七月在线的算法课程,如有侵权请通知。

Thomas

February 02, 2017
Tweet

More Decks by Thomas

Other Decks in Programming

Transcript

  1. 复杂度 • O(n!) 枚举全排列 • 总结: 优秀 O(1) < O(logn)

    < O(n1/2) < O(n) < O(nlogn) 可能可以优化 O(n2) < O(n3) < O(2n) < O(n!)
  2. 暴力枚举 • 暴力枚举:三重循环 for i  1 to n for

    j  i to n sum  a[i]+..+a[j] ans  max(ans, sum) 时间复杂度 O(n3) 附加空间复杂度 O(1)
  3. 优化枚举 • 优化枚举:两重循环 for i  1 to n sum

     0 for j  i to n sum  sum + a[i] ans  max(ans, sum) 时间复杂度 O(n2) 附加空间复杂度 O(1)
  4. 贪心法 • 贪心法:一重循环 sum  0 ans  0 for

    i  1 to n sum  sum + a[i] ans  max(sum, ans) if (sum < 0) sum  0 时间复杂度 O(n) 附加空间复杂度 O(1)