-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】7問目 1235. Maximum Profit in Job Scheduling #66
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
base: main
Are you sure you want to change the base?
Conversation
| end_profit_heap = [(0, 0)] | ||
| for start, i in sorted_starts: | ||
| max_profit_use_i = 0 | ||
| pre_info = () |
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.
pre_info という変数名から、変数の中に何が入っているのか想起できませんでした。「あるジョブを開始する前に終了していたジョブの中で、利益の最も大きかったもの」が入っているのだと思います。 best_end_time_and_profit あたりでいかがでしょうか?
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.
確かに中身が分からず、何を表しているか読み取るのが大変ですね。
変数名が長くなりそうで諦めてしまっていました。
| @@ -0,0 +1,31 @@ | |||
| # startTimeでソートして、繋げられるものを順番に確認していく | |||
| # 終了時刻の早いものから見ていき、最大の利益になるものを選択していく | |||
| # (最大の利益にならないものは、それ以降でも最大にならないため捨てて良い) | |||
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.
あるジョブを開始する際、その時刻以前に終了したジョブの中で、最も利益の総和が大きかったものを、次以降のジョブを開始する際の選択肢として残しておく、という点がポイントのように思いました。その部分についてコメントしてあるとよいと思いました。
| sorted_starts = sorted([(startTime[i], i) for i in range(len(startTime))]) | ||
| max_profit = 0 | ||
| end_profit_heap = [(0, 0)] | ||
| for start, i in sorted_starts: |
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.
i のスコープがやや広いため、 job_index 等、叙述的に書いたほうが良いと思います。
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.
for文の中だけでも、nestが深くなる場合にはちゃんと名付けたほうが良さそうということですね。
| max_profit = 0 | ||
| end_profit_heap = [(0, 0)] | ||
| for start, i in sorted_starts: | ||
| max_profit_use_i = 0 |
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.
直訳が「i を使う最大の利益」となっており、変数の中に何が格納されているのか分かりにくく感じました。「i 番目のジョブを使った際の最大の利益」を表したいのだと思いますので max_profit_using_current_job あたりがよいと思いました。ただ、 current に情報がないため、もう少し別の単語を使ったほうが良いかもしれません。
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.
iでも情報量変わらないかと思いましたが、ちゃんと単語で書かないと伝わらないですね。
| @@ -0,0 +1,31 @@ | |||
| # startTimeでソートして、繋げられるものを順番に確認していく | |||
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.
自分がこの問題を読んだときに思い付いた解法は、
- 時間を座標圧縮する。
- 圧縮後の時間 i の時点での最大の利益を求める動的計画法をする。
でした。 level 1~3 の解法は思い浮かびませんでした。
level 1 は、第一感やや複雑に感じました。 level 2~3 は、スマートだと感じました。
| ) -> int: | ||
| jobs = list(zip(startTime, endTime, profit)) | ||
| jobs.sort() | ||
| max_profit_begin_i = [None] * len(jobs) |
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.
max_profit_begin_i と言う変数名から、変数に格納されている値がどのようなものか想起できませんでした。 max_profits で十分だと思います。
| @@ -0,0 +1,21 @@ | |||
| # DP | |||
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.
DP ではなく、メモ付き再帰だと思います。なお「メモ付き再帰」は競技プログラミング用語だと思います。
念のため、ループを使った DP でも書いてみていただけますか?
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.
DPとメモ付き再帰は同じものだと思っていました。
再帰の結果を保存するのがメモ付き再帰で、
ループで結果を保存するのがDP?
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.
"メモ付き再帰"で調べたら、競技プログラミングの第3,4世代の用例が多かったので、これはあまり下の世代は使わない用語?
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.
自分は「メモ付き再帰」「メモ化再帰」は phoenixstarhiro か nya3jp のどちらかから聞いた気がします。自分自身は「キャッシュ付き再帰」と呼んでいた気がします。
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.
DPとメモ付き再帰は同じものだと思っていました。 再帰の結果を保存するのがメモ付き再帰で、 ループで結果を保存するのがDP?
はい、自分はそのように認識しています。
問題
https://leetcode.com/problems/maximum-profit-in-job-scheduling/