-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
added solution & modified README.md for problem 121 #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
leetcode/src/121.c
Outdated
| 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. |
There was a problem hiding this comment.
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?
leetcode/src/121.c
Outdated
| return 0; | ||
| } | ||
| int min_element = prices[0]; | ||
| int max_difference = prices[1] - min_element; |
There was a problem hiding this comment.
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.
|
Hi @hrishikeshSuresh . Could you add my 2nd way to resolve this problem? My way uses 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;
} |
#249