-
Notifications
You must be signed in to change notification settings - Fork 0
560. Subarray Sum Equals K #22
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
syoshida20
wants to merge
5
commits into
main
Choose a base branch
from
feature/hash-map/subarray-sum-equals-k
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
25055d7
feat : #22 add the empty markdown file for creating a PR
syoshida20 c1a2f4b
feat : #22 add the STEP1/STEP2/STEP3
syoshida20 11cf97d
feat : #22 add the other PRs/ oda comments
syoshida20 abcaa18
feat : #22 fix the comments
syoshida20 d2ab86b
feat : #22 add the solution with stations on the mountain concept
syoshida20 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,279 @@ | ||
| # Title | ||
|
|
||
| ## STEP1 | ||
|
|
||
| * 発想(誤り) | ||
| * しゃくとり虫みたいに、2つのポインターを用意し、 | ||
| 合計値より小さい場合には、右のポインターを動かし、 | ||
| 合計値より大きい場合には、左のポインターを動かす。 | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| let currentSum = nums[0] | ||
| let left = 0 | ||
| let right = 0 | ||
| let subArrayCount = 0 | ||
| while (right < nums.length) { | ||
| if (currentSum === k) { | ||
| subArrayCount++ | ||
| } | ||
| if (left < right && k < currentSum) { | ||
| currentSum -= nums[left] | ||
| left++ | ||
| continue | ||
| } | ||
| right++ | ||
| currentSum += nums[right] | ||
| } | ||
| return subArrayCount | ||
| }; | ||
| ``` | ||
|
|
||
| * 上の発想では、要素にマイナスのケースが入ることを考慮できていなかった。 | ||
| ---> 答えを見る。 | ||
|
|
||
| * 発想 (正しい) | ||
| * for文で回す | ||
| * 各インデックスをsubarrayのお尻となるインデックスを固定し、 | ||
| 先頭となるインデックスを累積和をkey, 回数をvalueとするハッシュテーブルから見つける。 | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| const cumSumToCount = new Map() | ||
| cumSumToCount.set(0, 1) | ||
|
|
||
| let sum = 0 | ||
| let count = 0 | ||
| for (const num of nums) { | ||
| sum += num | ||
| if (cumSumToCount.has(sum - k)) { | ||
| count += cumSumToCount.get(sum - k) | ||
| } | ||
| if (!cumSumToCount.has(sum)) { | ||
| cumSumToCount.set(sum, 0) | ||
| } | ||
| cumSumToCount.set(sum, cumSumToCount.get(sum) + 1) | ||
| } | ||
| return count | ||
| }; | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
|
|
||
| * 特に治すところはないと感じた。 | ||
|
|
||
| * STEP3をやった後に、感じた修正点 | ||
|
|
||
| * sumという変数名よりもprefixSum, cumSumの方が良い。 | ||
|
|
||
| * 以下の記法でも良い。(https://github.com/goto-untrapped/Arai60/pull/28/files#r1641918143のレビューを見て) | ||
| JavaScriptでもPythonのDefaultDictのdefaultの値のようなことができる。 | ||
|
|
||
| (変更前) | ||
|
|
||
| ```javascript | ||
| if (!cumSumToCount.has(sum)) { | ||
| cumSumToCount.set(sum, 0) | ||
| } | ||
| cumSumToCount.set(sum, cumSumToCount.get(sum) + 1) | ||
| ``` | ||
|
|
||
| (変更後) | ||
|
|
||
| ```javascript | ||
| cumSumToCount.set(sum, (cumSumToCount.get(sum) || 0) + 1) | ||
| ``` | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| const cumSumToCount = new Map() | ||
| cumSumToCount.set(0, 1) | ||
| let count = 0 | ||
| let sum = 0 | ||
| for (const num of nums) { | ||
| sum += num | ||
| if (cumSumToCount.has(sum - k)) { | ||
| count += cumSumToCount.get(sum - k) | ||
| } | ||
| if (!cumSumToCount.has(sum)) { | ||
| cumSumToCount.set(sum, 0) | ||
| } | ||
| cumSumToCount.set(sum, cumSumToCount.get(sum) + 1) | ||
| } | ||
| return count | ||
| }; | ||
| ``` | ||
|
|
||
| ## STEP3 | ||
|
|
||
| * 時間計算量 : O(N) | ||
| * 空間計算量 : O(N) | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| const cumSumToCount = new Map() | ||
| cumSumToCount.set(0, 1) | ||
|
|
||
| let sum = 0 | ||
| let count = 0 | ||
| for (const num of nums) { | ||
| sum += num | ||
| if (cumSumToCount.get(sum - k)) { | ||
| count += cumSumToCount.get(sum - k) | ||
| } | ||
| if (!cumSumToCount.has(sum)) { | ||
| cumSumToCount.set(sum, 0) | ||
| } | ||
| cumSumToCount.set(sum, cumSumToCount.get(sum) + 1) | ||
| } | ||
| return count | ||
| }; | ||
| ``` | ||
|
|
||
| ## 感想 | ||
|
|
||
| ### コメント集を読んで | ||
|
|
||
| * 特になし | ||
|
|
||
| ### 他の人のPRを読んで | ||
|
|
||
| * katataku | ||
| * PR: https://github.com/katataku/leetcode/pull/15 | ||
| * 変数名は、sum よりも `prefixSum`, `cumSum`とすべきだった。 | ||
| https://github.com/katataku/leetcode/pull/15/files#diff-302b3c57a99f55a6ede5338b83f17a5d903d52dbeddd3fe485ae5f5d1cdc4badR64-R68 | ||
|
|
||
| * goto-untrapped | ||
| * PR: https://github.com/goto-untrapped/Arai60/pull/28/ | ||
| * mapのset時の短縮記法をJavascriptでもできることを知りました。 | ||
|
|
||
| * Harukawa2121 | ||
| * PR: https://github.com/Hurukawa2121/leetcode/pull/16/ | ||
|
|
||
| * hproc | ||
| * PR: https://github.com/hroc135/leetcode/pull/16/ | ||
| * https://github.com/hroc135/leetcode/pull/16/files#r1739569493 | ||
| * 実行時間を見積もってみる | ||
| * `*0`の場合、 | ||
| N = 2 * (10 ** 4) | ||
| 実行時間の見積もり | ||
| = 2 * (10 `**` 4) * 2 * (10 `**` 4) / (10 `**` 7) (C++よりもJavascriptが100倍遅いとする, 1G / 100 = 10 `**` 7) | ||
| = 40 seconds | ||
| 実際には、 | ||
| = 1.569 seconds | ||
| * 上で生じた差の考察としては、もう少しjavascriptの処理できるステップ数が大きいということなのだろうか? | ||
| * まだ考察できる引き出しが少ない。 | ||
|
|
||
| * Yoshiki-Iwasa | ||
| * https://github.com/Yoshiki-Iwasa/Arai60/pull/15 | ||
|
|
||
| * hayashi-ay | ||
| * PR: https://github.com/hayashi-ay/leetcode/pull/31/ | ||
| * DPを用いた方法が理解できなかった。 https://github.com/hayashi-ay/leetcode/pull/31/files#diff-302b3c57a99f55a6ede5338b83f17a5d903d52dbeddd3fe485ae5f5d1cdc4badR49-R61 | ||
|
|
||
| ## エラーだったコード | ||
|
|
||
| * `*0` 演算子の優先順位 | ||
|
|
||
| * 誤り | ||
|
|
||
| ```javascript | ||
| const cumSumToIndex = new Map() | ||
| cumSumToIndex.set(0, 1) | ||
| let cumSum = 0 | ||
| cumSumToIndex.set(cumSum, cumSumToIndex.get(0) || 0 + 1) | ||
| ``` | ||
|
|
||
| * 正解 | ||
| * `+`の演算子 が `||`の演算子よりも先に処理される | ||
| * 参考: 演算子の優先順位 https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Operator_precedence | ||
|
|
||
| ```javascript | ||
| const cumSumToIndex = new Map() | ||
| cumSumToIndex.set(0, 1) | ||
| let cumSum = 0 | ||
| cumSumToIndex.set(cumSum, (cumSumToIndex.get(0) || 0) + 1) | ||
| ``` | ||
|
|
||
| ## その他の解法 | ||
|
|
||
| * `*0` ブルートフォースの方法 | ||
| * N = numsの長さ | ||
| * 時間計算量 : O(N^2) | ||
| * 空間計算量 : O(1) | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| let subArrayCount= 0 | ||
| for (let i = 0; i < nums.length; i++) { | ||
| let sum = 0; | ||
| for (let j = i; j < nums.length; j++) { | ||
| sum += nums[j] | ||
| if (sum === k) { | ||
| subArrayCount++ | ||
| } | ||
| } | ||
| } | ||
| return subArrayCount | ||
| }; | ||
| ``` | ||
|
|
||
| * `*1` 累積和を使った方法 | ||
| * N = numsの長さ | ||
| * 時間計算量 : O(N^2) | ||
| * 空間計算量 : O(N) | ||
|
|
||
| ```javascript | ||
| const subarraySum = function(nums, k) { | ||
| const cumSum = new Array(nums.length + 1) | ||
| cumSum[0] = 0 | ||
| for (let i = 0; i < nums.length; i++) { | ||
| cumSum[i + 1] = cumSum[i] + nums[i] | ||
| } | ||
|
|
||
| let count = 0 | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = i; j < nums.length; j++) { | ||
| if (cumSum[j + 1] - cumSum[i] === k) { | ||
| count++ | ||
| } | ||
| } | ||
| } | ||
| return count | ||
| }; | ||
|
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. 特に問題ないと思いました。
Owner
Author
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. ありがとうございます。 cumSumがcumulativeSumであることは、確かに自明ではないですね。 |
||
| ``` | ||
|
|
||
| * `*2` 山に駅があり、標高差がKである駅を求めるというイメージで解いた方法 | ||
|
|
||
| ```javascript | ||
| // 山に駅があり、標高差がある。 | ||
| // それぞれの駅は前の駅との標高差が書いてある。 | ||
| // 駅と駅の間の標高差が、K mの組み合わせの個数を求める。 | ||
| const subarraySum = function(nums, k) { | ||
| const heightToCount = new Map() | ||
| heightToCount.set(0, 1) | ||
| let count = 0 | ||
| let currentHeight = 0 | ||
| for (const num of nums) { | ||
| currentHeight += num | ||
| const targetStationCount = heightToCount.get(currentHeight - k) | ||
| if (targetStationCount !== undefined) { | ||
| count += targetStationCount | ||
| } | ||
| const previousCount = heightToCount.get(currentHeight) || 0 | ||
| heightToCount.set(currentHeight, previousCount + 1) | ||
| } | ||
| return count | ||
| }; | ||
| ``` | ||
|
|
||
| ## 調べたこと | ||
|
|
||
| * cumulative sumをjavascriptでどのようにかけるか? | ||
| https://stackoverflow.com/questions/20477177/creating-an-array-of-cumulative-sum-in-javascript | ||
|
|
||
| * JavaScriptにおける演算子の優先順位 | ||
| * || よりも +が早いことを知らなかった | ||
| https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Operator_precedence | ||
|
|
||
|
|
||
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.
この1に違和感がなければ問題ないかと思います。
Uh oh!
There was an error while loading. Please reload this page.
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.
はい、違和感は無いです。
先頭の要素を含む部分列を含めるために必要です!