算法题解:买卖股票的最好时机

题目描述

假设你有一个数组,其中第\ i i 个元素是股票在第\ i i 天的价格。
你有一次买入和卖出的机会。(只有买入了股票以后才能卖出)。请你设计一个算法来计算可以获得的最大收益。

示例1
输入
[1,4,2]

返回值
3

示例2
输入
[2,4,1]

返回值
2

代码实现

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        int maxProfit = 0;  //获得的最大收益
        int countProfit = 0; //能取得的收益
        for(int i=1;i<prices.length;i++){
            if(countProfit < 0 ){              //如果模拟计算的之前收益为负,则从当前开始买卖计算收益
                countProfit = prices[i]-prices[i-1];
            }else{                       //如果模拟计算的之前有收益,则以之前收益和当前收益为总收益
                countProfit=countProfit+prices[i]-prices[i-1];
            }
            if(countProfit > maxProfit){
                maxProfit = countProfit;
            }
        }
        return maxProfit;
    }
}

解题思路

这个问题的解决采用动态规划的方法。

这个问题总结起来就是要连续几天的收益最大,可以理解每天都有一个收益,每天收益为正时,表示有收益,在增长,当某天收益为负时,表示在减少,我们要在收益为正累加到最大,在收益为负前结束。取收益累加最大的一次作为我们的答案。

countProfit[i]表示到i天时候卖出(天数从0开始编号)所能取得的最大收益,所以第i-1天必须持有(当天买当天卖的情况就是0,编程时将答案maxProfit初始化为0就不需要考虑当天买卖了)。那么就是看是第i-1天之前买入的,还是第i-1天买入的了,即

 

也就是说,当countProfit[i-1]为负的时候,就直接取这一天的"收益"就行了(当然这一天的"收益"也可能是负的,只是不必考虑当天买当天卖的情况了,如前面所解释的只要将maxProfit最开始设置为0就行了)。

另外,因为countProfit[i]只可能使用了上一时刻的countProvfit[i-1],而且maxProfit可以在countProvfit生成过程中算出来,所以没必要把countProvfit开成数组了,直接用一个int countProvfit就完事了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值