-
Notifications
You must be signed in to change notification settings - Fork 0
Maximum Profit In Job Scheduling #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
1235.Maximum-Profit-in-Job-Scheduling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # 1235. Maximum Profit in Job Scheduling | ||
|
|
||
| ## step1 | ||
|
|
||
| 流れ: | ||
|
|
||
| DPで解くことを考える -> 制約的に時間計算量はO(n^2)が厳しいと判断 -> 価値が制約にならないのでナップザックDPは難しそうだと考える -> 二次元DPは時間計算量的に難しそうだと判断 -> start timeのソート、座標圧縮 | ||
|
|
||
| 30m ぐらい考えて方針が立たず、Hint 2を見て解いた。 | ||
|
|
||
| > Hint 2: Sort the elements by starting time, then define the dp[i] as the maximum profit taking elements from the suffix starting at i. | ||
|
|
||
| 二分探索を用いて O(nlog n)で解けることには気がついた。 | ||
|
|
||
| 変数名などを適当にとりあえず書いたのが step1.py, 変数名を改善したのが revised | ||
|
|
||
| ### LLMとの対話 | ||
|
|
||
| 私がこの問題を解いた時の思考の流れは以下の通りでした。この問題をHintを見ずに自力で解くためには、どのような思考が必要なのでしょうか? | ||
|
|
||
| > ここで必要なのは、「『どの区間を組み合わせるか』ではなく、『各時点で、それ以降の未来の最適な選択は何か』に注目する」 という思考の切り替えです。 | ||
|
|
||
| > ブレイクスルー:「時間軸の『ある時点 $i$』に自分が立っているとする。ここから先の未来(サフィックス)だけを見て、得られる最大利益はいくらか?」 | ||
|
|
||
| > 依存関係を一方通行にする「未来から過去へ」の思考: | ||
|
|
||
| > 多くの人はソートした後に「前から順に(プレフィックスで)」DPを組もうとして挫折します。 | ||
|
|
||
| > 閃きのトリガー: | ||
| > ソートした基準(今回は startTime)が、次の状態の条件(endTime <= startTime)と直結している場合、「条件を満たす要素が綺麗に片側に寄る方向」からDPを埋めるのが鉄則です。 | ||
|
|
||
|
|
||
| ## step2 | ||
|
|
||
| もう一度考えてみると、自力で異なる解法を導けた: sol2_from_start.py | ||
|
|
||
| 前から見ていくのでこちらの方が個人的には素直。step1で解けなかったのは、start time のソートにこだわって、end timeのソートを深く考えなかったため。時間tでちょうど終わる時刻がある場合に更新が行われることがポイントだと思う。 | ||
|
|
||
| ## 他の人のコード | ||
|
|
||
| https://github.com/shining-ai/leetcode/pull/66 | ||
|
|
||
| > DPとメモ付き再帰は同じものだと思っていました。 再帰の結果を保存するのがメモ付き再帰で、 ループで結果を保存するのがDP? | ||
|
|
||
| level_2, level_3は自分のstep1と同じ解法だがメモ化再帰で書かれている。こちらの方がわかりやすいと感じた。 | ||
|
|
||
|
|
||
| > level 1 は、第一感やや複雑に感じました | ||
|
|
||
| ヒープの解法。自分はDPしか頭になかったので全く思いつかなかった。 | ||
| 手続き的に考えると自然にも思えると個人的には思った。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def jobScheduling( | ||
| self, startTime: List[int], endTime: List[int], profit: List[int] | ||
| ) -> int: | ||
| jobs = list(sorted(zip(startTime, endTime, profit))) | ||
| jobs.append((float("inf"), float("inf"), 0)) | ||
| max_profit = 0 | ||
| running_jobs = [] | ||
| for job_start, job_end, job_profit in jobs: | ||
| while running_jobs and running_jobs[0][0] <= job_start: | ||
| _, max_profit_from_running_job = heapq.heappop(running_jobs) | ||
| max_profit = max(max_profit, max_profit_from_running_job) | ||
| heapq.heappush(running_jobs, (job_end, max_profit + job_profit)) | ||
| return max_profit | ||
| ``` | ||
|
|
||
| ## step3 | ||
| TODO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def jobScheduling( | ||
| self, startTime: List[int], endTime: List[int], profit: List[int] | ||
| ) -> int: | ||
| start_and_index = sorted((t, i) for i, t in enumerate(startTime)) | ||
| dp = [0] * len(startTime) | ||
|
|
||
| for i, (start, index) in enumerate(reversed(start_and_index)): | ||
| if i == 0: | ||
| dp[-i - 1] = profit[index] | ||
| continue | ||
| end = endTime[index] | ||
| i_after = bisect.bisect_left(start_and_index, (end, -1)) | ||
| if i_after >= len(start_and_index): | ||
| dp[-i - 1] = max(dp[-i], profit[index]) | ||
| continue | ||
| dp[-i - 1] = max(dp[-i], profit[index] + dp[i_after]) | ||
|
|
||
| return dp[0] |
32 changes: 32 additions & 0 deletions
32
1235.Maximum-Profit-in-Job-Scheduling/step1_from_last_revised.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def jobScheduling( | ||
| self, startTime: list[int], endTime: list[int], profit: list[int] | ||
| ) -> int: | ||
| jobs_sorted_by_start = sorted((t, i) for i, t in enumerate(startTime)) | ||
|
|
||
| # dp[index_job]: the maximum profit taking elements from the suffix starting at index_job | ||
| dp = [0] * len(startTime) | ||
|
|
||
| for index_reversed, (start, index_job) in enumerate( | ||
| reversed(jobs_sorted_by_start) | ||
| ): | ||
| index_dp = len(startTime) - 1 - index_reversed | ||
|
|
||
| if index_reversed == 0: | ||
| dp[index_dp] = profit[index_job] | ||
| continue | ||
|
|
||
| end = endTime[index_job] | ||
|
|
||
| index_next_job = bisect.bisect_left(jobs_sorted_by_start, (end, -1)) | ||
|
|
||
| if index_next_job >= len(startTime): | ||
| dp[index_dp] = max(dp[index_dp + 1], profit[index_job]) | ||
| continue | ||
|
|
||
| dp[index_dp] = max(dp[index_dp + 1], profit[index_job] + dp[index_next_job]) | ||
|
|
||
| return dp[0] |
30 changes: 30 additions & 0 deletions
30
1235.Maximum-Profit-in-Job-Scheduling/step2_from_last_recursive.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import bisect | ||
| import functools | ||
|
|
||
|
|
||
| class Solution: | ||
| def jobScheduling( | ||
| self, startTime: list[int], endTime: list[int], profit: list[int] | ||
| ) -> int: | ||
| jobs_sorted_by_start = sorted((t, i) for i, t in enumerate(startTime)) | ||
| sorted_to_original_index = { | ||
| job_index: original_index | ||
| for job_index, (_, original_index) in enumerate(jobs_sorted_by_start) | ||
| } | ||
|
|
||
| @functools.cache | ||
| def max_profit_after(job_index: int): | ||
| if job_index >= len(startTime): | ||
| return 0 | ||
|
|
||
| original_index = sorted_to_original_index[job_index] | ||
| next_job_index = bisect.bisect_left( | ||
| jobs_sorted_by_start, (endTime[original_index], -1) | ||
| ) | ||
|
|
||
| return max( | ||
| max_profit_after(job_index + 1), | ||
| profit[original_index] + max_profit_after(next_job_index), | ||
| ) | ||
|
|
||
| return max_profit_after(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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_sorted_by_end = sorted( | ||
| (time_to_index[end], i) for i, end in enumerate(endTime) | ||
| ) | ||
|
|
||
| # dp[t]: the maximum profit up to time t | ||
| dp = [0] * len(time_to_index) | ||
| end_previous = 0 | ||
|
|
||
| for end, original_index in jobs_sorted_by_end: | ||
| for t in range(end_previous + 1, end): | ||
| dp[t] = dp[end_previous] | ||
|
|
||
| start = time_to_index[startTime[original_index]] | ||
|
|
||
| dp[end] = max(dp[end_previous], dp[start] + profit[original_index]) | ||
| end_previous = end | ||
|
|
||
| return dp[end_previous] | ||
35 changes: 35 additions & 0 deletions
35
1235.Maximum-Profit-in-Job-Scheduling/step2_from_start_revised.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 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[t]: the maximum profit up to time t | ||
| 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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
座標圧縮によるマッピングとendTimeでのソートによるマッピングが入り混じっているので読みづらいと思います。
こんな感じでどうでしょう?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jobクラスを定義したことで元のコードから大分シンプルになっていますね。end_previous = end もインクリメントするだけなので冗長だったように思います。