Skip to content

Latest commit

 

History

History
40 lines (24 loc) · 1.97 KB

贪心思想.md

File metadata and controls

40 lines (24 loc) · 1.97 KB

贪心思想

  • 121.Best Time to Buy and Sell Stock (only permitted to complete at most one transaction)

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.

leetcode

    1. Best Time to Buy and Sell Stock II

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

这两题都是股票卖出,求最大收益,但是设定的条件不一样,第一题是只准出售一次,二后一题则是可以多次出售。

leetcode

    1. Can Place Flowers Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

找出最大能终止的鲜花数,跟输入值比较,注意边界收尾两个值即可。 leetcode

    1. 跳跃游戏
给定一个非负整数数组,您最初位于数组的第一个索引处。

数组中的每个元素表示您在该位置的最大跳跃长度。

确定是否能够到达最后一个索引。

示例:
A = [2,3,1,1,4],返回 true。

A = [3,2,1,0,4],返回 false。