Skip to content

best [terminology]

Terminology

Usage

  • Use for an optimum found so far.
  • It implies repeated maximum or minimum comparisons.
  • Prefer it over ans when the algorithm explicitly tracks the best candidate.
  • Use a compound name such as best_l and best_r when multiple pieces describe the optimum.
cur = nums[0] # best sum of a subarray ending at current position
best = nums[0] # best sum seen anywhere
for x in nums[1:]:
    # either extend previous subarray or start at x
    cur = max(x, cur + x)
    best = max(best, cur)

See 53. Maximum Subarray.