wbalbo / LeetCode-121.BestTimetoBuyandSellStock

LeetCode Problem Number 121

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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/

About

LeetCode Problem Number 121


Languages

Language:C# 100.0%