每日一道Leetcode算法—— Best Time to Buy and Sell Stock——2019.01.23

中文:

假设您有一个数组,其中第i个元素是第i天给定股票的价格。 如果您只被允许完成最多一笔交易(即买入并卖出一股股票),请设计一个算法以找到最大利润。 请注意,在购买之前不能出售股票。

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: 在第二天购买,价格为1,并且在第五天卖出,价格为6,最大利润为6-1=5

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: 在这种情况下,不进行任何交易,即最大利润= 0。

英文:

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:

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:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

实现思路:根据题意,即找出数组中两个数差为最大值作为结果并返回,但是大的数值要在小的数值的右边。

方法一:

循环数组,相当于一个指针指向一个位置,第二个指针在第一个指针右侧的位置,比较大小,用第二个指针的值,减去第一个指针的值,并将结果赋值给最大利润值。

  /**                                                                                    
   * 最大利润                                                                                
   * @param prices 股票价格数组                                                                
   * @return 最大利润                                                                        
   */                                                                                    
  public static int maxProfit1(int[] prices) {                                           
      //设定初始最大利润                                                                         
      int maxProfit = 0;                                                                 
      //循环数组,一个相当于一个指针指向一个位置,第二个指针在其右侧的位置,比较大小,赋值给最大利润值                                  
      for (int i = 0; i < prices.length; i++) {                                          
          for (int j = i + 1; j < prices.length; j++) {                                  
              //大于最大利润,则修改最大利润的值                                                         
              if (prices[j] - prices[i] > maxProfit) {                                   
                  maxProfit = prices[j] - prices[i];                                     
              }                                                                          
          }                                                                              
      }                                                                                  
      return maxProfit;                                                                  
  }                                                                                      

方法二:

其实就是在方法一的基础上进行了优化,先找出数组中的最大值和其所在位置,最小值和其所在位置,可能的最大利润为最大值-最小值。如果最小值刚好在最大值的左边位置,则最大利润为刚才算的可能的最大利润。

如果最小值不在最大值的左边,则使用方法一的办法再次进行循环,当算出的最大值=可能的最大值-1时,该数组中不会有更大的利润值了,直接终止,减少后续的循环消耗。

package cn.leetcode.easy;


/**
 * 假设您有一个数组,其中第i个元素是第i天给定股票的价格。
 * 如果您只被允许完成最多一笔交易(即买入并卖出一股股票),请设计一个算法以找到最大利润。
 * 请注意,在购买之前不能出售股票。
 *
 * @author kimtian
 */
public class MaxProfit {
    /**
     * 最大利润
     *
     * @param prices 股票价格数组
     * @return 最大利润
     */
    public static int maxProfit2(int[] prices) {
        //设定初始最大利润
        int maxProfit = 0;
        //初始化数组的最大值
        int maxValue = Integer.MIN_VALUE;
        //初始化数组最大值的索引
        int maxIndex = 0;
        //初始化数组的最小值
        int minValue = Integer.MAX_VALUE;
        //初始化数组最小值的索引
        int minIndex = 0;
        for (int i = 0; i < prices.length; i++) {
            //获取数组的最大值
            if (prices[i] > maxValue) {
                maxValue = prices[i];
                maxIndex = i;
            }
            //获取数组的最小值
            if (prices[i] < minValue) {
                minValue = prices[i];
                minIndex = i;
            }
        }
        //可能的最大利润
        int possibleMaxProfit = maxValue - minValue;
        //如果最小值索引位置比最大值索引位置值小
        if (minIndex < maxIndex) {
            //则最大利润为可能的最大利润
            return possibleMaxProfit;
        }
        //循环数组,一个相当于一个指针指向一个位置,第二个指针在其右侧的位置,比较大小,赋值给最大利润值
        for (int i = 0; i < prices.length; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                //大于最大利润,则修改最大利润的值
                if (prices[j] - prices[i] > maxProfit) {
                    maxProfit = prices[j] - prices[i];
                    //为了减少循环次数,如果最小值索引不在最大值索引的左边,最大利润只可能为可能的最大利润-1
                    if (maxProfit >= possibleMaxProfit - 1) {
                        break;
                    }
                }
            }
        }
        return maxProfit;
    }

    public static void main(String[] args) {
        int[] a = new int[]{7, 6, 4, 3, 1};
        System.out.println(maxProfit2(a));
    }

}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值