leetcode-每日打卡-day 2

leetcode 每日打卡

附上kuangbin大佬话激励自己:
人一我百,人十我万,追逐青春的梦想,怀着自信的心,永不放弃!
2020.2.9


记录下来自己做题时得思路,并不一定是最优解

#944. 删列造序

class Solution {
public:
    int minDeletionSize(vector<string>& A) {
        int ans = 0;
        for(int i = 0;i < A[0].size();i ++)
        {
            for(int j = 0;j < A.size() - 1;j++)
            {
                if(A[j][i] > A[j + 1][i])
                {
                    ans++;break;
                }
            }
        }
        return ans;
    }
};

#1217. 玩筹码

class Solution {
public:
    int minCostToMoveChips(vector<int>& chips) {
        int j = 0, o = 0;
        for(int i = 0;i < chips.size();i ++)
        {
            if(chips[i] % 2 == 0)o++;
            else j++;
        }
        return min(j,o);
    }
};

#714. 买卖股票的最佳时机含手续费

动态规划 在第 i 天我们需要根据第 i + 1 天的状态来更新cash 或者 hold 的值
对于 cash 我们可以保持不变,或者讲手上的股票卖出
即:

cash = max(cash, hold + price[i] - fee);

对于 hold,我们可以保持不变,或者买入这一天的股票:

hold = max(hold,cash - prices[i]);
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int cash = 0,hold = -prices[0];
        for(int i = 0;i < prices.size();i ++)
        {
            cash = max(cash,hold + prices[i] - fee);
            hold = max(hold,cash - prices[i]);
        }
        return cash;
    }
};

测试样例

[1,3,2,8,4,9] 2

cash : 0  0  0  5  5  8
hold :-1 -1 -1 -1  1  1

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12289182.html