Skip to content
Open
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
83 changes: 83 additions & 0 deletions leetcode/arai60/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 283. Move Zeroes
* 問題: https://leetcode.com/problems/move-zeroes/
* 言語: Python

## Step1
* 単純にforループで `nums[i] == 0` のときにその位置の要素を削除し後ろに `0` を追加していく方針としたがWA
- 要素を削除する前に取得した添字(インデックス)が指す要素と削除した後の添字が指す要素はズレる
* このことに気づくのと、コピーを作成せずにin-placeな実装を考えるのに時間がかかった

### 解答(AC)
```py
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 1:
return None

nums_i = 0
nums_len = len(nums)
for _ in range(nums_len):
if nums[nums_i] == 0:
nums.pop(nums_i)
Copy link

Choose a reason for hiding this comment

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

list の末尾以外の要素に対して pop() を呼び出すと、それより後の要素を手前にずらさなければならないため、時間計算量 O(n) がかかり、重くなります。原則避けることをおすすめします。

nums.append(0)
nums_i -= 1
nums_i += 1
```
* 解答時間: 40:00
* 時間計算量: $O(n^2)$
- `pop()`は $O(n)$

## Step2
* 典型コメント: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.v62rdhwkdymb
* https://github.com/fhiyo/leetcode/pull/54
- Python
- `nonzeros` に非ゼロな値をコピーして退避し、それを`nums` に追加していく方法
- (コピー作成してもOKなんだ…)

Choose a reason for hiding this comment

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

これはコピーしても ok というか、補助的なリスト (nonzeros) を用意して、最終的には nums を mutate していますね。コピーしたリストを返しているわけではないので、念のためコメントさせてください。

- 非ゼロ要素とゼロ要素を交換するクイックソート的なアルゴリズム
- forループ、whileループを使った方法
- リストの代わりにジェネレーターとセイウチ演算子を使う方法
- `nums` から `0` が存在しなくなるまで `nums.remove(0)` を試行する方法
* https://github.com/olsen-blue/Arai60/pull/55
- Python
- 上記と同じ方法
- 挿入ソートっぽい方法
- バブルソートっぽい方法
- 探索範囲がすべて非ゼロの場合、その時点でループを打ち切る

### 読みやすく書き直したコード
* 非ゼロ要素を先頭に移動する部分とゼロ要素で埋める部分で分ける方法
```py
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
non_zero_pos = 0
for i in range(len(nums)):
if nums[i] == 0:
continue
nums[non_zero_pos] = nums[i]
non_zero_pos += 1

for i in range(non_zero_pos, len(nums)):
nums[i] = 0
```

## Step3
```py
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
non_zero_pos = 0

Choose a reason for hiding this comment

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

pos よりも index の方が馴染みがあるようには思いますが、好みの範囲かと思います。

for i in range(len(nums)):
if nums[i] == 0:
continue
nums[non_zero_pos] = nums[i]
non_zero_pos += 1

for i in range(non_zero_pos, len(nums)):
nums[i] = 0
```

Choose a reason for hiding this comment

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

良いと思います。

* 解答時間
- 1回目: 1:54
- 2回目: 1:38
- 3回目: 1:37