Skip to content
Merged
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
97 changes: 97 additions & 0 deletions 競技プロ就活部PR用/50. Pow(x, n).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
```
負の数の割り算と余り、言語によって振る舞いが色々なので自分の使い慣れたものについては覚えておきましょう。
```

## 計算式による解法
---
時間計算量: O(1)<br>
空間計算量: O(1)<br>
```python
class Solution:
def myPow(self, x: float, n: int) -> float:
return float(x ** n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

x ** n はもともと float 型だと思いますので、 float() で囲わなくてよいと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これfloatキャストしているのは何ででしょう。

```

## 再帰による解法 (maximum recursion depth exceeded)
---

### 1回目
時間計算量: O(N)<br>
空間計算量: O(N)<br>
よく考えたら、```if n == 1: return x```は不要。
また、n == -1 は、最初にnが正負かで値の更新を行った方が、わかりやすい。
(2回目以降で反映。)

```python
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 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.

関数が float を返していますので、整数の 1 ではなく float の 1.0 を返したほうが自然だと思います。

if n == 1:
return x
if n == -1:
return 1 / x

return x * self.myPow(x, n - 1) if n > 0 else 1 / x * self.myPow(x, n + 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.

負の数だった場合、 return self.myPow(1.0 / x, -n) とすることで、この行をシンプルにできると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

認知負荷が高いと感じます。

```

### 2回目
時間計算量: O(logN)<br>
空間計算量: O(logN)<br>

分割統治法のイメージで書いたが、直感的に何をしているのかわかづらい。
```python
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
x = 1 / x
n *= -1

divided_prod = self.myPow(x, n // 2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

divided_prod は変数名の意味が分かりませんでした。変数に含まれる値の意味を、日本語で説明してみていただけますか?

if n % 2 == 0:
return divided_prod * divided_prod
else:
return divided_prod * divided_prod * x
```

### 3回目
時間計算量: O(logN)<br>
空間計算量: O(logN)<br>
```python
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
x = 1 /x
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

/ の両隣には空白を空けましょう。

n *= -1
if n % 2 == 0:
return self.myPow(x * x, n // 2)
else:
return x * self.myPow(x * x, n // 2)
```



## Interationによる解法
---
時間計算量: O(logN)<br>
空間計算量: O(logN)<br>
```python
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
x = 1 /x
n *= -1

accumulated_prod = 1
while n > 0:
if n % 2 != 0:
accumulated_prod *= x
x *= x
n //= 2

return accumulated_prod
```