Skip to content

283. Move Zeroes#54

Open
garunitule wants to merge 3 commits intomainfrom
283
Open

283. Move Zeroes#54
garunitule wants to merge 3 commits intomainfrom
283

Conversation

@garunitule
Copy link
Copy Markdown
Owner

"""
Do not return anything, modify nums in-place instead.
"""
non_zero_values = [num for num in nums if num != 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.

選択肢の幅としてはいいですが、今回の制約は破ってしまいそうですね。

Note that you must do this in-place without making a copy of the array.

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.

一時的に別のlistに退避した後にnumsを修正しているので問題ないと考えていました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(追加の)空間計算量をO(1)にしてくださいというのが問題の意図ですね。

Comment on lines +195 to +198
if first_zero_index < i:
nums[first_zero_index] = nums[i]
nums[i] = 0
first_zero_index += 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.

first_zero_index < i という条件が腹落ちしにくいので(そもそもiの方が先行しているのだから常に成り立ちそう、と思ってしまう)、この条件をチェックせずにスワップする方が好みです。

Suggested change
if first_zero_index < i:
nums[first_zero_index] = nums[i]
nums[i] = 0
first_zero_index += 1
nums[first_zero_index], nums[i] = nums[i], nums[first_zero_index]
first_zero_index += 1

Copy link
Copy Markdown
Owner Author

@garunitule garunitule Mar 23, 2026

Choose a reason for hiding this comment

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

first_zero_index < i という条件が腹落ちしにくいので(そもそもiの方が先行しているのだから常に成り立ちそう、と思ってしまう)

この観点はなかったです、ありがとうございます。
swapにするかどうかは悩ましいと思いました。
私はこの処理でやりたいことは下記だと思っていて、そのままプログラムに書き起こすと私が書いたようなコードになると思っています。
スワップにするとどうしても入れ替えるというニュアンスが強くなるので、元の意図から外れてしまうと思いif文を入れたほうが良いのかなと思いました。
ただ、swapでも良い部分だとは思いました。

- nums[i] == 0のとき:何もしない。ゼロ領域が伸びるだけで不変条件は維持されるので。
- nums[i] != 0のとき
  - first_zero_index == i:非ゼロ領域と未調査領域しかない。現在の要素を未調査領域から非ゼロ領域にすればいいので、first_zero_indexをインクリメントする。
  - first_zero_index < i: 非ゼロ領域、ゼロ領域、未調査領域がある。現在の要素を非ゼロ領域の末尾に移動し、現在の位置はゼロ領域とする。

Comment on lines +108 to +110
- 0 <= index < first_zero_index: 非ゼロ領域
- first_zero_index <= index < i: ゼロ領域
- i <= index: 未調査領域
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

first_zero_index = 0 と初期化していて、ループが i = 0 から始まるので、この辺の不変条件が成り立たないように思いました。また、不変条件といいつつ i が出てくる(ループカウンタによって条件が変化する)、というのも違和感がありました。

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.

i = 0, first_zero_index = 0の場合は、処理開始時点で下記のようになるので正しいかと思っていました。

  • 0 <= index < 0: 非ゼロ領域は空。処理を進めていないため非ゼロ領域は確定していないので空なのは正しい。
  • 0 <= index < 0: ゼロ領域は空。処理を進めていないため非ゼロ領域は確定していないので空なのは正しい。
  • 0 <= index: 未調査領域が全領域。処理を進めていないため全てが未調査であるのは正しい。

また、不変条件といいつつ i が出てくる(ループカウンタによって条件が変化する)、というのも違和感がありました。

もし表現方法でよいものがあったら教えていただけたら嬉しいです。
i回目の試行の場合に、i-1までで不変条件を考える必要があって書き方は悩ましいと考えていました。

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.

後で考えてみて、このように記載した方がシンプルだと思いました

 - nums[0:write] には nums[0:i] の中の非ゼロ値が元の順序で詰まっている
 - nums[write:i] はゼロで埋まっている

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 <= index < 0 のうち 0 < 0 の部分が、数学的にちょっと気持ち悪いなと思いました。不等式でなく「first_zero_index よりも左は非ゼロ領域であることが確定している」という記述の方がしっくりきました。こう書くと、last_fixed_non_zero_index = -1 などもアリかなと感じました。

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に初期化するのもより分かりやすいと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants