Skip to content

Maximum Profit In Job Scheduling#126

Open
tom4649 wants to merge 2 commits into
mainfrom
1235.Maximum-Profit-in-Job-Scheduling
Open

Maximum Profit In Job Scheduling#126
tom4649 wants to merge 2 commits into
mainfrom
1235.Maximum-Profit-in-Job-Scheduling

Conversation

@tom4649

@tom4649 tom4649 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@@ -0,0 +1,25 @@
class Solution:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

座標圧縮によるマッピングとendTimeでのソートによるマッピングが入り混じっているので読みづらいと思います。
こんな感じでどうでしょう?

from dataclasses import dataclass

@dataclass
class Job:
    start: int
    end: int
    profit: int

class Solution:
    def jobScheduling(self, startTime: list[int], endTime: list[int], profit: list[int]) -> int:
        time_to_index = {t: i for i, t in enumerate(sorted(set(startTime) | set(endTime)))}
        
        jobs = [
            Job(time_to_index[startTime[i]], time_to_index[endTime[i]], profit[i])
            for i in range(len(startTime))
        ]
        jobs.sort(key=lambda j: j.end)
        
        dp = [0] * len(time_to_index)
        i = 0
        for job in jobs:
            while i < job.end:
                dp[i + 1] = dp[i]
                i += 1
                
            dp[job.end] = max(dp[job.end], dp[job.start] + job.profit)
            
        return dp[-1]

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jobクラスを定義したことで元のコードから大分シンプルになっていますね。end_previous = end もインクリメントするだけなので冗長だったように思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants