- Notifications
You must be signed in to change notification settings - Fork437
Added problem to find the middle element of the linked-list and P03 in Heaps for finding KthLargestElement#43
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
LunaticProgrammer wants to merge4 commits intoOmkarPathak:masterChoose a base branch fromLunaticProgrammer:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletionsHeap/P03_FindKthLargestElementInArray.py
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Heap | ||
def findKthLargestElement(nums:list,k:int): | ||
''' | ||
The problem here is to find the Kth Largest Element from the array. | ||
to solve this we could've used sort and given the answer but it would've cost us O(N*logN) where N is the number of elements in the array | ||
to even optimize that solution we can use heaps. | ||
the idea here is: | ||
1. create a min-heap with adding all the values as negative to it | ||
2. deleting till second last step | ||
3. returning the element at the last step as the answer | ||
T - O(N +K*log(N)) where K is the position of Kth largest element and N is the number of elements in the array | ||
''' | ||
h=Heap.BinaryHeap() | ||
[h.insert(-num) for num in nums] | ||
[h.delete() for _ in range(k-1)] | ||
return -h.delete() | ||
if __name__ == '__main__': | ||
nums=[2,3,5,6,4] | ||
k=2 | ||
kthLargestElement = findKthLargestElement(nums,k) | ||
print("the Kth("+str(k)+") largest element is",kthLargestElement) |
39 changes: 39 additions & 0 deletionsLinked Lists/P03_FindingMidOfLinkedList.py
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Author: VARNIT SHARMA (LunaticProgrammer) | ||
import SinglyLinkedList | ||
def findMid(myLinkedList): | ||
''' | ||
The approch is simple: | ||
1. creating a fast pointer that is moving and skipping one element of the linked-list | ||
2. creating a slow pointer that goes through every next element in the linked-list | ||
3. when the fast pointer has reached the end the slower one is supposed to be at mid | ||
4. return the element pointed by slow pointer | ||
Time-O(logN) where N is the number of elements in linked-list | ||
''' | ||
if not myLinkedList or not myLinkedList.head.next: return myLinkedList.head | ||
slow = myLinkedList.head | ||
fast = myLinkedList.head | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
return slow | ||
if __name__ == '__main__': | ||
myLinkedList = SinglyLinkedList.LinkedList() | ||
for i in range(10, 0, -1): | ||
myLinkedList.insertAtStart(i) | ||
print("LinkedList",end=" ") | ||
myLinkedList.printLinkedList() | ||
print() | ||
midleElement = findMid(myLinkedList) | ||
print("The middle element of the LinkedList is",midleElement.data) | ||
# OUTPUT: | ||
# The middle element of the LinkedList is 6 |
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.