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 0088.Merge-Sorted-Array/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 88. Merge Sorted Array

## step1

4mほど。マージソートとは設定が異なるようだ。配列の末尾から並べていくことがポイント。

## step2

条件分岐に不要なところがあったので改善。

merge sortに近いものも書く。

## 他の人のコード

https://github.com/sota009/swe-coding-practice/pull/1

> あってもいいと思います。m == 0 のほうは私は消すかもしれません。書き終わった直後に、これなくてもいいですかね、とコメントするとわりと読み慣れている人だなと思います。

色々と考えてコードを書いている。

> "In many ways the object returned by range() behaves as if it is a list, but in fact it isn't. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn't really make the list, thus saving space." https://docs.python.org/3/tutorial/controlflow.html

> スペースは食わないみたい

## step3
今回は省略
43 changes: 43 additions & 0 deletions 0735.Asteroid-Collision/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 735. Asteroid Collision

## step1
自然に左から順に処理していけばよい。23mほどかかった。

間違えた点:
- 絶対値が等しい時の処理を忘れた
- 最後に右に動いているasteroidを加えるのを忘れた

時間計算量、空間計算量ともにO(n)

## step2
解答を一瞬見るとスタックは一つで十分らしいことが分かったので考え直す。大きくシンプルになった。

まだ答えがあっていても最もシンプルなコードに直接辿りつけないことがある。

## 他の解法

https://github.com/sota009/swe-coding-practice/pull/2

https://leetcode.com/problems/asteroid-collision/solutions/8325855/stack-collision-simulation-on-beats-9415-66yi/

while else という文法を知らなかった。基礎的な話なのかもしれない。

> while-else への私の所感は、使ってもいいが、必要となる状況では関数化することなどによってより整理できる可能性が高い、というものです。

https://docs.python.org/3/reference/compound_stmts.html#the-while-statement

> while_stmt: "while" assignment_expression ":" suite
> ["else" ":" suite]

> C言語だと、 a < b < c が a < b and b < cにならないそうです。
> 言語間の違いを意識せずに済むという意味で、
> andを省略せずに書いてあげるのが親切かなと思いました。

これは自分も同意する



## step3
自分はwhile-elseを使いそうにないので、使わない方を書く。
処理を少しだけまとめた。

34 changes: 34 additions & 0 deletions 0735.Asteroid-Collision/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution:
def asteroidCollision(self, asteroids: list[int]) -> list[int]:
asteroids_moving_left = []
asteroids_moving_right = []
for asteroid in asteroids:
if asteroid > 0:
asteroids_moving_right.append((asteroid, len(asteroids_moving_left)))
else:
while asteroids_moving_right and asteroids_moving_right[-1][0] < abs(
asteroid
):
asteroids_moving_right.pop()
if asteroids_moving_right and asteroids_moving_right[-1][0] == abs(
asteroid
):
asteroids_moving_right.pop()
continue
if not asteroids_moving_right:
asteroids_moving_left.append(asteroid)

result = []
index_moving_right = 0
for i in range(len(asteroids_moving_left) + 1):
while index_moving_right < len(asteroids_moving_right) and (
i == len(asteroids_moving_left)
or i >= asteroids_moving_right[index_moving_right][1]
):
asteroid, _ = asteroids_moving_right[index_moving_right]
result.append(asteroid)
index_moving_right += 1
if i < len(asteroids_moving_left):
result.append(asteroids_moving_left[i])

return result
21 changes: 21 additions & 0 deletions 0735.Asteroid-Collision/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def asteroidCollision(self, asteroids: list[int]) -> list[int]:
stack = []
for asteroid in asteroids:
if asteroid > 0:
stack.append(asteroid)
continue

while stack and stack[-1] > 0 and stack[-1] < abs(asteroid):
stack.pop()

if stack and stack[-1] == abs(asteroid):
stack.pop()
continue

if stack and stack[-1] > abs(asteroid):
continue

stack.append(asteroid)

return stack
16 changes: 16 additions & 0 deletions 0735.Asteroid-Collision/step2_while_else.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def asteroidCollision(self, asteroids: list[int]) -> list[int]:
stack = []
for asteroid in asteroids:
while asteroid < 0 and stack and stack[-1] > 0:
if stack[-1] == abs(asteroid):
stack.pop()
break
elif stack[-1] < abs(asteroid):
stack.pop()
else:
break
else:
stack.append(asteroid)

return stack
19 changes: 19 additions & 0 deletions 0735.Asteroid-Collision/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def asteroidCollision(self, asteroids: list[int]) -> list[int]:
stack = []
for asteroid in asteroids:
if asteroid > 0:
stack.append(asteroid)
continue

while stack and stack[-1] > 0 and stack[-1] < abs(asteroid):
stack.pop()

if stack and stack[-1] > 0:
if stack[-1] == abs(asteroid):
stack.pop()
continue

stack.append(asteroid)

return stack