-
Notifications
You must be signed in to change notification settings - Fork 0
solve: 213.House Robber II #36
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
t9a-dev
wants to merge
1
commit into
main
Choose a base branch
from
213.House-Robber-II
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
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
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,83 @@ | ||
| // Step1a | ||
| // 目的: 別の解法を練習する。空間計算量がO(1)となる解法を実装する。 | ||
|
|
||
| // 方法 | ||
| // 5分考えてわからなかったら答えをみる | ||
| // 答えを見て理解したと思ったら全部消して答えを隠して書く | ||
| // 5分筆が止まったらもう一回みて全部消す | ||
| // 正解したら終わり | ||
|
|
||
| /* | ||
| 問題の理解 | ||
| - 家毎に置いてある金額を表す配列numsが与えられる。家から盗める金額の最大値を答えとして返す。 | ||
| 制約として隣接する家からは金を盗めない。 | ||
| 家は円形に並んでいるのでnums[0],nums[nums.len() - 1]は隣接していることになる。 | ||
|
|
||
| 解法について | ||
| - 前回の問題(198.House Robber)のレビューで提案してもらった解法で解いてみた。 | ||
| 存在しない家(nums[-2],nums[-1])の金を0として仮定している部分が考え方として番兵に近い気がする。 | ||
| https://github.com/t9a-dev/LeetCode_arai60/pull/35#discussion_r2564647801 | ||
| 自分でこの解法を思いつくまでの距離は大分ある気がするものの、何をしているのかは理解できている。 | ||
|
|
||
| 何を考えて解いていたか | ||
| 以下の二通りのケースでそれぞれの最大値を求めて、最後に大きい方を確認して返す。 | ||
| - 最初の家[0]を含み最後の家を除く場合(0..nums.len()-1) | ||
| - 最初の家[0]を含まず最後の家を含む場合(1..nums.len()) | ||
|
|
||
| 正解してから気づいたこと | ||
| - 最大値を求めるループのコードが重複しているのでまとめられる。 | ||
| - テストコードで検知できたが、nums.len() == 1のケースをチェックしないと想定通り動かない。 | ||
| コーディング試験で行われるようなホワイトボード上でのコーディングだとテストコードによるテスト実行はできないので、配列の境界周りのロジックは大分注意が必要だと思った。 | ||
| */ | ||
|
|
||
| pub struct Solution {} | ||
| impl Solution { | ||
| pub fn rob(nums: Vec<i32>) -> i32 { | ||
| if nums.is_empty() { | ||
| return 0; | ||
| }; | ||
| if nums.len() == 1 { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| let mut two_before_max = 0; | ||
| let mut one_before_max = 0; | ||
| for i in 0..nums.len() - 1 { | ||
| let current_max = one_before_max.max(two_before_max + nums[i]); | ||
| two_before_max = one_before_max; | ||
| one_before_max = current_max; | ||
| } | ||
| let with_first_max = one_before_max; | ||
|
|
||
| two_before_max = 0; | ||
| one_before_max = 0; | ||
| for i in 1..nums.len() { | ||
| let current_max = one_before_max.max(two_before_max + nums[i]); | ||
| two_before_max = one_before_max; | ||
| one_before_max = current_max; | ||
| } | ||
| let with_out_first_max = one_before_max; | ||
|
|
||
| with_first_max.max(with_out_first_max) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn step1a_test() { | ||
| assert_eq!(Solution::rob(vec![1, 2, 3, 1]), 4); | ||
| assert_eq!(Solution::rob(vec![2, 3, 2]), 3); | ||
| assert_eq!(Solution::rob(vec![1, 2, 3]), 3); | ||
| assert_eq!(Solution::rob(vec![6, 2, 3, 4, 6]), 10); | ||
| assert_eq!(Solution::rob(vec![6, 6, 3, 4, 6]), 12); | ||
| assert_eq!(Solution::rob(vec![6, 6, 3, 4, 6, 7, 8]), 20); | ||
|
|
||
| assert_eq!(Solution::rob(vec![1]), 1); | ||
| assert_eq!(Solution::rob(vec![2, 1]), 2); | ||
|
|
||
| assert_eq!(Solution::rob(vec![]), 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 |
|---|---|---|
|
|
@@ -12,26 +12,61 @@ | |
| // 改善する時に考えたこと | ||
|
|
||
| /* | ||
| 講師陣はどのようなコメントを残すだろうか? | ||
| - | ||
|
|
||
| 他の人のコードを読んで考えたこと | ||
| - | ||
|
|
||
| 他の想定ユースケース | ||
| - | ||
| - Vecのような可変長配列の一部分を別のところへ渡す時にコピーが行われないかはだいぶ気にするようになってきた。 | ||
| Pythonだとスライスでコピーが発生する仕様らしいので覚えておく必要があると思った。 | ||
| https://github.com/Kaichi-Irie/leetcode-python/pull/6#discussion_r2171435011 | ||
| 同じ指摘を見つけたのでPythonにおけるスライスの暗黙的なコピーはよくある落とし穴っぽいと思った。 | ||
| https://github.com/ryosuketc/leetcode_arai60/pull/49#discussion_r2217263665 | ||
|
|
||
| 改善する時に考えたこと | ||
| - | ||
| - 重複しているループのコードをまとめる。 | ||
| */ | ||
|
|
||
| pub struct Solution {} | ||
| impl Solution {} | ||
| impl Solution { | ||
| pub fn rob(nums: Vec<i32>) -> i32 { | ||
| if nums.is_empty() { | ||
| return 0; | ||
| } | ||
| if nums.len() == 1 { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| let collect_max_amount = |targets: &[i32]| { | ||
| let mut two_before_max = 0; | ||
| let mut one_before_max = 0; | ||
| for target in targets { | ||
| let current_max = one_before_max.max(two_before_max + *target); | ||
| two_before_max = one_before_max; | ||
| one_before_max = current_max; | ||
| } | ||
| one_before_max | ||
| }; | ||
|
|
||
| let with_first_max_amount = collect_max_amount(&nums[0..nums.len() - 1]); | ||
| let with_out_first_max_amount = collect_max_amount(&nums[1..nums.len()]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 細かいですが、withoutで一語なのでwith_outの部分はつなげたいですね。 |
||
|
|
||
| with_first_max_amount.max(with_out_first_max_amount) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn step2_test() {} | ||
| fn step2_test() { | ||
| assert_eq!(Solution::rob(vec![1, 2, 3, 1]), 4); | ||
| assert_eq!(Solution::rob(vec![2, 3, 2]), 3); | ||
| assert_eq!(Solution::rob(vec![1, 2, 3]), 3); | ||
| assert_eq!(Solution::rob(vec![6, 2, 3, 4, 6]), 10); | ||
| assert_eq!(Solution::rob(vec![6, 6, 3, 4, 6]), 12); | ||
| assert_eq!(Solution::rob(vec![6, 6, 3, 4, 6, 7, 8]), 20); | ||
|
|
||
| assert_eq!(Solution::rob(vec![1]), 1); | ||
| assert_eq!(Solution::rob(vec![2, 1]), 2); | ||
|
|
||
| assert_eq!(Solution::rob(vec![]), 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
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.
エッジケースの処理、こう書いてみてもいいかもしれません。