顯示具有 LeetCode 標籤的文章。 顯示所有文章
顯示具有 LeetCode 標籤的文章。 顯示所有文章

2020年4月23日 星期四

[LeetCode] Medium - 1305. All Elements in Two Binary Search Trees (?)

Source From Here
Question
Given two binary search trees root1 and root2.

Return a list containing all the integers from both trees sorted in ascending order:


Constraints:
* Each tree has at most 5000 nodes.
* Each node's value is between [-10^5, 10^5].

Solution

Naive
  1. # Definition for a binary tree node.  
  2. class TreeNode:  
  3. #     def __init__(self, x):  
  4. #         self.val = x  
  5. #         self.left = None  
  6. #         self.right = None  
  7.   
  8. class Solution:  
  9.     def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:          
  10.         # 1) Collecting values of two trees  
  11.         list1 = []  
  12.         list2 = []  
  13.         def inorder(node, alist):  
  14.             if node is None:  
  15.                 return   
  16.               
  17.             if node.left:  
  18.                 inorder(node.left, alist)  
  19.                   
  20.             alist.append(node.val)  
  21.               
  22.             if node.right:  
  23.                 inorder(node.right, alist)  
  24.           
  25.         inorder(root1, list1)  
  26.         inorder(root2, list2)  
  27.           
  28.         # 2) Conducting mergesort  
  29.         rlist = []  
  30.         while list1 and list2:  
  31.             if list1[0] < list2[0]:  
  32.                 rlist.append(list1.pop(0))  
  33.             else:  
  34.                 rlist.append(list2.pop(0))  
  35.           
  36.         if list1:  
  37.             rlist.extend(list1)  
  38.           
  39.         if list2:  
  40.             rlist.extend(list2)  
  41.               
  42.         return rlist  
Runtime: 392 ms, faster than 45.72% of Python3 online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 21.6 MB, less than 100.00% of Python3 online submissions for All Elements in Two Binary Search Trees.

Discussion (link)
  1. class Solution:  
  2.     def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:          
  3.         def gen(node):  
  4.             if node:  
  5.                 yield from gen(node.left)  
  6.                 yield node.val  
  7.                 yield from gen(node.right)  
  8.                   
  9.         return list(heapq.merge(gen(root1), gen(root2)))  
Runtime: 3556 ms, faster than 5.01% of Python3 online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 23.5 MB, less than 100.00% of Python3 online submissions for All Elements in Two Binary Search Trees.

Supplement
FAQ - In practice, what are the main uses for the new "yield from" syntax in Python 3.3?

2020年4月9日 星期四

[LeetCode] Easy - 849. Maximize Distance to Closest Person (?)

Source From Here
Question
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.

Example 2:
Input: [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Solution

Naive
  1. class Solution:  
  2.     def maxDistToClosest(self, seats):  
  3.         max_dist = 0  
  4.   
  5.         def dist2cp(zero_seq, is_edged):  
  6.             if is_edged:  
  7.                 return zero_seq  
  8.             else:  
  9.                 return (zero_seq-1) // 2 + 1  
  10.   
  11.         zc = 0  
  12.         is_edge = True  
  13.         for v in seats:  
  14.             if v == 1:  
  15.                 dist = dist2cp(zc, is_edge)  
  16.                 if dist > max_dist:  
  17.                     max_dist = dist  
  18.   
  19.                 zc = 0  
  20.                 is_edge = False  
  21.             else:  
  22.                 zc += 1  
  23.   
  24.         if zc > 0:  
  25.             dist = dist2cp(zc, True)  
  26.             if dist > max_dist:  
  27.                 max_dist = dist  
  28.   
  29.         return max_dist  
  30.   
  31. if __name__ == '__main__':  
  32.     s = Solution()  
  33.     for seats, ans in [  
  34.                         ([1, 0, 0, 0, 1, 0, 1], 2),  
  35.                         ([0, 0, 0, 1, 0, 0, 0, 1], 3),  
  36.                         ([1, 0, 0, 1, 1], 1)  
  37.                       ]:  
  38.         rel = s.maxDistToClosest(seats)  
  39.         print("{}: {}".format(seats, rel))  
  40.         assert ans == rel  
Runtime: 120 ms, faster than 99.47% of Python3 online submissions for Maximize Distance to Closest Person.
Memory Usage: 14.5 MB, less than 8.33% of Python3 online submissions for Maximize Distance to Closest Person.

Discussion - Solution in Python 3 (link)
  1. class Solution:  
  2.     def maxDistToClosest(self, seats: List[int]) -> int:  
  3.         L = len(seats)  
  4.           
  5.         # 1) Get seat of each person  
  6.         # e.g seats = [1, 0, 0, 0, 1, 0, 1]  
  7.         #     S = [0, 4, 6]  
  8.         S = [i for i in range(L) if seats[i]]  
  9.           
  10.         # 2) Calculate distance between two sequential person  
  11.         #    S = [0, 4, 6]  
  12.         #    d = [(4-0//2)=2, (6-4)//2=1] = [2, 1]  
  13.         d = [S[i+1]-S[i] for i in range(len(S)-1)] if len(S) > 1 else [0]  
  14.           
  15.         # 3) Find maximum distance  
  16.         return max(max(d)//2, S[0], L-1-S[-1])  
Runtime: 136 ms, faster than 78.43% of Python3 online submissions for Maximize Distance to Closest Person.
Memory Usage: 14.7 MB, less than 8.33% of Python3 online submissions for Maximize Distance to Closest Person.


[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...