leetcode121

public class Solution {
    public int MaxProfit(int[] prices)
        {
            //寻找最优极值点(包括2个端点)

            if (prices.Length < 2)
            {
                return 0;
            }
            else if (prices.Length == 2)
            {
                return prices[1] - prices[0] > 0 ? prices[1] - prices[0] : 0;
            }
            else//至少3个点
            {
                //先对原始数据进行压缩
                var list = new List<int>();
                for (int i = 0; i < prices.Length - 1; i++)
                {
                    if (prices[i] != prices[i + 1])
                    {
                        list.Add(prices[i]);
                    }
                }
                var last = list.LastOrDefault();
                if (last != prices[prices.Length - 1])
                {
                    list.Add(prices[prices.Length - 1]);
                }

                var dic = new Dictionary<int, int>();//记录所有极值点极其类型
                //Key是index,Value是类型:-1是极小,1是极大

                //list已经压缩,没有平行点了
                for (int i = 1; i < list.Count - 1; i++)
                {
                    //判断是否是极大值点
                    if (list[i - 1] < list[i] && list[i] > list[i + 1])
                    {
                        dic.Add(i, 1);
                    }
                    else if (list[i - 1] > list[i] && list[i] < list[i + 1])
                    {
                        dic.Add(i, -1);
                    }
                    //判断是否是极小值点
                }

                //处理端点
                if (dic.Count == 0)
                {
                    return list[list.Count - 1] - list[0] > 0 ? list[list.Count - 1] - list[0] : 0;
                }
                else
                {
                    var list2 = dic.OrderBy(x => x.Key).ToList();
                    var d1 = list2.FirstOrDefault();
                    var d2 = list2.LastOrDefault();
                    if (d1.Value == 1)
                    {
                        list2.Add(new KeyValuePair<int, int>(0, -1));
                    }
                    else
                    {
                        list2.Add(new KeyValuePair<int, int>(0, 1));
                    }

                    if (d2.Value == 1)
                    {
                        list2.Add(new KeyValuePair<int, int>(list.Count - 1, -1));
                    }
                    else
                    {
                        list2.Add(new KeyValuePair<int, int>(list.Count - 1, 1));
                    }

                    list2 = list2.OrderBy(x => x.Key).ToList();//得到全部的极值点

                    var maxProfit = 0;

                    for (int i = 0; i < list2.Count; i++)
                    {
                        if (list2[i].Value == -1)
                        {
                            for (int j = i; j < list2.Count; j++)
                            {
                                if (list2[j].Value == 1)
                                {
                                    if (list[list2[j].Key] - list[list2[i].Key] > maxProfit)
                                    {
                                        maxProfit = list[list2[j].Key] - list[list2[i].Key];
                                    }
                                }
                            }
                        }
                    }
                    return maxProfit;
                }

            }
        }
}

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description

时间: 2024-10-30 23:02:03

leetcode121的相关文章

LeetCode121:Best Time to Buy and Sell Stock

题目: 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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

LeetCode121/122/123 Best Time to Buy and Sell Stock&lt;股票&gt; I/II/III----DP+Greedy**

一:LeetCode 121 Best Time to Buy and Sell Stock 题目: 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 (ie, buy one and sell one share of the stock), d

leetcode 股票问题

leetcode 股票问题 参考:labuladong (leetcode-121) 给定一个数组,它的第?i 个元素是一支给定股票第 i 天的价格.如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润.注意你不能在买入股票前卖出股票. class Solution: def maxProfit(self, prices: List[int]) -> int: minp = float('inf') maxpr = float('-inf') if not