-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
added a problem on kadane's algo and its solution. #8569
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
Changes from 3 commits
6209eb7
767b1d0
a5ea4b2
28b4406
71ceff4
0a6d492
0d4a9aa
b841831
88aeeba
caea207
4f63025
39ac3cb
ea6a16c
45e5d97
9e3d158
5fa1095
12eb7e4
dc92eb7
58df091
889163b
3fc72c3
87880bb
5437ee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||||||||||
def max_product_subarray(nums: list[int]) -> int: | ||||||||||||||||||||
""" | ||||||||||||||||||||
Returns the maximum product that can be obtained by multiplying a | ||||||||||||||||||||
contiguous subarray of the given integer list `nums`. | ||||||||||||||||||||
|
||||||||||||||||||||
Example: | ||||||||||||||||||||
>>> max_product_subarray([2, 3, -2, 4]) | ||||||||||||||||||||
6 | ||||||||||||||||||||
>>> max_product_subarray([-2, 0, -1]) | ||||||||||||||||||||
0 | ||||||||||||||||||||
>>> max_product_subarray([2, 3, -2, 4, -1]) | ||||||||||||||||||||
48 | ||||||||||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
""" | ||||||||||||||||||||
n = len(nums) | ||||||||||||||||||||
|
||||||||||||||||||||
if n == 0: | ||||||||||||||||||||
return 0 | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'n' is the length of nums list , so it will always be a number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not need it until line 23 so let's not run the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's an edge case, so we need to check it first.. rather than going to next step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can check it in two lines not three and without a function call. |
||||||||||||||||||||
|
||||||||||||||||||||
max_till_now = nums[0] | ||||||||||||||||||||
min_till_now = nums[0] | ||||||||||||||||||||
max_prod = nums[0] | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
for i in range(1, n): | ||||||||||||||||||||
rohan472000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
# update the maximum and minimum subarray products | ||||||||||||||||||||
if nums[i] < 0: | ||||||||||||||||||||
max_till_now, min_till_now = min_till_now, max_till_now | ||||||||||||||||||||
max_till_now = max(nums[i], max_till_now * nums[i]) | ||||||||||||||||||||
min_till_now = min(nums[i], min_till_now * nums[i]) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
# update the maximum product found till now | ||||||||||||||||||||
max_prod = max(max_prod, max_till_now) | ||||||||||||||||||||
|
||||||||||||||||||||
return max_prod |
Uh oh!
There was an error while loading. Please reload this page.