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
26 changes: 26 additions & 0 deletions BestTimetoBuyandSellStock/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
```go
// For each day,
// We need to check if it's day that we can buy the stock with lower price
// And then check what if we sell it with the lowest price that we found in the past
// Time complexity: O(N) N: number of prices
// Space complexity: 1
// Time spend: 04:32
func maxProfit(prices []int) int {
buy := -prices[0];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、負の値を入れたのが、min を定義するのが億劫という理由だけのように見えていますが、なにか理由ございますでしょうか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

購入価格を負の数で表すのはなるほどとなりました!
個人的には正の数で表して、利益は引き算で計算するやり方が直感的でした。

sell := buy + prices[0];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この部分、何か考えて初期化していますか。


for _, p := range prices {
buy = max(buy, -p)
sell = max(sell, buy + p)
}

return sell
}

func max(a, b int) int {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Go全然知らないのですが現在は組み込み関数のmaxがあるようです?
https://future-architect.github.io/articles/20230815a/

if a > b {
return a
}
return b
}
```
21 changes: 21 additions & 0 deletions BestTimetoBuyandSellStock/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```go
// Time spend: 01:10
func maxProfit(prices []int) int {
buy := -prices[0]
sell := buy + prices[0]

for _, p := range prices {
buy = max(buy, -p)
sell = max(sell, buy + p)
Comment on lines +8 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

buy, sellがそれぞれ最小購入価格、最大売却価格の意味を持つことを変数名で表すとわかりやすいかと思いました。
minBuyingPrice, maxSellingPrice

}

return sell
}

func max(a, b int) int {
if a < b {
return b
}
return a
}
```
71 changes: 71 additions & 0 deletions BestTimetoBuyandSellStock/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
```go
// Time spend: 01:00
package main
func maxProfit(prices []int) int {
buy := -prices[0]
sell := buy + -prices[0]

for _, p := range prices {
buy = max(buy, -p)
sell = max(sell, buy + p)
}

return sell
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

```

```go
// Time spend: 01:02
package main
func maxProfit(prices []int) int {
buy := -prices[0]
sell := buy + -prices[0]

for _, p := range prices {
buy = max(buy, -p)
sell = max(sell, buy + p)
}

return sell
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

```

```go
// Time spend: 00:55
package main
func maxProfit(prices []int) int {
buy := -prices[0]
sell := buy + -prices[0]

for _, p := range prices {
buy = max(buy, -p)
sell = max(sell, buy + p)
}

return sell
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

```