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

Offen
#868 1 Kommentar 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@PeteChiang1211 arbeitet bereits daran.

Seit 26.11.2026.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

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.

Vorherrschende Sprache
Rust
Sterne
236
Forks
153
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
187

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus sysprog21/codetrial

Alle Issues in sysprog21/codetrial

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.