LeetCode 122. Best Time to Buy and Sell Stock II

Problem:

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 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).

Solution:

Just when I thought this was a DP…….

public class Solution {
    int[] profit;
    
    public int maxProfit(int[] prices) {
        if (prices.length < 1) return 0;
        int max = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i-1]) max += prices[i] - prices[i-1];
        }
        return max;
    }
}

14 thoughts on “LeetCode 122. Best Time to Buy and Sell Stock II”

  1. ~100 預防高危致癌的 HPV 16、18 型號 (可減低 70 患子宮頸癌的風險) ~100 減低引致生殖器官濕疣 (俗稱「椰菜花」) 的 HPV 6、11 型的感染 (可減低超過 90 患生殖器官濕疣的風險) HPV4合1子宮頸癌疫苗 Gardasil HPV病毒會感染人類的皮膚及黏膜,一般會透過性接觸及親密的皮膚接觸而受到感染,是一種男性與女性都可能感染的常見病毒。可感染身體各個部位的HPV超過100種,當中有部份的HPV類型可影響生殖器部位,導致生殖器疣(genital warts) 、子宮頸細胞異常(abnormal cervical cells) ,甚至子宮頸癌 (cervical cancer)。 4合1 HPV 子宮頸癌疫苗,覆蓋4種高危HPV病毒:6、 11、16及18型(約70的子宮頸癌由HPV16和HPV18病毒引致),有助預防子宮頸癌、外陰癌、陰道癌及生殖器官濕疣

  2. FineScan 會在肌膚上製造數以千計的細小深入傷口,即所謂的顯微加熱區(microthermal zone),但要確保每次治療時皆有部份組織不受能量影響,於是,每一個顯微加熱區的作用雖然強烈而明顯,但周圍都包覆著正常且結構完整的皮膚組織,使傷口能在短時間內癒合,並替換之前有缺陷的受損組織。Finescan不僅可讓表皮新生,更可促進深層膠原再生,從內而外徹底喚醒細胞,瞬時找回年輕時的肌膚狀態。憑藉最新的雙軸技術,FINESCAN 6可治療 – 面部 – 頸部 – 暗瘡凹凸洞 – 增生性疤痕

  3. 部落客VERA x【4個月後效果更新!】拒當老娃娃!永遠年輕彈力童顏肌!CPT電波拉皮『凍齡記』 @ ~ 相信悠美 你會更美 ~ :: 痞客邦 :: 還記得年初(1/20)時,小編發過這篇日本藥妝失心瘋版主Vera到悠美施作的CPT電波拉皮『凍齡記』嗎! 一轉眼也過了快四個月了呢!立刻幫大家update一下Vera最近的童顏肌美照 &hea

  4. Yeezy Shoes http://www.yeezy.com.co/
    Yeezys http://www.yeezys.us.com/
    Yeezy Shoes http://www.yeezysupply.us.com/
    Yeezy Shoes http://www.yeezy-shoes.us.com/
    Yeezy Boost http://www.yeezy-boost350.com/
    Yeezy Boost 350 V2 http://www.yeezyboost350.us.com/
    Yeezy Blue Tint http://www.yeezybluetint.com/
    Yeezy 500 Utility Black http://www.yeezy500utilityblack.com/
    Adidas Yeezy 500 http://www.yeezy500utilityblack.us/
    Vapor Max http://www.vapor-max.org.uk/
    Salomon http://www.salomon-shoes.org.uk/
    Salomon http://www.salomons.me.uk/
    Salomon Shoes http://www.salomonspeedcross4.org.uk/
    Off White Air Jordan 1 http://www.offwhitejordan1.com/
    Nike Air VaporMax http://www.nikevapormax.org.uk/
    React Element 87 http://www.nikereactelement87.us.com/
    Nike React Element 87 http://www.nikereactelement87.us/
    Nike Air Vapormax Plus http://www.nikeplus.us/
    Nike Outlet http://www.nike–outlet.us/
    Nike Outlet http://www.nikeoutletstoreonlineshopping.us/
    Nike Outlet Store http://www.nikeoutletonlineshopping.us/
    Cheap Nike NBA Jerseys http://www.nikenbajerseys.us/
    Nike Air Max http://www.nikeairmax.us/
    Nike Air Max 2017 http://www.max2017.us/
    Jordan Shoes http://www.jordan-com.com/
    Jordan 11 Concord 2018 http://www.jordan11-concord.com/
    Cheap Yeezy Shoes http://www.cs7boots1.com/
    Cheap NBA Jerseys From China http://www.cheapnba-jerseys.us/
    Birkenstock Sandals UK http://www.birkenstocksandalsuk.me.uk/
    NBA Jerseys http://www.basketball-jersey.us/
    Balenciaga http://www.balenciaga.me.uk/
    Balenciaga http://www.balenciagauk.org.uk/
    Balenciaga http://www.balenciagatriples.org.uk/
    Balenciaga http://www.birkenstocks.me.uk/
    Balenciaga http://www.balenciagatrainers.org.uk/
    Nike Air Max 270 http://www.airmax270.org.uk/
    Yeezy Shoes http://www.adidasyeezyshoes.org.uk/
    Adidas Yeezy Shoes http://www.adidasyeezyshoes.org.uk/

Leave a Reply