-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateSortedArraythroughInstructions.py
More file actions
39 lines (32 loc) · 1 KB
/
createSortedArraythroughInstructions.py
File metadata and controls
39 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/create-sorted-array-through-instructions/
# Author: Miao Zhang
# Date: 2021-05-25
class FenwickTree:
def __init__(self, n):
self.sums = [0 for _ in range(n + 1)]
def update(self, i, delta):
while i < len(self.sums):
self.sums[i] += delta
i += self.lowbit(i)
def query(self, i):
res = 0
while i > 0:
res += self.sums[i]
i -= self.lowbit(i)
return res
def lowbit(self, x):
return x & (-x)
class Solution:
def createSortedArray(self, instructions: List[int]) -> int:
n = len(instructions)
kMod = 10 ** 9 + 7
tree = FenwickTree(10 ** 5)
res = 0
for i in range(n):
lt = tree.query(instructions[i] - 1)
gt = i - tree.query(instructions[i])
res += min(lt, gt)
tree.update(instructions[i], 1)
return res % kMod