Skip to content

Commit f76f157

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

File tree

7 files changed

+342
-239
lines changed

7 files changed

+342
-239
lines changed

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

+116-78
Original file line numberDiff line numberDiff line change
@@ -114,34 +114,30 @@ class BinaryIndexedTree:
114114
self.n = n
115115
self.c = [0] * (n + 1)
116116

117-
@staticmethod
118-
def lowbit(x):
119-
return x & -x
120-
121-
def update(self, x, delta):
117+
def update(self, x: int, v: int):
122118
while x <= self.n:
123-
self.c[x] += delta
124-
x += BinaryIndexedTree.lowbit(x)
119+
self.c[x] += v
120+
x += x & -x
125121

126-
def query(self, x):
122+
def query(self, x: int) -> int:
127123
s = 0
128-
while x > 0:
124+
while x:
129125
s += self.c[x]
130-
x -= BinaryIndexedTree.lowbit(x)
126+
x -= x & -x
131127
return s
132128

133129

134130
class Solution:
135131
def createSortedArray(self, instructions: List[int]) -> int:
136-
n = max(instructions)
137-
tree = BinaryIndexedTree(n)
132+
m = max(instructions)
133+
tree = BinaryIndexedTree(m)
138134
ans = 0
139-
for num in instructions:
140-
a = tree.query(num - 1)
141-
b = tree.query(n) - tree.query(num)
142-
ans += min(a, b)
143-
tree.update(num, 1)
144-
return ans % int((1e9 + 7))
135+
mod = 10 ** 9 + 7
136+
for i, x in enumerate(instructions):
137+
cost = min(tree.query(x - 1), i - tree.query(x))
138+
ans += cost
139+
tree.update(x, 1)
140+
return ans % mod
145141
```
146142

147143
线段树:
@@ -212,50 +208,48 @@ class Solution:
212208
树状数组:
213209

214210
```java
215-
class Solution {
216-
public int createSortedArray(int[] instructions) {
217-
int n = 100010;
218-
int mod = (int) 1e9 + 7;
219-
BinaryIndexedTree tree = new BinaryIndexedTree(n);
220-
int ans = 0;
221-
for (int num : instructions) {
222-
int a = tree.query(num - 1);
223-
int b = tree.query(n) - tree.query(num);
224-
ans += Math.min(a, b);
225-
ans %= mod;
226-
tree.update(num, 1);
227-
}
228-
return ans;
229-
}
230-
}
231-
232211
class BinaryIndexedTree {
233212
private int n;
234213
private int[] c;
235214

236215
public BinaryIndexedTree(int n) {
237216
this.n = n;
238-
c = new int[n + 1];
217+
this.c = new int[n + 1];
239218
}
240219

241-
public void update(int x, int delta) {
220+
public void update(int x, int v) {
242221
while (x <= n) {
243-
c[x] += delta;
244-
x += lowbit(x);
222+
c[x] += v;
223+
x += x & -x;
245224
}
246225
}
247226

248227
public int query(int x) {
249228
int s = 0;
250229
while (x > 0) {
251230
s += c[x];
252-
x -= lowbit(x);
231+
x -= x & -x;
253232
}
254233
return s;
255234
}
235+
}
256236

257-
public static int lowbit(int x) {
258-
return x & -x;
237+
class Solution {
238+
public int createSortedArray(int[] instructions) {
239+
int m = 0;
240+
for (int x : instructions) {
241+
m = Math.max(m, x);
242+
}
243+
BinaryIndexedTree tree = new BinaryIndexedTree(m);
244+
int ans = 0;
245+
final int mod = (int) 1e9 + 7;
246+
for (int i = 0; i < instructions.length; ++i) {
247+
int x = instructions[i];
248+
int cost = Math.min(tree.query(x - 1), i - tree.query(x));
249+
ans = (ans + cost) % mod;
250+
tree.update(x, 1);
251+
}
252+
return ans;
259253
}
260254
}
261255
```
@@ -350,47 +344,43 @@ class SegmentTree {
350344
```cpp
351345
class BinaryIndexedTree {
352346
public:
353-
int n;
354-
vector<int> c;
355-
356347
BinaryIndexedTree(int _n)
357348
: n(_n)
358-
, c(_n + 1) { }
349+
, c(_n + 1) {}
359350

360351
void update(int x, int delta) {
361352
while (x <= n) {
362353
c[x] += delta;
363-
x += lowbit(x);
354+
x += x & -x;
364355
}
365356
}
366357

367358
int query(int x) {
368359
int s = 0;
369-
while (x > 0) {
360+
while (x) {
370361
s += c[x];
371-
x -= lowbit(x);
362+
x -= x & -x;
372363
}
373364
return s;
374365
}
375366

376-
int lowbit(int x) {
377-
return x & -x;
378-
}
367+
private:
368+
int n;
369+
vector<int> c;
379370
};
380371

381372
class Solution {
382373
public:
383374
int createSortedArray(vector<int>& instructions) {
384-
int n = 100010;
385-
int mod = 1e9 + 7;
386-
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
375+
int m = *max_element(instructions.begin(), instructions.end());
376+
BinaryIndexedTree tree(m);
377+
const int mod = 1e9 + 7;
387378
int ans = 0;
388-
for (int num : instructions) {
389-
int a = tree->query(num - 1);
390-
int b = tree->query(n) - tree->query(num);
391-
ans += min(a, b);
392-
ans %= mod;
393-
tree->update(num, 1);
379+
for (int i = 0; i < instructions.size(); ++i) {
380+
int x = instructions[i];
381+
int cost = min(tree.query(x - 1), i - tree.query(x));
382+
ans = (ans + cost) % mod;
383+
tree.update(x, 1);
394384
}
395385
return ans;
396386
}
@@ -487,38 +477,42 @@ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
487477
return &BinaryIndexedTree{n, c}
488478
}
489479

490-
func (this *BinaryIndexedTree) lowbit(x int) int {
491-
return x & -x
492-
}
493-
494480
func (this *BinaryIndexedTree) update(x, delta int) {
495481
for x <= this.n {
496482
this.c[x] += delta
497-
x += this.lowbit(x)
483+
x += x & -x
498484
}
499485
}
500486

501487
func (this *BinaryIndexedTree) query(x int) int {
502488
s := 0
503489
for x > 0 {
504490
s += this.c[x]
505-
x -= this.lowbit(x)
491+
x -= x & -x
506492
}
507493
return s
508494
}
509495

510-
func createSortedArray(instructions []int) int {
511-
n := 100010
512-
mod := int(1e9 + 7)
513-
tree := newBinaryIndexedTree(n)
514-
ans := 0
515-
for _, num := range instructions {
516-
a, b := tree.query(num-1), tree.query(n)-tree.query(num)
517-
ans += min(a, b)
518-
ans %= mod
519-
tree.update(num, 1)
496+
func createSortedArray(instructions []int) (ans int) {
497+
m := 0
498+
for _, x := range instructions {
499+
m = max(m, x)
500+
}
501+
tree := newBinaryIndexedTree(m)
502+
const mod = 1e9 + 7
503+
for i, x := range instructions {
504+
cost := min(tree.query(x-1), i-tree.query(x))
505+
ans = (ans + cost) % mod
506+
tree.update(x, 1)
507+
}
508+
return
509+
}
510+
511+
func max(a, b int) int {
512+
if a > b {
513+
return a
520514
}
521-
return ans
515+
return b
522516
}
523517

524518
func min(a, b int) int {
@@ -529,6 +523,50 @@ func min(a, b int) int {
529523
}
530524
```
531525

526+
### **TypeScript**
527+
528+
```ts
529+
class BinaryIndexedTree {
530+
private n: number;
531+
private c: number[];
532+
533+
constructor(n: number) {
534+
this.n = n;
535+
this.c = new Array(n + 1).fill(0);
536+
}
537+
538+
public update(x: number, v: number): void {
539+
while (x <= this.n) {
540+
this.c[x] += v;
541+
x += x & -x;
542+
}
543+
}
544+
545+
public query(x: number): number {
546+
let s = 0;
547+
while (x > 0) {
548+
s += this.c[x];
549+
x -= x & -x;
550+
}
551+
return s;
552+
}
553+
}
554+
555+
function createSortedArray(instructions: number[]): number {
556+
const m = Math.max(...instructions);
557+
const tree = new BinaryIndexedTree(m);
558+
let ans = 0;
559+
const mod = 10 ** 9 + 7;
560+
for (let i = 0; i < instructions.length; ++i) {
561+
const x = instructions[i];
562+
const cost = Math.min(tree.query(x - 1), i - tree.query(x));
563+
ans = (ans + cost) % mod;
564+
tree.update(x, 1);
565+
}
566+
return ans;
567+
}
568+
```
569+
532570
### **...**
533571

534572
```

0 commit comments

Comments
 (0)