/root/compile: line 2: $'\r': command not foundThe above means you need to dos2unix the file.
| """ | |
| https://leetcode.com/problems/top-k-frequent-words/ | |
| Given a non-empty list of words, return the k most frequent elements. | |
| Your answer should be sorted by frequency from highest to lowest. | |
| If two words have the same frequency, then the word with the | |
| lower alphabetical order comes first. | |
| collections.Counter([iterable-or-mapping]) counts hashable objects |
| """ | |
| https://py.checkio.org/forum/post/9298/sorted-function-explanation-please/ | |
| This will sort a list of tuples by strings (second tuple item) | |
| and then by number in reversed order when strings are equal. | |
| """ | |
| list_of_tuples = [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')] | |
| list_of_tuples.sort(key=lambda t:(t[1], -t[0])) | |
| print(list_of_tuples) |
| # Parallel sorting of lists | |
| data = zip(list1, list2) | |
| data.sort() | |
| list1, list2 = map(lambda t: list(t), zip(*data)) |
| """ | |
| https://practice.geeksforgeeks.org/problems/inorder-traversal/1 | |
| """ | |
| def InOrder(root): | |
| if root is None: | |
| return | |
| InOrder(root.left) | |
| """ | |
| https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1 | |
| - use recursion to find the height | |
| - use max on left and right branch on every recursion (add 1 every iteration) | |
| """ | |
| def height(root): | |
| if root is None: | |
| return 0 | |
| return 1 + max(height(root.left), height(root.right)) |
| """ | |
| https://practice.geeksforgeeks.org/problems/finding-middle-element-in-a-linked-list/1 | |
| - Use "tortoise and the hare" method to find the middle | |
| - Use a pointer that increments half the "speed" of another pointer | |
| """ | |
| def findMid(head): | |
| if head is None: | |
| return None |
| """ | |
| https://www.algoexpert.io/questions/Branch%20Sums | |
| """ | |
| class BinaryTree: | |
| def __init__(self, value): | |
| self.value = value | |
| self.left = None | |
| self.right = None |
| """ | |
| https://www.algoexpert.io/questions/Find%20Closest%20Value%20In%20BST | |
| Average: Theta(logn) time | Theta(logn) space | |
| Worst: O(n) time | O(n) space | |
| This solution takes up O(n) space because of recursion. | |
| float('inf') is used to set a unbounded upper value for comparison | |
| """ |
| """ | |
| https://www.algoexpert.io/questions/Two%20Number%20Sum | |
| O(nlogn) time | O(1) space | |
| This algorithm sorts the arrays and then puts two index | |
| pointers at the edges and then advances towards the | |
| middle to find a solution. | |
| """ | |
| def twoNumberSum(array, targetSum): | |
| # sort array (really a list) first |