Giter Site home page Giter Site logo

leetcode-121.besttimetobuyandsellstock's Introduction

LeetCode-121.BestTimetoBuyandSellStock

LeetCode Problem Number 121

My solution for a problem in LeetCode called Best Time to Buy and Sell Stock, original link:

https://leetcode.com/problems/best-time-to-buy-and-sell-stock

Problem:

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

My Results:

211 / 211 test cases passed.
Status: Accepted
Runtime: 444 ms
Memory Usage: 44.4 MB

image

This problem was harder than I expected, and took more hours than expected to solve. And I think my solution was not good, in this solution I used a while loop but earlier I solved with 2 for loops, which I think was better but in some tests I was getting "Time Limit Exceeded", so was not performatic at all. So I switched to while and used a different logic, following a tip in a Youtube video, but in the end my runtime was not so good. But at least I solved, I certainly can improve this code in the future.

This is part of Top Interview Questions curated list, from Leetcode:

https://leetcode.com/explore/interview/card/top-interview-questions-easy/

leetcode-121.besttimetobuyandsellstock's People

Contributors

wbalbo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.