Skip to content

Conversation

@argondev22
Copy link
Owner

@argondev22 argondev22 commented Nov 7, 2025

Status

Time Limit Exceeded (203 / 212 testcases passed)

Time Taken

33 m 52 s

@argondev22 argondev22 self-assigned this Nov 7, 2025
Copilot AI review requested due to automatic review settings November 7, 2025 14:45
@argondev22 argondev22 linked an issue Nov 7, 2025 that may be closed by this pull request
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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


コーディング面接対策 - 解答評価レポート

📊 総合評価:4点 / 10点


✅ 良い点

  1. 正確性: アルゴリズムは正しく動作し、正しい答えを返します
  2. コードの明確性: ロジックが直感的で理解しやすい
  3. 変数名: maxProfit, todayProfit など、意図が明確な命名
  4. 境界条件: 配列の範囲チェックが適切

⚠️ 改善が必要な点

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 &lt; 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 &lt; 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. 面接での思考プロセス

  1. ブルートフォース解を考える(あなたはここまで完了✅)
  2. 「何を記憶すれば二重ループを避けられるか?」を考える
  3. 状態変数を最小化する

5. 類似問題への応用

  • Maximum Subarray (Kadane’s Algorithm)
  • Best Time to Buy and Sell Stock II/III/IV
  • Sliding Window問題全般

📝 まとめ

あなたの現状

  • 正しい解答を導ける基礎力がある
  • コードの構造が理解しやすい
  • ⚠️ 計算量の最適化に課題

次のステップ

  1. 必ず最初に計算量を分析する習慣をつける
  2. **「今の状態で何を覚えておくべきか?」**を常に考える
  3. 二重ループを見たら「一回のループで解けないか?」を自問する
  4. 類似問題(Maximum Subarray等)で貪欲法のパターンを練習

面接での評価予想

  • 実務: 動作するが、パフォーマンス改善が必要
  • アルゴリズム面接: アプローチの説明とO(n)解への改善を求められる
  • 改善提案できれば: 8-9点レベルに到達可能

頑張ってください!🚀

@argondev22 argondev22 merged commit 9d6b215 into main Nov 7, 2025
6 checks passed
@argondev22 argondev22 deleted the 20251107/19-121-best-time-to-buy-and-sell-stock branch November 7, 2025 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

121. Best Time to Buy and Sell Stock

2 participants