Welcome to Incels.is - Involuntary Celibate Forum

Welcome! This is a forum for involuntary celibates: people who lack a significant other. Are you lonely and wish you had someone in your life? You're not alone! Join our forum and talk to people just like you.

Serious Can you solve this problem

cripplecel

cripplecel

Lord of HungER
★★★★★
Joined
Jun 20, 2022
Posts
19,434
You are given an array, prices where prices 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.

Example 1:

Input:
prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input:
prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
 
What's the question
Create a function to solve this problem

Like this
1745028349856
 
@trying to ascend alt?
 
Do you own programming homework, boyo.
 
Good. Let me guess, binary search?
No need for that

Lowest price = max int value
Max profit = 0

Just need to iterate on the array.

On each iteration:
profit = current price - lowest price
If ( profit > max profit ) then max profit = profit
If (current price < lowest price) then lowest price = current price

Return max profit
 
Last edited:
No need for that

Lowest price = maxint
Max profit = 0

Just need to iterate on the array.

On each iteration:
profit = lowest price - current item
If ( profit > max profit ) then max profit = profit
If (current price < lowest price) then lowest price = current price

Return max profit
Fucking arrays in leet code problems. I've always hated this shit. JFL
 
Fucking arrays in leet code problems. I've always hated this shit. JFL
Gotta know a bunch of shit like sliding window, two pointer, hashmap, hashset for these array / string problems.

This problem was solved with the two pointer technique.
 
You ain't getting no job by solving high school questions boyo.
 
What is the Compromise of 1850?
 
Most people will struggle with easy level questions initially.

when I first did this I did a fully retarded solution.
My first thought was to keep splitting the array and look for i and j where i is at the lowest and j is at the highest for every split. Then if i < j, then it's just max(j-i).

But that didn't work.
 
Tbf I had a stroke reading the first sentence.
It's not so bad, if you know some programming, tbh, but I could see how it would be confusing.
 

Similar threads

VλREN
Replies
21
Views
735
Emba
Emba
MaldireMan0077
Replies
1
Views
269
Ahnfeltia
Ahnfeltia
WeirdPanda
Replies
14
Views
429
glutty
glutty
SIR ETHNICCEL
Replies
32
Views
883
SIR ETHNICCEL
SIR ETHNICCEL

Users who are viewing this thread

shape1
shape2
shape3
shape4
shape5
shape6
Back
Top