leetcode everyday 3

Problem 1 Sort List

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:
Input: 4->2->1->3
Output: 1->2->3->4

Example 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5

Hint: Due to the topic of time complexity and space complexity is higher, so looked at various solutions, the best solution is to merge sort, because the list in the merge operation does not need to be like an array of merge operation assigned a temporary array space, space complexity, so it is constant, of course, there is no consideration of recursive system call stack.
This involves a linked list commonly used operation, that is, fast and slow pointer skills.Set slow and fast Pointers, both of which start at the top of the table, fast takes two steps at a time, slow takes one step at a time, fast goes to the end of the list, slow goes right to the middle, and that cuts the list in two.

class :
def sortlist(self,head:ListNode) -> ListNode:
if not head or not head.next:
return head
pre,slow,fast = None,head,head
while fast and fast.next:
pre,slow,fast = slow,slow.next,fast.next.next
pre.next = None
return self.merge(self.sortlist(head),self.sortlist(slow))

def merge(self,h1,h2):
dummy = tail = ListNode(None)
while h1 and h2:
if h1.val < h2.val:
tail.next,h1 = h1,h1.next
else:
tail.next,h2 = h2,h2.next
tail = tail.next
tail.next = h1 or h2
return dummy.next

Problem 2 Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: 2, [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished
course 0. So the correct course order is [0,1] .
Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both
courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3]

Hint: Topological sorting application.The topological order is like the backward order of every small project completed in a project, and our task is to find out the sequence of successful completion.
The method is to delete a node whose entry degree is 0 at a time, and subtract the entry degree of the node it points to by one.And each time the degree of entry is 0 node saved, and finally return it.That’s pretty much the same thing.

class :
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
dic = [0 for i in range(numCourses)]
neigh = collections.defaultdict(set)
for i,j in prerequisites:
dic[i]+=1
neigh[j].add(i)
stack = [i for i in range(numCourses) if dic[i]==0]
res = []
while stack:
node = stack.pop()
res.append(node)
for i in neigh[node]:
dic[i]-=1
if dic[i] == 1:
stack.append(i)
for i in range(numCourses):
if dic[i] > 0:
return []
return res