【问题标题】:My function to find the max profit with k transactions returns an empty array我用 k 笔交易找到最大利润的函数返回一个空数组
【发布时间】:2020-05-18 21:33:54
【问题描述】:

我写了一个函数来找出恰好 k 笔交易的最大利润,一笔交易包括以低价买入和以高价卖出“你不能在同一天买卖,必须先完成一笔交易”例如给定 [ 100, 180, 260, 310, 40, 535, 695 ],2 应该返回 865 当天买入:0 当天卖出:3 当天买入:4 当天卖出:6,总买入 = 140,总卖出 = 105,最大利润 = 865 我为此编写了一个函数,但它返回一个空数组


   function maxProfit(price, k) {
     // check for the availability of at least two prices and 1 transaction
     if ((k = 0 || price.length < 1)) return 0;

     // Initialize the profit;
     let profit = [];

     //Create count for each cycle of transaction
     for (let t = 1; t <= k; t++) {
       for (let i = 0; i < price.length; i++) {
         // Find the day's Minimal by comparing present element to the next element
         if (price[i + 1] <= price[i]) i++;
         // When you find the first minimal then Find another day's Maximal
         else
           for (let j = i + 1; j <= price.length; j++) {
             // The day you find a higher price than you bought is the day at which the stock should be sold
             if (price[j] > price[i]) {
               let curr_profit = price[j] - price[i] + maxProfit(price, t + 1);

               // Update the maximum profit so far
               profit = Math.max(profit, curr_profit);
             }
           }
       }
     }
     // Update the profit so far
     return profit;
   }

//This is returning an empty array and I can't figure out why

【问题讨论】:

  • 请修正缩进
  • 您永远不会更新 profit 数组。您只能潜在地覆盖变量,但永远不会向数组本身添加任何内容。

标签: javascript arrays algorithm function output


【解决方案1】:

试试这个:

function maxProfit(price, k) { 

   // check for the availability of at least two prices and 1 transaction
    if (k = 0 || price.length < 1) 
        return 0; 

   // Initialize the profit;
    let profit =0;

   //Create count for each cycle of transaction
    for (let t = 1; t <= k; t++) {

        for (let i = 0; i < price.length; i++) { 

            // Find the day's Minimal by comparing present element to the next element
            if (price[i + 1] <= price[i])
                    i++
            else
                // When you find the first minimal then Find another day's Maximal
                for (let j = i + 1; j <= price.length; j++) { 

                    // The day you find a higher price than you bought is the day at which the stock should be sold
                    if (price[j] > price[i]) {

                        let curr_profit = price[j] - price[i] + maxProfit(price, t+1);

                        // Update the maximum profit so far
                        profit = Math.max(profit, curr_profit);   
                    } 
                }

        }

   }
    let profitArr = [profit];
       // Update the profit so far
       return profitArr
   }

只是将利润值存储为数组,因为您从未对数组执行任何操作

【讨论】:

    【解决方案2】:

    在第 2 行(If 语句)中,您使用了赋值运算符,而不是相等运算符。

    由于 k = 0,执行永远不会进入 for 循环 "for (let t = 1; t "

     if ((k = 0 || price.length < 1)) return 0; // old
     if ((k == 0 || price.length < 1)) return 0; // new
    

    另外,为了更好的优化,最好去掉尾调用递归。

    let curr_profit = price[j] - price[i] + maxProfit(price, t + 1);
    

    【讨论】:

    • 哎呀,谢谢,现在它返回一个数组,但它是错误的答案,它返回 595 而不是 865,这意味着它只执行一个。
    猜你喜欢
    • 2022-12-03
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多