• best-time-to-buy-and-sell-stock-ii


    /**
    * @author gentleKay
    * 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 as many transactions as you like
    * (ie, buy one and sell one share of the stock multiple times).
    * However, you may not engage in multiple transactions at the same time
    * (ie, you must sell the stock before you buy again).
    *
    * 假设您有一个数组,其中第i个元素是第一天给定股票的价格。
    * 设计了一种求最大利润的算法。您可以完成任意多的交易
    * (即,购买一份股票并多次出售一份股票)。
    * 但是,您不能同时进行多个交易
    * (即,您必须在再次购买之前出售股票)。
    */

    /**
     * 
     * @author gentleKay
     * 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 as many transactions as you like 
     * (ie, buy one and sell one share of the stock multiple times). 
     * However, you may not engage in multiple transactions at the same time 
     * (ie, you must sell the stock before you buy again).
     * 
     * 假设您有一个数组,其中第i个元素是第一天给定股票的价格。
     * 设计了一种求最大利润的算法。您可以完成任意多的交易
     * (即,购买一份股票并多次出售一份股票)。
     * 但是,您不能同时进行多个交易
     * (即,您必须在再次购买之前出售股票)。
     */
    
    public class Main06 {
    
    	public static void main(String[] args) {
    		int[] prices = {1,4,2};
    		System.out.println(Main06.maxProfit(prices));
    	}
    	
    	public static int maxProfit(int[] prices) {
    		if (prices == null || prices.length < 2) {
    			return 0;
    		}
    		
    		int num = 0;
    		for (int i=1;i<prices.length;i++) {
    			if (prices[i] > prices[i-1]) {
    				num = num + prices[i] - prices[i-1];
    			}
    		}
            return num;
        }
    }
    

      

  • 相关阅读:
    白盒测试笔记之:Junit 单元测试以及测试覆盖率
    Bootstrap+JSP实例学习笔记一.简单的带登录功能的首页
    一位普通测试人2018年简单回顾
    Web API学习笔记(Python实现)
    web安全测试--sql注入攻击
    电源分配系统及电源完整性
    FPGA Timing笔记
    使用arm开发板搭建无线mesh网络(二)
    使用arm开发板搭建无线mesh网络(一)
    arm tiny6410双网卡桥接问题
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11236536.html
Copyright © 2020-2023  润新知