Back to course home
0% completed
Vote For New Content
My greedy solution
Mohammed Dh Abbas
Sep 14, 2024
class Solution: def minMoves(self, nums): min_number = float('inf') max_number = float('-inf') max_index = min_index = 0 for index, num in enumerate(nums): if num < min_number: min_number = num min_index = index if num > max_number: max_number = num max_index = index # left deletes to min and max items lef_min_del = min_index + 1 lef_max_del = max_index + 1 # right deletes to min an max items right_min_del = len(nums) - min_index right_max_del = len(nums) - max_index # deletes that could cover both of min and max items lef_both = max(lef_min_del, lef_max_del) right_both = max(right_min_del, right_max_del) same_dir_del_both = min(lef_both, right_both) # minimum deletes to min item min_del = min(lef_min_del, right_min_del) # minimum deletes to max item max_del = min(lef_max_del, right_max_del) return same_dir_del_both if same_dir_del_both < min_del + max_del else min_del + max_del
0
0
Comments
Comments
On this page