[
sort
hash table
]
BinarySearch 0267 Mixed Sorting
Problem statement
https://binarysearch.com/problems/Mixed-Sorting/
Solution
Keep odd and even elements + indexes. Then sort and write to hash table. Iterate over hash table and return list.
Complexity
It is O(n log n)
for time and space.
Code
class Solution:
def solve(self, nums):
d = {}
odd = [(i, x) for i, x in enumerate(nums) if x % 2 == 1]
evn = [(i, x) for i, x in enumerate(nums) if x % 2 == 0]
for i, x in zip([i for i, _ in odd], sorted([x for _, x in odd])[::-1]):
d[i] = x
for i, x in zip([i for i, _ in evn], sorted([x for _, x in evn])):
d[i] = x
return [d[i] for i in range(len(nums))]