Highway Billboard Leasing accepts a solution that greedily leases the largest offer first

Open
#868 1 comment 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@PeteChiang1211 is already working on this.

Since Nov 26, 2026.

Assessment

This issue has not been assessed yet.

Description

A solution that sorts the sites by offer and leases each one whose neighbours are still free passes all five house-robber cases, although it is not correct.

Wrong solution (Python):

class Solution:
    def bestLeaseRevenue(self, nums):
        taken, total = set(), 0
        for i in sorted(range(len(nums)), key=lambda i: -nums[i]):
            if i - 1 not in taken and i + 1 not in taken:
                taken.add(i)
                total += nums[i]
        return total

It returns the right answer for every case in problem-bank/judges.json ([1, 2, 3, 1], [2, 7, 9, 3, 1], [5], [0, 0, 0], [2, 1, 1, 2]), because in each of them the largest offer belongs to an optimal set.

It fails when the largest offer sits between two smaller offers whose sum is bigger:

nums expected greedy
[2, 3, 2] 4 3
[4, 5, 4, 1] 8 6

Both inputs are inside the stated domain (1 to 100 sites, offers 0 to 400).

Proposed fix: add a case such as [2, 3, 2] (label along the lines of "largest offer is not in the best set") to the judge, and a test that pins the greedy solution to that case so removing the case fails the test, following the jump-game and stock III judge fixes. I would like to work on this.

Dominant language
Rust
Stars
236
Forks
153
Avg merge
1d 9h
Merged PRs (30d)
187

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sysprog21/codetrial

All issues in sysprog21/codetrial

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.