Sponsored
Sponsored
The Binary Indexed Tree (BIT) allows us to efficiently update elements and calculate cumulative frequencies in an iterative manner. In this approach, we traverse the input array from right to left, update the BIT with the index of the current number, and use the BIT to find the count of numbers which are smaller.
Time Complexity: O(n log n), where n is the number of elements in nums. Updating the BIT and querying both take O(log n) time.
Space Complexity: O(n) for the BIT and result array.
1class FenwickTree {
2 constructor(size) {
3 this.tree = new Array(size + 1).fill(0);
4 }
5 update(index, value) {
6 while (index < this.tree.length) {
7 this.tree[index] += value;
8 index += index & -index;
9 }
10 }
11 query(index) {
12 let sum = 0;
13 while (index > 0) {
14 sum += this.tree[index];
15 index -= index & -index;
16 }
17 return sum;
18 }
19}
20
21const countSmaller = function (nums) {
22 const offset = 10000;
23 const size = 20001;
24 const bit = new FenwickTree(size);
25 const result = new Array(nums.length).fill(0);
26 for (let i = nums.length - 1; i >= 0; i--) {
27 result[i] = bit.query(nums[i] + offset);
28 bit.update(nums[i] + offset + 1, 1);
29 }
30 return result;
31};
32
33const nums = [5, 2, 6, 1];
34console.log(countSmaller(nums));This JavaScript solution leverages a class-based structure for the Fenwick Tree, managing updates and queries in a dynamic array. Using an offset to translate negative numbers effectively, backward iteration over the input ensures that smaller counts are accurately gathered into the result list, making use of these BIT operations.
This approach takes advantage of the divide and conquer strategy combined with a binary search tree (BST). As we process each number from right to left, we insert them into the BST, which allows us to compute the count of elements smaller than the current number efficiently.
Time Complexity: O(n log n) on average, assuming balanced inserts into the BST.
Space Complexity: O(n) for storing BST nodes and result array.
using System.Collections.Generic;
class Solution {
public class TreeNode {
public int val, leftCount, dup;
public TreeNode left, right;
public TreeNode(int v) {
val = v;
leftCount = 0;
dup = 1;
}
}
public IList<int> CountSmaller(int[] nums) {
List<int> result = new List<int>(new int[nums.Length]);
TreeNode root = null;
for (int i = nums.Length - 1; i >= 0; i--) {
int[] preSum = {0};
root = Insert(root, nums[i], preSum);
result[i] = preSum[0];
}
return result;
}
private TreeNode Insert(TreeNode node, int val, int[] preSum) {
if (node == null) {
return new TreeNode(val);
} else if (node.val == val) {
node.dup++;
preSum[0] += node.leftCount;
} else if (node.val > val) {
node.leftCount++;
node.left = Insert(node.left, val, preSum);
} else {
preSum[0] += node.dup + node.leftCount;
node.right = Insert(node.right, val, preSum);
}
return node;
}
static void Main() {
Solution solution = new Solution();
int[] nums = {5, 2, 6, 1};
IList<int> result = solution.CountSmaller(nums);
Console.WriteLine(string.Join(" ", result));
}
}This C# program deploys a TreeNode that tracks duplicate counts and tree structure, allowing for integrity in traversal while accumulating occurrences of numbers smaller than the current particular element. Insert efficiency is achieved as seen within the problem requirements, substantiated by the preSum reusable variable designed throughout the recursive insert method.