Skip to content

Conversation

@jiku0730
Copy link
Owner

@jiku0730 jiku0730 commented Jun 3, 2025

@jiku0730 jiku0730 changed the title 完了!! 53. maximum-subarray Jun 3, 2025

currentSum = nums[0];
maxSum = nums[0];
i = 1;
Copy link

@fuga-98 fuga-98 Jun 3, 2025

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;
みたいにしない理由ってありますか?

Copy link

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++;
Copy link

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

![提出結果]({EA17A920-E9C3-44E7-9D6F-FE87CB3DE6DB}.png)

OK!
けど計算時間もうちょい短く出来そう?

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;

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);

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);

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

```
参考にしたコード。

なんでこれでいけんのや??
Copy link

Choose a reason for hiding this comment

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

あ、このコードを読んで、「なんでこれでいけんのや」と思ったということは、よりよい説明や変数名があった可能性があります。
「いま見ている位置 i を終点とする区間、で取り得る最大和」と読めなかったということですよね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants