【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

题目

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

又是时间复杂度,离真相就差一行代码。。。还是没想出来,看了discuss才知道答案。

代码

public class Solution {
    public int maxProfit(int[] prices) {

        if (prices.length<=0 || prices.length==1) return 0;
    	   int maxProfit=0;
    	   int minTemp=prices[0];
    	   for(int i=1;i<prices.length;i++){
    		   if(prices[i]<minTemp){
    			   minTemp=prices[i];
    		   }
    		   else{
    			   if(prices[i]-minTemp>maxProfit){
    				   maxProfit=prices[i]-minTemp;
    			   }
    		   }
    	   }
    	   return maxProfit;
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-09-15 04:56:12

【LeetCode从零单排】No121 Best Time to Buy and Sell Stock的相关文章

[LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee. You may complete as many transactions as you like, but you need to pay the t

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security

【LeetCode从零单排】No70.ClimbingStairs

题目           爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这道题一共有三个方法实现(我想到的): 1.递归(对应代码climbStairs) 如果楼梯有

【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

题目      题目要求:去除sort int数组中的重复项.      Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory

【LeetCode从零单排】No58.Length of Last Word

题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

【LeetCode从零单排】No.9 Palindrome Number

题目       这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints:Could negative integers be palindromes? (ie, -1) If you are

【LeetCode从零单排】No.135Candy(双向动态规划)

1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路.记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索.等把这一套盒子都打开完,再打开第二套的.Wikipedia上的讲解是:"Depth

【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路:利用递归找到倒数第一个父节点,记录下它的右节点,将左边的移到右边,然后再把之前标记的右节点连接上. 代码 public class Solution { public