forked from super30admin/Design-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed Sprint Design-5 - Please Review
- Loading branch information
1 parent
741f177
commit 71f4c80
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
# Time Complexity : O(1) for allocate,freespace anf available | ||
# Space Complexity : O(n) | ||
# Did this code successfully run on Leetcode : Yes | ||
# Any problem you faced while coding this : No | ||
from heapq import heappush as push | ||
from heapq import heappop as pop | ||
from heapq import heapify | ||
class Parking: | ||
|
||
def __init__(self,spaces,dist_map): | ||
self.spaces = spaces | ||
self.heap = [] | ||
self.dist_map = dist_map | ||
heapify(self.heap) | ||
for key in self.dist_map: | ||
push(self.heap,(self.dist[key],key)) | ||
|
||
def allocate(self): | ||
_,key = pop(self.heap) | ||
return key | ||
|
||
|
||
def freeSpace(self,key): | ||
push(self.heap,(self.dist[key],key)) | ||
|
||
|
||
def available(self): | ||
print(self.heap) | ||
|
This file contains 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,41 @@ | ||
# Time Complexity : O(n) | ||
# Space Complexity : O(1) | ||
# Did this code successfully run on Leetcode : Yes | ||
# Any problem you faced while coding this : No | ||
|
||
""" | ||
# Definition for a Node. | ||
class Node: | ||
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): | ||
self.val = int(x) | ||
self.next = next | ||
self.random = random | ||
""" | ||
|
||
#create a new node for every node and place it after original node then copy all the random pointers then remove the original nodes | ||
class Solution: | ||
def copyRandomList(self, head: 'Node') -> 'Node': | ||
if head == None: | ||
return head | ||
node = head | ||
while node != None: | ||
dummy = Node(node.val) | ||
nNode = node.next | ||
node.next = dummy | ||
dummy.next = nNode | ||
node = nNode | ||
node = head | ||
while node != None: | ||
if node.random == None: | ||
node.next.random = None | ||
else: | ||
node.next.random = node.random.next | ||
node = node.next.next | ||
node = head.next | ||
while node.next != None: | ||
node.next = node.next.next | ||
node = node.next | ||
node = head.next | ||
|
||
|
||
return head.next |