前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NSum及股票系列

NSum及股票系列

原创
作者头像
王脸小
修改2020-08-04 10:46:58
5770
修改2020-08-04 10:46:58
举报
文章被收录于专栏:王漂亮

NSum系列

1. 2Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

代码语言:javascript
复制
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

1. 一边Dict

2. 排序,双指针,Onlogn

15. 3Sum

3Sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

代码语言:javascript
复制
Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

思路 排序+双指针,On2,固定一个i,l,r从左右开始扫描

18. 4Sum

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

代码语言:javascript
复制
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

Solution

和3Sum思路一样,固定两个再双指针,On3

股票系列

算法:买卖股票系列

代码语言:javascript
复制
// 第i天的没有股票的状态 = Max(前一天就没有,前一天有但今天卖出了) 
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]); 
// 第i天的有股票的状态 = Max(前一天就有了,前一天没有今天买了) 
profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);

121. 买卖股票的最佳时机

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

代码语言:javascript
复制
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
             Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

代码语言:javascript
复制
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Solution

维护最小值,不断更新最大收益

代码语言:javascript
复制
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices: return 0
        
        dp = [0]*len(prices)
        min_v = prices[0]
        
        for i in range(1, len(prices)):
            min_v = min(min_v, prices[i])
            dp[i] = prices[i] - min_v
            
        return max(dp)

122. 买卖股票的最佳时机 II

Say you have an array prices for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

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

Example 1:

代码语言:javascript
复制
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

代码语言:javascript
复制
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

Solution

只要后一天比前一天大,就交易

代码语言:javascript
复制
ab

123. 买卖股票的最佳时机 III

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

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

Example 1:

代码语言:javascript
复制
Input: [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.

Example 2:

代码语言:javascript
复制
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

Solution

dp[i][s],i为天数,s为状态

代码语言:javascript
复制
ab

188. 买卖股票的最佳时机 IV

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example 1:

代码语言:javascript
复制
Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

代码语言:javascript
复制
Input: [3,2,6,5,0,3], k = 2
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
             Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

Solution

dp[i][s],i为天数,s为状态

代码语言:javascript
复制
ab

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NSum系列
    • 1. 2Sum
      • 15. 3Sum
        • 18. 4Sum
        • 股票系列
          • 121. 买卖股票的最佳时机
            • 122. 买卖股票的最佳时机 II
              • 123. 买卖股票的最佳时机 III
                • 188. 买卖股票的最佳时机 IV
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档