首页 > 编程语言 >LeetCode算法笔记 121. 买卖股票的最佳时机

LeetCode算法笔记 121. 买卖股票的最佳时机

时间:2022-10-11 23:56:24浏览次数:55  
标签:prices int max System 121 最佳时机 println LeetCode out

import junit.framework.TestCase;


public class LeetCode03_2 extends TestCase {


    /**
     * 121. 买卖股票的最佳时机
     * 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
     * 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
     * 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
     * <p>
     * 示例 1:
     * 输入:[7,1,5,3,6,4]
     * 输出:5
     * 解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     * 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
     * <p>
     * 示例 2:
     * 输入:prices = [7,6,4,3,1]
     * 输出:0
     * 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
     * <p>
     * 提示:
     * 1 <= prices.length <= 105
     * 0 <= prices[i] <= 104
     */
    
    /**
     * 方法一:暴力法
     * 时间复杂度:O(n^2)。循环运行 n(n−1)/2次。
     * 空间复杂度:O(1)O(1)。只使用了常数个变量。
     */
    public int maxProfit(int[] prices) {
        int max = 0;
        for (int i = 0; i < prices.length; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                int profit = prices[j] - prices[i];
                if (profit > max) {
                    max = profit;
                }
            }
        }
        return max;
    }

    /**
     * 方法二:一次遍历
     * 我们只需要遍历价格数组一遍,记录当前时间点前历史最低点
     * 
     * 时间复杂度:O(n),只需要遍历一次。
     * 空间复杂度:O(1),只使用了常数个变量。
     */
    public int maxProfit2(int[] prices) {
        int max = 0;
        int minPrice = Integer.MAX_VALUE;
        for (int price : prices) {
            if (price < minPrice) {
                minPrice = price;
            } else if (price - minPrice > max) {
                max = price - minPrice;
            }
        }
        return max;
    }

    public void test() {
        int[] nums1 = {7, 1, 5, 3, 6, 4};
        int[] nums2 = {7, 6, 4, 3, 1};
        System.out.println(maxProfit(nums1));
        System.out.println(maxProfit(nums2));
        System.out.println("==============");
        System.out.println(maxProfit2(nums1));
        System.out.println(maxProfit2(nums2));
    }
}

  

标签:prices,int,max,System,121,最佳时机,println,LeetCode,out
From: https://www.cnblogs.com/sueyyyy/p/16783063.html

相关文章

  • leetcode-67-easy
    AddBinary思路一:先计算公共部分,最后补充未计算的位置,模拟二进制加法,写的太丑了publicStringaddBinary(Stringa,Stringb){charONE='0'+'1';char......
  • LeetCode148. Sort List
    题意链表排序方法递归代码classSolution{public:ListNode*sortList(ListNode*head){returnsortList(head,nullptr);}ListNode*......
  • leetcode-26-easy
    RemoveDuplicatesfromSortedArray思路一:双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指......
  • leetcode-58-easy
    LengthofLastWord思路一:从后面非空格字符开始扫描,记录非空格字符个数。优化:不用char[],直接用charAt()判断publicintlengthOfLastWord(Strings){......
  • leetcode-66-easy
    PlusOne思路一:暴力,方向想错了,不能把digits当做一个整数看publicint[]plusOne(int[]digits){if(digits[digits.length-1]!=9){digits[digit......
  • #yyds干货盘点# LeetCode 热题 HOT 100:最小覆盖子串
    题目:给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串,则返回空字符串""。 注意:对于t中重复字符,我们寻......
  • LeetCode88.合并两个数组
    1.题目描述给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2到nums1中,使合并后的......
  • LeetCode 1195. Fizz Buzz Multithreaded
    原题链接在这里:https://leetcode.com/problems/fizz-buzz-multithreaded/题目:Youhavethefourfunctions:printFizz thatprintstheword "fizz" totheconsole......
  • leetcode-394.字符串解码
    394.字符串解码publicStringdecodeString(Strings){Stack<Character>stack=newStack<>();for(charc:s.toCharArray()){if(c......
  • leetcode 785. Is Graph Bipartite判断二分图 (中等)
    一、题目大意存在一个无向图,图中有n个节点。其中每个节点都有一个介于0到n-1之间的唯一编号。给你一个二维数组graph,其中graph[u]是一个节点数组,由节点u......