找LeetCode上简单的题目来撑过30天啦(DAY7)

昨天熬夜看小说,今天早上爬不起来就翘班(开玩笑的,是请假/images/emoticon/emoticon11.gif
今天赶快发一发文我要来去补眠

今天的题目↓

题目:122 标题:Best Time to Buy and Sell Stock II 难度:Medium

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.

Example 1:
Input: prices = [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.
Total profit is 4 + 3 = 7.


Example 2:
Input: prices = [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.
Total profit is 4.


Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

Constraints:
• 1 <= prices.length <= 3 * 104
• 0 <= prices[i] <= 104


我的程序码

int maxProfit(int* prices, int pricesSize){
    int i,mix,max,result=0,flagt=0,flagm=0;
    mix = 10001;
    max = -1;
    for(i=0;i<pricesSize;i++){
        if(flagt== 0){
            if(*prices < mix){
                mix = *prices;
            }else if(*prices > mix){
                max = *prices;
                flagt = 1;
                if(i == pricesSize -1){
                    result = result +  max -mix;
                }
            }
        }else if(flagm == 0){
            if(i == pricesSize -1){
                if(*prices>max){
                    result = result +  *prices -mix;
                }else{
                    result = result + max - mix;
                }
            }else{
                if(max > *prices){
                    result = result + max - mix;
                    mix = *prices;
                    flagt = 0;
                    max = -1; 
                }else{
                    max = *prices;
                } 
            }
        }
        *prices++;
     }
    return result;
}

花比较多时间的地方
看题目,我英文是真滴不好,一开始误会题目的意思,这题至少改了三次(题意理解了三次才终於懂了)这就是看测资推题目的缺点阿

DAY7心得
写题目有一部份是想试看看之前没用过的东西或是逻辑,所以我偏好用跟自己以往习惯不同的方式来解题,这题就不用a[i]的写法,改成 *a 希望30天内有机会可以多了解一些资料结构跟function


<<:  Day10

>>:  【设计+切版30天实作】|Day8 - 看起来简单但不单调的steps设计

Day04 如何通讯-网路协商

WebRTC 通讯 WebRTC 最常见的应用场景就是一对一的视讯通话,当我们准备和另一端的人进行点...

Day30. 8人订阅Q&A问答

前情提要:Day1. 前言(动机与简介) 动机 重新整理在blog中的相关内容。 与大家讨论学习。...

[番外] 一步一步实现购物车功能 [续]

状态管理 建立一个空间来储存应用程序的 store state store 资料夹 放在 src 下...

SEO网站优化技巧,为网站创建友好的图像,加快网站读取速度

你知道优化图像会增加你的网站的SEO排名吗?了解Web的图像优化如何提升网站速度有效提升在网站上的排...

Android学习笔记02

DataBinding简易的单项及双向绑定 使用DataBinding之前要先在DataBingin...