-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
All suggeted changes within additional time limit tests #1815
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
88e977a
With all suggested changes :white_check_mark:
itsvaibhav01 7e11335
Updated with both slow and faster algorithms
itsvaibhav01 ea8ad6b
removed the time comparision part!
itsvaibhav01 d73abb1
Update data_structures/stacks/next_greater_element.py
itsvaibhav01 f043b0f
Update data_structures/stacks/next_greater_element.py
itsvaibhav01 850ef0e
Update data_structures/stacks/next_greater_element.py
itsvaibhav01 39d93d0
Update data_structures/stacks/next_greater_element.py
itsvaibhav01 e767f9c
Add benchmark using timeit
cclauss 06a5f51
Optimize slow() to create fast() - Three algorithms in the race
cclauss b28b4ef
Use a bigger test array with floats, negatives, zero
cclauss b46513b
Setup import next_greatest_element_fast
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
def printNGE(arr): | ||
def next_greatest_element_slow(arr): | ||
""" | ||
Function to print element and Next Greatest Element (NGE) pair for all elements of list | ||
NGE - Maximum element present afterwards the current one which is also greater than current one | ||
Function to get Next Greatest Element (NGE) pair for all elements of list | ||
Maximum element present afterwards the current one which is also greater than current one | ||
>>> printNGE([11,13,21,3]) | ||
11 -- 13 | ||
13 -- 21 | ||
21 -- -1 | ||
3 -- -1 | ||
[13,21,-1,-1] | ||
itsvaibhav01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
result = [] | ||
for i in range(0, len(arr), 1): | ||
|
||
next = -1 | ||
for j in range(i + 1, len(arr), 1): | ||
if arr[i] < arr[j]: | ||
next = arr[j] | ||
break | ||
result.append(next) | ||
|
||
return result | ||
|
||
def next_greatest_element(arr): | ||
""" | ||
Function to get Next Greatest Element (NGE) pair for all elements of list | ||
Maximum element present afterwards the current one which is also greater than current one | ||
|
||
Naive way to solve this is to take two loops and check for the next bigger number but that will make the | ||
time complexity as O(n^2). The better way to solve this would be to use a stack to keep track of maximum | ||
number givig a linear time complex solution. | ||
|
||
>>> printNGE([11,13,21,3]) | ||
itsvaibhav01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[13,21,-1,-1] | ||
itsvaibhav01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
stack = [] | ||
result = [-1]*len(arr) | ||
|
||
for index in reversed(range(len(arr))): | ||
if len(stack): | ||
while stack[-1] <= arr[index]: | ||
stack.pop() | ||
if len(stack) == 0: | ||
break | ||
|
||
if len(stack) != 0: | ||
result[index] = stack[-1] | ||
|
||
stack.append(arr[index]) | ||
|
||
return result | ||
|
||
print(str(arr[i]) + " -- " + str(next)) | ||
|
||
|
||
# Driver program to test above function | ||
arr = [11, 13, 21, 3] | ||
printNGE(arr) | ||
next_greatest_element_slow(arr) | ||
next_greatest_element(arr) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.