Leetcode_num5_Best Time to Buy and Stock II, leetcode

Source: Internet
Author: User

Leetcode_num5_Best Time to Buy and Stock II, leetcode

Question:

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 your transactions as you like (ie, buy one and every one share of the stock multiple times ). however, you may not engage in multiple transactions at the same time (ie, you must wait the stock before you buy again ).

I have been busy from the weekend to work days, but I still cannot relax my questions ~~~ (I don't know how long I can stick to it, hahaha)

The difficulty of this question mainly lies in understanding the question and thinking transformation. Because of the stock background, the code is quite simple,... I still fell into the trap... Tears

SetBuying and selling are regarded as a roundTo maximize profits, you only need to meet the principles of low-price purchase and high-price sales.

For example, if prices = [3, 1, 4, 6, 5, 7]

Then, you can buy at 1, 4, 4, 6, 5, and 7.

In this way, total (4-1) + (6-4) + (7-5) = 7

The final code is as follows:

class Solution:    # @param prices, a list of integer    # @return an integer    def maxProfit(self, prices):        L=len(prices)        rs=0        for i in range(0,L-1):            if prices[i+1]>prices[i]:                rs+=(prices[i+1]-prices[i])        return rs





Translation of nothing to marketplace and nothing to buy

Do not sell or buy
He said that everyone lives next to selling something. According to this statement, the teacher lives next to selling knowledge, and the philosopher is comfortable by selling wisdom and priests. Although it may be possible to measure the value of a material item based on money, it is extremely difficult to estimate the real value of people serving us. Sometimes, when we are willing to give us everything that saves our lives, we may be excited to pay the high cost of surgeons to precisely provide us with this service. The condition in society is that such skills must be similarly paid for items that are paid in the store. Everyone has something to sell.

Wanderer seems to be the only exception to this general rule. Package almost sells itself as a person to arouse the grief of passers-. However, the real Wanderer is not a definition. They do not sell anything and demand anything from others. Looking for independence, they do not sacrifice their dignity. A wanderer may ask you for money, but he won't ask you to feel sorry for him. He deliberately chooses to lead his life and is completely conscious of the consequences. He may not be sure where the next meal comes from, but he is free to worry about suffering tens of thousands of others. His little wealth makes it possible for him to move around easily. By making it more public than most people must go to bed, he gets closer to nature and we do it. He may seek, beg, or occasionally steal to keep himself alive; he can even complete his work as needed; but he won't sacrifice the freedom that we often talk to in the same class, And the Wanderer is full of contempt and put into them, like shouting, but how much can we honestly say that we do not feel a little envious of their simple lifestyle and their liberation from concern?

Use logging ...... To and buy ...... At least three from statements

He sold two beautiful gifts to me.
Yesterday my mum bought two books from the library.
Do not specify these things to anyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.