Implement maxProfit function for stock prices#20
Merged
argondev22 merged 1 commit intomainfrom Nov 7, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
コーディング面接対策 - 解答評価レポート
📊 総合評価:4点 / 10点
✅ 良い点
- 正確性: アルゴリズムは正しく動作し、正しい答えを返します
- コードの明確性: ロジックが直感的で理解しやすい
- 変数名:
maxProfit,todayProfitなど、意図が明確な命名 - 境界条件: 配列の範囲チェックが適切
⚠️ 改善が必要な点
1. 計算量の問題(重大)
- 時間計算量: O(n²) - 二重ループにより、大規模入力で実行時間超過(TLE)の可能性が高い
- LeetCodeでは制約が
1 ≤ prices.length ≤ 10^5のため、最悪ケースで約100億回の演算が発生
2. 非効率なアプローチ
- すべての買い売りのペアを試しているため、無駄な計算が多い
- 「過去の最安値で買って今日売る」という単純な戦略で解ける問題
🏆 最適解との比較
あなたの解法(ブルートフォース)
// 二重ループで全ペアを確認
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
const todayProfit = prices[j] - prices[i];
maxProfit = Math.max(maxProfit, todayProfit);
}
}
最適解(ワンパス)
function maxProfit(prices: number[]): number { let minPrice = prices[0]; let maxProfit = 0;for (let i = 1; i < prices.length; i++) { maxProfit = Math.max(maxProfit, prices[i] - minPrice); minPrice = Math.min(minPrice, prices[i]); } return maxProfit;
}
アルゴリズムの違い
| 項目 | あなたの解法 | 最適解 |
|---|---|---|
| アプローチ | 全ペア探索 | 動的計画法的思考 |
| 核心アイデア | 各買い日に対して全売り日を試す | 「今日まで見た最安値」を記録 |
実行時間の例(n = 10,000の場合)
- あなたの解法: 約5000万回の演算
- 最適解: 約1万回の演算
- 差: 約5000倍
🔧 あなたのコードの具体的な改善
Step 1: 問題を言い換える
「i日目に買ってj日目に売る最大利益」
↓
「今日売るとしたら、過去の最安値で買った場合の利益」
Step 2: 改善版コード
function maxProfit(prices: number[]): number { let minPrice = prices[0]; // これまで見た最安値 let maxProfit = 0;for (let i = 1; i < prices.length; i++) { // 今日売った場合の利益を計算 const todayProfit = prices[i] - minPrice; maxProfit = Math.max(maxProfit, todayProfit); // 最安値を更新 minPrice = Math.min(minPrice, prices[i]); } return maxProfit;
}
Step 3: さらに洗練
function maxProfit(prices: number[]): number { let minPrice = Infinity; let maxProfit = 0;for (const price of prices) { maxProfit = Math.max(maxProfit, price - minPrice); minPrice = Math.min(minPrice, price); } return maxProfit;
}
📚 学習ポイント
1. 「貪欲法」の考え方
- 「各時点で最善の選択をする」アプローチ
- この問題では「今日までの最安値を常に記録」
2. 状態管理パターン
今まで見た中で重要な情報 = 最安値のみ
→ O(1)の追加メモリで解決可能
3. 計算量削減の典型パターン
二重ループ O(n²)
↓
「前処理 + 一回のループ」でO(n)に改善
4. 面接での思考プロセス
- ブルートフォース解を考える(あなたはここまで完了✅)
- 「何を記憶すれば二重ループを避けられるか?」を考える
- 状態変数を最小化する
5. 類似問題への応用
- Maximum Subarray (Kadane’s Algorithm)
- Best Time to Buy and Sell Stock II/III/IV
- Sliding Window問題全般
📝 まとめ
あなたの現状
- ✅ 正しい解答を導ける基礎力がある
- ✅ コードの構造が理解しやすい
⚠️ 計算量の最適化に課題
次のステップ
- 必ず最初に計算量を分析する習慣をつける
- **「今の状態で何を覚えておくべきか?」**を常に考える
- 二重ループを見たら「一回のループで解けないか?」を自問する
- 類似問題(Maximum Subarray等)で貪欲法のパターンを練習
面接での評価予想
- 実務: 動作するが、パフォーマンス改善が必要
- アルゴリズム面接: アプローチの説明とO(n)解への改善を求められる
- 改善提案できれば: 8-9点レベルに到達可能
頑張ってください!🚀
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Status
Time Limit Exceeded (203 / 212 testcases passed)
Time Taken
33 m 52 s