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
69 changes: 69 additions & 0 deletions 6/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# step1 何も見ずに解く

3行の場合であれば、
文字の先頭から1行目->2行目->3行目->2行目->1行目->2行目
みたいな感じで進んでいって、各行の文字を左から見た文字を追加していけば良い。

Nを文字列の長さとすると、
時間計算量はO(N)
空間計算量もO(N)
Nの最大値は1000なので余裕で1秒以内に間に合う。

```ruby
# @param {String} s
# @param {Integer} num_rows
# @return {String}
def convert(s, num_rows)
return s if num_rows == 1

chars_by_row = Array.new(num_rows) { [] }
row = 0
is_down = true
s.each_char do |char|
chars_by_row[row] << char

if row.zero?
is_down = true
row = 1
elsif row == num_rows - 1
is_down = false
row = num_rows - 2
else
row = is_down ? row + 1 : row - 1
end
end
chars_by_row.inject("") { |result, chars| result << chars.join }
Copy link

Choose a reason for hiding this comment

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

chars_by_row.map(&:join).join

これでいけましたっけ。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。
確かにそっちの方が簡潔で良いですね。いけました。

end
```

Rubyの文字列はミュータブルなので以下のように書いてもコストは変わらない。

```ruby
# @param {String} s
# @param {Integer} num_rows
# @return {String}
def convert(s, num_rows)
return s if num_rows == 1

strings_by_row = Array.new(num_rows) { "" }
row = 0
is_down = true
s.each_char do |char|
strings_by_row[row] << char

if row.zero?
is_down = true
row = 1
elsif row == num_rows - 1
is_down = false
row = num_rows - 2
else
row = is_down ? row + 1 : row - 1
end
end
strings_by_row.join
end
```

むしろ手元でベンチマークをとったらこっちの方がわずかにパフォーマンスがよかった。
わかりやすさは変わらないのでどっちでもいいな。
26 changes: 26 additions & 0 deletions 6/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# step2 他の方の解答を見る
- https://github.com/olsen-blue/Arai60/pull/61

step1で`is_down`としていたが、`is_going_down`の方がいいかも。`is_downward`とか。
あとis_downの切り替えとrowの更新を同時にやらずに分けた方が見やすいな。

```ruby
# @param {String} s
# @param {Integer} num_rows
# @return {String}
def convert(s, num_rows)
return s if num_rows == 1

chars_by_row = Array.new(num_rows) { [] }
row = 0
is_downward = true
s.each_char do |char|
chars_by_row[row] << char

is_downward = true if row.zero?
is_downward = false if row == num_rows - 1
row = is_downward ? row + 1 : row - 1
end
chars_by_row.inject("") { |result, chars| result << chars.join }
end
```
22 changes: 22 additions & 0 deletions 6/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# step3 3回続けて10分以内に書いてエラーを出さなければOKとする

```ruby
# @param {String} s
# @param {Integer} num_rows
# @return {String}
def convert(s, num_rows)
return s if num_rows == 1

strings_by_row = Array.new(num_rows) { "" }
row = 0
is_downward = true
Copy link

Choose a reason for hiding this comment

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

これをdirectionというような名前の1と-1を切り替える変数にして、rowの更新をrow + directionで済ませてしまうようなコードもよく見ます。(どちらでもよいと思います)

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
その発想はなかったです。

s.each_char do |char|
strings_by_row[row] << char

is_downward = true if row.zero?
is_downward = false if row == num_rows - 1
row = is_downward ? row + 1 : row - 1
end
strings_by_row.join
end
Copy link

Choose a reason for hiding this comment

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

0...num_rowsの範囲でループを回し、その行に来るであろうindexを一回で全て計算してしまいstringに入れていく解法もあると思います。
(自分が解いたときは多分違う配列にアクセスを切り替える頻度が少なくなるおかげでこちらの解法の方が少し実行が速かったです)

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
それぞれの行に来る文字のインデックスには規則性があるので、それを利用する感じですよね。

```

Choose a reason for hiding this comment

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

良いと思います。

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