📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 695| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 为啥类似?lc 689 3连续k的subarray最大和 和 lc 123 买卖股票3

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
为啥类似?lc 689 3连续k的subarray最大和 和 lc 123 买卖股票3
虽然都可以动态规划,我有点没整明白为啥相似。求大佬指教:
689:
Given an integer array nums and an integer k, find three non-overlapping subarrays of length k with maximum sum and return them.

Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.



Example 1:

Input: nums = [1,2,1,2,6,7,5,1], k = 2
Output: [0,3,5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.


123:
You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).



Example 1:

Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

评分

参与人数 1大米 +1 收起 理由
14417335 + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分


上一篇:刷题900打个卡
下一篇:有没有什么练习follow up问题的资源哇
🔗
 楼主| hanyue04 2022-10-27 21:57:37 | 只看该作者
全局:
其实动态规划也不是很难,做个几百道也就说不出难了。。。。
回复

使用道具 举报

全局:
本帖最后由 马保国骑马保国 于 2022-10-27 11:54 编辑

  • 689 的nums 生成一个 subsumsArray,在这个里面就是以i为开头长度为k的sub sum, 类似于i天购买stock的价值
  • 689 non overlapping, 比如选择了 subsumsArray, 那么下一个必须从subsumsArray[i + k] 开始,类似于不能同时有两笔交易,更准确的应该对应 stock购买的冷却时间
  • 689 3 个 subarray 相当于 stock 需要交易 3次

评分

参与人数 1大米 +1 收起 理由
hanyue04 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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