Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
minimize-rounding-error-to-meet-target.py 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-06-03 13:46 +08:00 . Update minimize-rounding-error-to-meet-target.py
# Time: O(n) on average
# Space: O(n)
import math
import random
class Solution(object):
def minimizeError(self, prices, target):
"""
:type prices: List[str]
:type target: int
:rtype: str
"""
def kthElement(nums, k, compare=lambda a, b: a < b):
def PartitionAroundPivot(left, right, pivot_idx, nums, compare):
new_pivot_idx = left
nums[pivot_idx], nums[right] = nums[right], nums[pivot_idx]
for i in xrange(left, right):
if compare(nums[i], nums[right]):
nums[i], nums[new_pivot_idx] = nums[new_pivot_idx], nums[i]
new_pivot_idx += 1
nums[right], nums[new_pivot_idx] = nums[new_pivot_idx], nums[right]
return new_pivot_idx
left, right = 0, len(nums) - 1
while left <= right:
pivot_idx = random.randint(left, right)
new_pivot_idx = PartitionAroundPivot(left, right, pivot_idx, nums, compare)
if new_pivot_idx == k - 1:
return
elif new_pivot_idx > k - 1:
right = new_pivot_idx - 1
else: # new_pivot_idx < k - 1.
left = new_pivot_idx + 1
errors = []
lower, upper = 0, 0
for i, p in enumerate(map(float, prices)):
lower += int(math.floor(p))
upper += int(math.ceil(p))
if p != math.floor(p):
errors.append(p-math.floor(p))
if not lower <= target <= upper:
return "-1"
lower_round_count = upper-target
kthElement(errors, lower_round_count)
result = 0.0
for i in xrange(len(errors)):
if i < lower_round_count:
result += errors[i]
else:
result += 1.0-errors[i]
return "{:.3f}".format(result)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助