12
返回列表 发新帖
楼主: YankeeDoodle
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 微软近期高频面试题分享 + 分析(十一)

 
🔗
 楼主| YankeeDoodle 2021-7-28 09:24:52 | 只看该作者
全局:
最佳买卖股票时机含冷冻期  解析

一样的解题模板
做法与上一题  买卖股票的最佳时机 II 完全一样
解析也一样
只不过为了处理冷冻期,购买只能根据`profit[i-2][0]`  
  1. class Solution {
  2.     public int maxProfit(int[] prices) {
  3.         int profit[][] = new int[prices.length][2];
  4.         int preProfit = 0;
  5.         profit[0][0] = 0;
  6.         profit[0][1] = -prices[0];
  7.         for(int i=1 ; i<prices.length; i++){
  8.             if(i==1){   //避免profit[i-2][0]中i-2数组越界,把i=1从循环中提出来
  9.                 preProfit=0;
  10.             }else{
  11.                 preProfit=profit[i-2][0];
  12.             }
  13.             profit[i][0] = Math.max(profit[i-1][0], profit[i-1][1]+prices[i]);
  14.             profit[i][1] = Math.max(profit[i-1][1], preProfit-prices[i]);
  15.         }
  16.         return profit[prices.length-1][0];

  17.     }
  18. }
复制代码



同理降低空间复杂度
  1. class Solution {
  2.     public int maxProfit(int[] prices) {
  3.         int profit_0 = 0, profit_1 = -prices[0], preProfit_0 = 0;   //preProfit_0是profit[i-2][0]
  4.         for(int i=1 ; i<prices.length; i++){
  5.             int temp = profit_0;
  6.             profit_0 = Math.max(profit_0,profit_1+prices[i]);
  7.             profit_1 = Math.max(profit_1,preProfit_0-prices[i]);
  8.             preProfit_0 = temp;
  9.         }
  10.         return profit_0;
  11.     }
  12. }
复制代码



回复

使用道具 举报

🔗
 楼主| YankeeDoodle 2021-7-29 09:44:02 | 只看该作者
全局:
买卖股票的最佳时机含手续费

给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
示例 1:
输入:prices = [1, 3, 2, 8, 4, 9], fee = 2
输出:8
解释:能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8

示例 2:
输入:prices = [1,3,7,5,10,3], fee = 3
输出:6




回复

使用道具 举报

🔗
 楼主| YankeeDoodle 2021-7-30 09:52:19 | 只看该作者
全局:
买卖股票的最佳时机含手续费 解析

与题目 买卖股票的最佳时机 II 一样
解析也一样
区别在于多了个fee,在卖出股票时减去就行了
即:`profit[i][0] = Math.max(profit[i-1][0], profit[i-1][1] + prices[i] - fee);`


class Solution {
    public int maxProfit(int[] prices, int fee) {
        int profit[][] = new int[prices.length][2];
        profit[0][0] = 0;
        profit[0][1] = -prices[0];
        for(int i=1 ; i<prices.length; i++){
            profit[i][0] = Math.max(profit[i-1][0], profit[i-1][1] + prices[i] - fee);
            profit[i][1] = Math.max(profit[i-1][1], profit[i-1][0] - prices[i]);
        }
        return profit[prices.length-1][0];
    }
}


同理降低空间复杂度

class Solution {
    public int maxProfit(int[] prices, int fee) {
        int profit_0 = 0, profit_1 = -prices[0], temp = 0;
        for(int i = 0; i<prices.length; i++){
            temp = profit_0;
            profit_0 = Math.max(profit_0, profit_1 + prices[i] - fee);
            profit_1 = Math.max(profit_1, temp - prices[i]);
            temp = profit_0;
        }
        return profit_0;
    }
}






回复

使用道具 举报

🔗
 楼主| YankeeDoodle 2021-7-31 09:11:04 | 只看该作者
全局:
买卖股票的最佳时机 IV


给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。


示例 1:
输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

示例 2:
输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
     随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。











回复

使用道具 举报

🔗
 楼主| YankeeDoodle 2021-8-2 09:21:47 | 只看该作者
全局:

买卖股票的最佳时机 IV  解析

做法与买卖股票的最佳时机 II基本相同
但是这里多了一种状态,即最多完成k笔交易
那么需要枚举出完成几笔交易的状态
仍然利用遍历所有情况的思想,将交易次数为0到k的所有情况遍历出来


class Solution {
    public int maxProfit(int k, int[] prices) {
        if(prices.length==0) return 0;
        if(k>prices.length/2) return maxProfit(prices.length/2, prices);    //最多进行prices.length/2次交易
        int profit[][][] = new int[prices.length][k+1][2];
        for(int i=0; i<prices.length; i++){
            profit[i][0][0] = 0;
        }
        for(int i=0; i<=k; i++){
            profit[0][i][0] = 0;
            profit[0][i][1] = -prices[0];
        }
        for(int i = 1; i<prices.length; i++){
            for(int j = 1; j<=k; j++){  //买入算一次交易,卖出不算
                profit[i][j][0] = Math.max(profit[i-1][j][0], profit[i-1][j][1]+prices[i]);
                profit[i][j][1] = Math.max(profit[i-1][j][1], profit[i-1][j-1][0]-prices[i]);
            }
        }
        return profit[prices.length-1][k][0];
    }
}





回复

使用道具 举报

🔗
clark.li86 2021-8-4 09:35:46 | 只看该作者
全局:
股票买卖也算经典套餐了
回复

使用道具 举报

全局:
YankeeDoodle 发表于 2021-08-01 18:21:47
买卖股票的最佳时机 IV  解析

做法与买卖股票的最佳时机 II基本相同
很受益!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表