Skip to content

Conversation

@hrishikeshSuresh
Copy link
Contributor

SaurusXI and others added 11 commits October 4, 2019 23:03
The for loop utilized in 283 was improperly structured as 'start'
was no declared as the value to index.

Also, make the other cases more readable.

Signed-off-by: RJ Trujillo <certifiedblyndguy@gmail.com>
Added commented solution to problem 461
leetcode: Address readability of a few cases, and fix 283
…sert-position

Feature/search insert position
Added commented solution to Leetcode problem 476
int min_element = prices[0];
int max_difference = prices[1] - min_element;
for(int i = 0; i < pricesSize; i++) {
/* whenever maximum profit can be made, we sell the stock.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix all indent?

return 0;
}
int min_element = prices[0];
int max_difference = prices[1] - min_element;
Copy link
Contributor

@danghai danghai Oct 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should initialize max_difference = 0 so that you do not need to check returning value. I mean:
int max_difference = 0. And then replace: return (max_difference < 0)? 0 : max_difference; by
return max_difference.

@danghai
Copy link
Contributor

danghai commented Oct 6, 2019

Hi @hrishikeshSuresh . Could you add my 2nd way to resolve this problem? My way uses Kadane algorithm that is an efficient way to find maximum sub-array. You can see problem 53.c for reference. Thank you

int maxcmp(int a, int b) {
    return a >= b ? a : b;
}
/* max subarray problem by using Kadane's Algorithm */
int maxProfit(int* prices, int pricesSize){
    /* maxCur: current maximum 
       maxSoFar: found maximum for subarray so far*/
    int maxCur = 0, maxSoFar = 0;
    for(int i = 1; i < pricesSize; i++) {
        maxCur = maxcmp(0, maxCur + prices[i] - prices[i - 1]);
        maxSoFar = maxcmp(maxSoFar, maxCur);
    }
    return maxSoFar;
}

@danghai danghai merged commit c069038 into TheAlgorithms:master Oct 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants