下面给出的这个问题使我发疯。请任何人帮助。
“给定一个数组,'n'个元素的个数返回一个长度为'n'的数组A,其中A [i]包含的元素数大于初始数组右边Num [i]的个数” / strong>
我看到一个SO答案here,但其中包含O(n ^ 2)的解。我需要 O(nlogn)的解决方案。
我有“为自己左侧的较小元素计数”的解决方案。但是修改它并没有给我所需的解决方案(请参阅下面的代码)。
感谢您的帮助:)
class BinarySearchTreeNode(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.count = 1
self.leftTreeSize = 0
class BinarySearchTree(object):
def __init__(self):
self.root = None
def insert(self, val, root):
if not root:
self.root = BinarySearchTreeNode(val)
return 0
if val == root.val:
root.count += 1
return root.leftTreeSize
if val < root.val:
root.leftTreeSize += 1
if not root.left:
root.left = BinarySearchTreeNode(val)
return 0
return self.insert(val, root.left)
if not root.right:
root.right = BinarySearchTreeNode(val)
return root.count + root.leftTreeSize
return root.count + root.leftTreeSize + self.insert(val, root.right)
class Solution(object):
def countSmaller(self, nums):
tree = BinarySearchTree()
return [
tree.insert(nums[i], tree.root)
for i in range(len(nums) - 1, -1, -1)
][::-1]
print(Solution().countSmaller(nums = [1, 4, 2, 7]))
示例:
给出数组[10,7,2,6,5]
那么从小到右的计数数组是[4,3,0,1,0]
从左到大的计数数组为[0,1,2,2,2,3]
希望这对您有帮助...
答案 0 :(得分:1)
好吧,我不知道这是否是您要寻找的解决方案,因为我假设您想让我修复您的代码,但是可以这样处理:
num = [10,7,2,6,5]
mergeSort(num)或heapSort(num)。我不知道python是否内置了这些代码,或者您是否需要自己实施。但是mergeSort / heapSort它的复杂度最差为O(nlog(n)。
sortedNum = [2,5,6,7,10]
answer = []
for every number in Num:
binarySearch number in sortedNum and return its position
answer.appened(position)
delete item from sortedNum at position.
for循环本身的复杂度为O(n),而循环内部的二分查找的复杂度为log(n)。因此,此函数的复杂度为O(n log(n)),因为追加和删除操作都需要O(1)
这意味着排序+伪编码函数的总复杂度为O(2nlog(n))= O(nlog(n))。
编辑:由于我弄错了,您想在自己的左边数更多的元素。
此处无需排序!
num = [10,7,2,6,5]
sortedNum = []
answer = []
for every number in Num:
position = binarySearch number in sortedNum to which numbers its inbetween.
insert number into that position into sortedNum
answer.append(len(num)-position)
答案 1 :(得分:0)
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.count = 1
self.rightTreeSize = 0
self.leftTreeSize = 0
class BinarySearchTree(object):
def __init__(self):
self.root = None
def insert(self, val, root):
if not root:
self.root = Node(val)
return 0
if val == root.val:
root.count += 1
return root.rightTreeSize
if val > root.val:
root.rightTreeSize += 1
if not root.right:
root.right = Node(val)
return 0
return self.insert(val, root.right)
if not root.left:
root.left = Node(val)
return root.count + root.rightTreeSize
return root.count + root.rightTreeSize + self.insert(val, root.left)
class BinarySearchTree1(object):
def __init__(self):
self.root = None
def insert(self, val, root):
if not root:
self.root = Node(val)
return 0
if val == root.val:
root.count += 1
return root.leftTreeSize
if val < root.val:
root.leftTreeSize += 1
if not root.left:
root.left = Node(val)
return 0
return self.insert(val, root.left)
if not root.right:
root.right = Node(val)
return root.count + root.leftTreeSize
return root.count + root.leftTreeSize + self.insert(val, root.right)
class Solution(object):
def countGreater(self):
nums = [10, 7, 2, 6, 5]
tree = BinarySearchTree()
print([tree.insert(nums[i], tree.root) for i in range(0, len(nums))])
def countSmaller(self):
nums = [10, 7, 2, 6, 5]
tree1 = BinarySearchTree1()
print([tree1.insert(nums[i], tree1.root) for i in range(len(nums) - 1, -1, -1)][::-1])
Solution().countSmaller()
Solution().countGreater()
抱歉,缩进问题无法在markdown编辑器中调整所有行。
上面的代码返回“自身右边较小元素的计数”和“自身左边较大元素的计数”