Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions 121/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# step1 何も見ずに解く

ある日に売る時に得られる最大の利益は、「ある日での売り値 - ある日以前での最安値」で求めることができる。
ある日以前の最安値と、最大の利益を変数で更新していけば時間計算量O(N)となり、Nの最大値は10^5なので1秒以内に間に合う。
空間計算量はO(1)

```ruby
# @param {Integer[]} prices
# @return {Integer}
def max_profit(prices)
prices_size = prices.size
return 0 if prices_size <= 1

min_price = prices.first
max_profit = 0
(1...(prices_size)).each do |i|
price = prices[i]
max_profit = [max_profit, price - min_price].max
min_price = [min_price, price].min
end
max_profit
end
```

6 changes: 6 additions & 0 deletions 121/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# step2 他の方の解答を見る

https://github.com/hayashi-ay/leetcode/pull/52/files#diff-0474f0ee7711182f0e97bb4047531dc4c65356748eafab139512400ac88c5c0bR67-R79

後ろから見て売る時の最高値を更新していき、最安値の時に買うパターン。
個人的には買う->売るという作業の流れを逆にしたくないので好みではない。
20 changes: 20 additions & 0 deletions 121/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# step3 3回続けて10分以内に書いてエラーを出さなければOKとする

step1と同じになった。
```ruby
# @param {Integer[]} prices
# @return {Integer}
def max_profit(prices)
prices_size = prices.size
return 0 if prices_size <= 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

試せていないので間違っていたら申し訳ないのですが、この行はなくても良い気がしました。
.each doの部分が回らずにそのまま0が出力されるのではないでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
おっしゃる通りなくても良いのですがサイズが1以下の時の処理をわかりやすくするために残していました。


min_price = prices.first
max_profit = 0
(1...(prices_size)).each do |i|
price = prices[i]
max_profit = [max_profit, price - min_price].max
min_price = [min_price, price].min
end
max_profit
end
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

price[i] が min_price より小さい場合は max_profit の更新はないため、min_price を更新して次のループに進むのもよさそうかなと思いました。

def max_profit(prices)
  prices_size = prices.size
  return 0 if prices_size <= 1

  min_price = prices.first
  max_profit = 0

  (1...prices_size).each do |i|
    if prices[i] < min_price
        min_price = prices[i]
        next
    end
    max_profit = [max_profit, prices[i] - min_price].max
  end

  max_profit
end

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
確かにそっちの方が意味がわかりやすいです。
どんな時でもmax_profitの更新をする必要はありませんね。

1 change: 1 addition & 0 deletions 121/step4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## step4 レビューを受けて解答を修正