Skip to content

Arrays : Best time to buy and sellstocks #77

@SharedMocha

Description

@SharedMocha

//You will be given price of ELA on multiple days in a month(assume array index as days).
//Now tell me the total profit i could have made if i buy and sell it
//There are 2 types of buyers -LONG HOLD or SHORT HOLD(BUY,SELL,MAKE PROMIT)- Here we are //intrested in both

//VVIMP ->>>>>>>>>LOGIC IS
//KEEP ADDING -> All profits you made by --> subtracting (highest money -lowest money)

/////////https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/564/discuss/134165/Simple-java-solution

/*
I got so confused on this question. Essential what you need to do is just adding the difference of adjascent numbers if the next number is greater than the first one. I throught I can't do buy and sell in one day. It's so confused.
*/
class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
for(int i=1;i<=prices.length-1;i++){
//
if(prices[i] > prices[i-1]){
sum = sum + (prices[i] - prices[i-1]);
}
}

    return sum;
    
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions