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
17 changes: 17 additions & 0 deletions best-time-to-buy-and-sell-stock/DaleSeo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TC: O(n)
// SC: O(1)
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut min_price = i32::MAX;
let mut max_profit = 0;
for price in prices {
if price < min_price {
min_price = price;
}
if price - min_price > max_profit {
max_profit = price - min_price;
}
}
max_profit
Copy link
Contributor

Choose a reason for hiding this comment

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

코드가 직관적이어서 새로운 언어였지만 이해에 무리가 없었습니다! 저는 이 문제를 투 포인터로도 풀어보았는데요, 이 방법으로도 풀이해보셔도 좋을 것 같아요~!

그나저나 러스트에서 값을 반환할 때는 그냥 변수명만 써도 되는군요! 신기합니다 🤩

Copy link
Member Author

Choose a reason for hiding this comment

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

네, 대신에 맨 뒤에 ;이 없어야 합니다 ㅎㅎ 신기한 언어에요 😆

}
}