Skip to content

Commit f1691e3

Browse files
committed
feat: add solutions to lc problem: No.1649
No.1649.Create Sorted Array through Instructions
1 parent d481df2 commit f1691e3

File tree

6 files changed

+566
-2
lines changed

6 files changed

+566
-2
lines changed

solution/1600-1699/1649.Create Sorted Array through Instructions/README.md

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,222 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
树状数组。
78+
79+
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
80+
81+
1. **单点更新** `update(x, delta)`: 把序列 x 位置的数加上一个值 delta;
82+
1. **前缀和查询** `query(x)`:查询序列 `[1,...x]` 区间的区间和,即位置 x 的前缀和。
83+
84+
这两个操作的时间复杂度均为 `O(log n)`
85+
86+
树状数组最基本的功能就是求比某点 x 小的点的个数(这里的比较是抽象的概念,可以是数的大小、坐标的大小、质量的大小等等)。
87+
88+
比如给定数组 `a[5] = {2, 5, 3, 4, 1}`,求 `b[i] = 位置 i 左边小于等于 a[i] 的数的个数`。对于此例,`b[5] = {0, 1, 1, 2, 0}`
89+
90+
解决方案是直接遍历数组,每个位置先求出 `query(a[i])`,然后再修改树状数组 `update(a[i], 1)` 即可。当数的范围比较大时,需要进行离散化,即先进行去重并排序,然后对每个数字进行编号。
91+
7792
<!-- tabs:start -->
7893

7994
### **Python3**
8095

8196
<!-- 这里可写当前语言的特殊实现逻辑 -->
8297

8398
```python
84-
99+
class BinaryIndexedTree:
100+
def __init__(self, n):
101+
self.n = n
102+
self.c = [0] * (n + 1)
103+
104+
@staticmethod
105+
def lowbit(x):
106+
return x & -x
107+
108+
def update(self, x, delta):
109+
while x <= self.n:
110+
self.c[x] += delta
111+
x += BinaryIndexedTree.lowbit(x)
112+
113+
def query(self, x):
114+
s = 0
115+
while x > 0:
116+
s += self.c[x]
117+
x -= BinaryIndexedTree.lowbit(x)
118+
return s
119+
120+
121+
class Solution:
122+
def createSortedArray(self, instructions: List[int]) -> int:
123+
n = max(instructions)
124+
tree = BinaryIndexedTree(n)
125+
ans = 0
126+
for num in instructions:
127+
a = tree.query(num - 1)
128+
b = tree.query(n) - tree.query(num)
129+
ans += min(a, b)
130+
tree.update(num, 1)
131+
return ans % int((1e9 + 7))
85132
```
86133

87134
### **Java**
88135

89136
<!-- 这里可写当前语言的特殊实现逻辑 -->
90137

91138
```java
139+
class Solution {
140+
public int createSortedArray(int[] instructions) {
141+
int n = 100010;
142+
int mod = (int) 1e9 + 7;
143+
BinaryIndexedTree tree = new BinaryIndexedTree(n);
144+
int ans = 0;
145+
for (int num : instructions) {
146+
int a = tree.query(num - 1);
147+
int b = tree.query(n) - tree.query(num);
148+
ans += Math.min(a, b);
149+
ans %= mod;
150+
tree.update(num, 1);
151+
}
152+
return ans;
153+
}
154+
}
155+
156+
class BinaryIndexedTree {
157+
private int n;
158+
private int[] c;
159+
160+
public BinaryIndexedTree(int n) {
161+
this.n = n;
162+
c = new int[n + 1];
163+
}
164+
165+
public void update(int x, int delta) {
166+
while (x <= n) {
167+
c[x] += delta;
168+
x += lowbit(x);
169+
}
170+
}
171+
172+
public int query(int x) {
173+
int s = 0;
174+
while (x > 0) {
175+
s += c[x];
176+
x -= lowbit(x);
177+
}
178+
return s;
179+
}
180+
181+
public static int lowbit(int x) {
182+
return x & -x;
183+
}
184+
}
185+
```
186+
187+
### **C++**
188+
189+
```cpp
190+
class BinaryIndexedTree {
191+
public:
192+
int n;
193+
vector<int> c;
194+
195+
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
196+
197+
void update(int x, int delta) {
198+
while (x <= n)
199+
{
200+
c[x] += delta;
201+
x += lowbit(x);
202+
}
203+
}
204+
205+
int query(int x) {
206+
int s = 0;
207+
while (x > 0)
208+
{
209+
s += c[x];
210+
x -= lowbit(x);
211+
}
212+
return s;
213+
}
214+
215+
int lowbit(int x) {
216+
return x & -x;
217+
}
218+
};
219+
220+
class Solution {
221+
public:
222+
int createSortedArray(vector<int>& instructions) {
223+
int n = 100010;
224+
int mod = 1e9 + 7;
225+
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
226+
int ans = 0;
227+
for (int num : instructions)
228+
{
229+
int a = tree->query(num - 1);
230+
int b = tree->query(n) - tree->query(num);
231+
ans += min(a, b);
232+
ans %= mod;
233+
tree->update(num, 1);
234+
}
235+
return ans;
236+
}
237+
};
238+
```
92239
240+
### **Go**
241+
242+
```go
243+
type BinaryIndexedTree struct {
244+
n int
245+
c []int
246+
}
247+
248+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
249+
c := make([]int, n+1)
250+
return &BinaryIndexedTree{n, c}
251+
}
252+
253+
func (this *BinaryIndexedTree) lowbit(x int) int {
254+
return x & -x
255+
}
256+
257+
func (this *BinaryIndexedTree) update(x, delta int) {
258+
for x <= this.n {
259+
this.c[x] += delta
260+
x += this.lowbit(x)
261+
}
262+
}
263+
264+
func (this *BinaryIndexedTree) query(x int) int {
265+
s := 0
266+
for x > 0 {
267+
s += this.c[x]
268+
x -= this.lowbit(x)
269+
}
270+
return s
271+
}
272+
273+
func createSortedArray(instructions []int) int {
274+
n := 100010
275+
mod := int(1e9 + 7)
276+
tree := newBinaryIndexedTree(n)
277+
ans := 0
278+
for _, num := range instructions {
279+
a, b := tree.query(num-1), tree.query(n)-tree.query(num)
280+
ans += min(a, b)
281+
ans %= mod
282+
tree.update(num, 1)
283+
}
284+
return ans
285+
}
286+
287+
func min(a, b int) int {
288+
if a < b {
289+
return a
290+
}
291+
return b
292+
}
93293
```
94294

95295
### **...**

0 commit comments

Comments
 (0)