-
Notifications
You must be signed in to change notification settings - Fork 0
53. maximum-subarray #4
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| currentSum = nums[0]; | ||
| maxSum = nums[0]; | ||
| i = 1; |
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.
C言語あまり知らないのですが、
int currentSum = nums[0];
int maxSum = nums[0];
int i = 1;
みたいにしない理由ってありますか?
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.
42(という学校)のスタイルがちょっと変わっていてこんな感じです。
(教育的配慮からくるものですね。)
まあ、こういうスタイルもあるくらいに思っていただけるといいかと思います。
| currentSum += nums[i]; | ||
| if (maxSum < currentSum) | ||
| maxSum = currentSum; | ||
| i++; |
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.
ぶら下がりif文は事故が起きやすいので、私は避けます。
https://discord.com/channels/1084280443945353267/1084283898617417748/1370794488607543306
|  | ||
|
|
||
| OK! | ||
| けど計算時間もうちょい短く出来そう? |
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.
LeetCodeの時間計測は誤差が大きいので、timeコマンド等を使ってご自身の環境で計測して評価するのも良いかと思いました。
準備、計測方法は以下が参考になると思います。
yukib0123/LeetCode#19 (comment)
| int maxSubArray(int *nums, int numsSize) | ||
| { | ||
| int *dp; | ||
| int i; |
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.
読み手が上から読んで行った時に覚えておくべき変数を減らせるという観点から、i をループの直前、あるいはfor (int i = 1; i < numsSize; i++)のように書くのも良いと思いました。
今回は覚えておくべきものが少ないので問題にならないと思いますが。
| int i; | ||
| int max_sum; | ||
|
|
||
| dp = malloc(sizeof(int) * numsSize); |
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.
i番目までのmax_sumという意味でmax_sum_so_farなどつけた方が理解の助けになる情報があって良いと思います。
| int numsSize = 4; | ||
|
|
||
| printf("%i\n", maxSubArray(nums, numsSize)); | ||
| return (0); |
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.
C++のスタイルガイドからの引用で恐縮ですが、()付きのreturnはあまり見ない気がします。
(Cのガイドか何かに沿ったものでしたらすみません。。)
Do not needlessly surround the return expression with parentheses.
https://google.github.io/styleguide/cppguide.html#Return_Values
| ``` | ||
| 参考にしたコード。 | ||
|
|
||
| なんでこれでいけんのや?? |
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.
あ、このコードを読んで、「なんでこれでいけんのや」と思ったということは、よりよい説明や変数名があった可能性があります。
「いま見ている位置 i を終点とする区間、で取り得る最大和」と読めなかったということですよね。
This Problem
https://leetcode.com/problems/maximum-subarray/description/
Next Problem
https://leetcode.com/problems/permutations/description/