Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions house-robber-ii/DaleSeo.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

러스트 코드는 볼때마다 느끼는거지만 엄청 복잡하게 느껴지는것 같아요 그러면서 로직은 엄청 깔끔하게 보여서 또 신기합니다!
고생하셨습니다!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

러스트의 매력입니다 ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// TC: O(n)
// SC: O(1)
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
let n = nums.len();
if n == 1 {
return nums[0];
}
Self::rob_line(&nums[..n - 1]).max(Self::rob_line(&nums[1..]))
}

fn rob_line(houses: &[i32]) -> i32 {
houses
.iter()
.fold((0, 0), |(prev, curr), &money| (curr, curr.max(prev + money)))
.1
}
}
Loading