Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 03533c4

Browse files
committedApr 24, 2025
Add solution #1649
1 parent fc760d3 commit 03533c4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,448 LeetCode solutions in JavaScript
1+
# 1,449 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1271,6 +1271,7 @@
12711271
1646|[Get Maximum in Generated Array](./solutions/1646-get-maximum-in-generated-array.js)|Easy|
12721272
1647|[Minimum Deletions to Make Character Frequencies Unique](./solutions/1647-minimum-deletions-to-make-character-frequencies-unique.js)|Medium|
12731273
1648|[Sell Diminishing-Valued Colored Balls](./solutions/1648-sell-diminishing-valued-colored-balls.js)|Medium|
1274+
1649|[Create Sorted Array through Instructions](./solutions/1649-create-sorted-array-through-instructions.js)|Hard|
12741275
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12751276
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12761277
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 1649. Create Sorted Array through Instructions
3+
* https://leetcode.com/problems/create-sorted-array-through-instructions/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer array instructions, you are asked to create a sorted array from the elements in
7+
* instructions. You start with an empty container nums. For each element from left to right in
8+
* instructions, insert it into nums. The cost of each insertion is the minimum of the following:
9+
* - The number of elements currently in nums that are strictly less than instructions[i].
10+
* - The number of elements currently in nums that are strictly greater than instructions[i].
11+
*
12+
* For example, if inserting element 3 into nums = [1,2,3,5], the cost of insertion is min(2, 1)
13+
* (elements 1 and 2 are less than 3, element 5 is greater than 3) and nums will become [1,2,3,3,5].
14+
*
15+
* Return the total cost to insert all elements from instructions into nums. Since the answer may
16+
* be large, return it modulo 109 + 7
17+
*/
18+
19+
/**
20+
* @param {number[]} instructions
21+
* @return {number}
22+
*/
23+
var createSortedArray = function(instructions) {
24+
const MOD = 1e9 + 7;
25+
const maxValue = Math.max(...instructions);
26+
const fenwickTree = new Array(maxValue + 1).fill(0);
27+
let totalCost = 0;
28+
29+
for (let i = 0; i < instructions.length; i++) {
30+
const value = instructions[i];
31+
const lessCount = query(value - 1);
32+
const greaterCount = i - query(value);
33+
totalCost = (totalCost + Math.min(lessCount, greaterCount)) % MOD;
34+
update(value);
35+
}
36+
37+
return totalCost;
38+
39+
function update(index) {
40+
for (let i = index; i <= maxValue; i += i & -i) {
41+
fenwickTree[i]++;
42+
}
43+
}
44+
45+
function query(index) {
46+
let sum = 0;
47+
for (let i = index; i > 0; i -= i & -i) {
48+
sum += fenwickTree[i];
49+
}
50+
return sum;
51+
}
52+
};

0 commit comments

Comments
 (0)
Please sign in to comment.