-
Notifications
You must be signed in to change notification settings - Fork 0
50. Pow(x, n) #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
50
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−0
Open
50. Pow(x, n) #40
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # step1 何も見ずに解く | ||
|
|
||
| 普通に計算しようと思ったが、n の最大が 2^31 - 1 なので線形時間だと 1 秒以内に間に合わない。 | ||
|
|
||
| ```ruby | ||
| # @param {Float} x | ||
| # @param {Integer} n | ||
| # @return {Float} | ||
| def my_pow(x, n) | ||
| res = 1 | ||
| n.abs.times do | ||
| if n < 0 | ||
| res /= x | ||
| else | ||
| res *= x | ||
| end | ||
| end | ||
| res | ||
| end | ||
| ``` | ||
|
|
||
| 再帰とメモ化で n を半分ずつに分けて計算すれば O(logN)に計算量を落とせるので間に合うのではないかと考えた。 | ||
| 空間計算量も O(logN)になる | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exsp | ||
| # @return {Float} | ||
| def my_pow(base, exsp) | ||
| exsp_to_pow = {} | ||
| calculate_my_pow = lambda do |exsp| | ||
| return 1 if exsp.zero? | ||
| return base if exsp == 1 | ||
| return exsp_to_pow[exsp] if exsp_to_pow.key?(exsp) | ||
|
|
||
| result = 1.0 | ||
| exsp_abs = exsp.abs | ||
| if exsp_abs.even? | ||
| exsp_left = exsp_right = exsp_abs / 2 | ||
| else | ||
| exsp_left = exsp_abs / 2 | ||
| exsp_right = (exsp_abs + 1) / 2 | ||
| end | ||
| if exsp >= 0 | ||
| result *= calculate_my_pow.call(exsp_left) | ||
| result *= calculate_my_pow.call(exsp_right) | ||
| else | ||
| result /= calculate_my_pow.call(exsp_left) | ||
| result /= calculate_my_pow.call(exsp_right) | ||
| end | ||
| exsp_to_pow[exsp] = result | ||
| result | ||
| end | ||
| calculate_my_pow.call(exsp) | ||
| end | ||
| ``` | ||
|
|
||
| calculate_my_pow では`exsp`が正の場合だけ計算して、外で exsp が負であれば割るのも良いかも。 | ||
| Ruby の冪乗を計算するための`Integer#**`メソッドも時間計算量 O(log)になっているか気になったのでコードを見た。 | ||
| バイナリ法という方法で計算することにより、同じ値を計算する必要がないのでメモ化がいらない。 | ||
| 時間計算量は O(logN) | ||
| 参考にすると以下のようになる。 | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exsp | ||
| # @return {Float} | ||
| def my_pow(base, exsp) | ||
| return 1 if exsp == 0 | ||
| return base if exsp == 1 | ||
|
|
||
| exsp = exsp.abs | ||
| power = 1 | ||
| current_base = base | ||
| bit = exsp | ||
| while bit > 0 | ||
| if (bit & 1) == 1 | ||
| power *= current_base | ||
| end | ||
| current_base *= current_base | ||
| bit = bit >> 1 | ||
| end | ||
| exsp < 0 ? 1 / power : power | ||
| end | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # step2 他の方の解答を見る | ||
|
|
||
| https://github.com/TORUS0818/leetcode/pull/47 | ||
|
|
||
| step1の再帰の方法であったとしても、子ノードが一つしか生まれないようにすればメモ化が必要なくなるのか。 | ||
| exponentが偶数のときは、exponentを半分に減らせて子ノードは1つしかできない。 | ||
| 奇数のときは次のノードのexponentが偶数になるようにすれば良い。 | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exponent | ||
| # @return {Float} | ||
| def my_pow(base, exponent) | ||
| return 1 if exponent.zero? | ||
| return 1.0 / my_pow(base, - exponent) if exponent < 0 | ||
|
|
||
| if exponent.even? | ||
| return my_pow(base, exponent / 2) ** 2 | ||
| else | ||
| return base * my_pow(base, exponent - 1) | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| こっちはもちろんノードが二つになるので以下だと時間以内に解けない | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exponent | ||
| # @return {Float} | ||
| def my_pow(base, exponent) | ||
| return 1 if exponent.zero? | ||
| return 1.0 / my_pow(base, - exponent) if exponent < 0 | ||
|
|
||
| if exponent.even? | ||
| # ノードが二つになる | ||
| return my_pow(base, exponent / 2) * my_pow(base, exponent / 2) | ||
| else | ||
| return base * my_pow(base, exponent - 1) | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| https://github.com/TORUS0818/leetcode/pull/47#discussion_r2038337006 | ||
| 確かに bit を 1 にして左にシフトしていくことで捜査する方が自然 | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exponent | ||
| # @return {Float} | ||
| def my_pow(base, exponent) | ||
| return 1 if exponent == 0 | ||
|
|
||
| if exponent < 0 | ||
| base = 1.0 / base | ||
| exponent = - exponent | ||
| end | ||
| pow = 1 | ||
| current_base = base | ||
| bit = 1 | ||
| while bit <= exponent | ||
| pow *= current_base if (exponent & bit) != 0 | ||
| current_base *= current_base | ||
| bit <<= 1 | ||
| end | ||
| pow | ||
| end | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {Float} base | ||
| # @param {Integer} exponent | ||
| # @return {Float} | ||
| def my_pow(base, exponent) | ||
| return 1 if exponent.zero? | ||
| return 1.0 / my_pow(base, -exponent) if exponent < 0 | ||
|
|
||
| if exponent.even? | ||
| return my_pow(base, exponent / 2) ** 2 | ||
| else | ||
| return base * my_pow(base, exponent - 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 趣味の範囲ですが、 |
||
| end | ||
| end | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## step4 レビューを受けて解答を修正 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文脈によっては二項演算子の左右を lhs, rhs と書くと伝わることがあると思いました。その場合は *= ではなく
result = calculate_my_pow.call(exsp_left) * calculate_my_pow.call(exsp_right)
と書いたらわかりやすそうです。