Skip to content

2519. Count the Number of K-Big Indices 👍

  • Time: $O(n\log n)$
  • Space: $O(n)$
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class FenwickTree {
 public:
  FenwickTree(int n) : sums(n + 1) {}

  void add(int i, int delta) {
    while (i < sums.size()) {
      sums[i] += delta;
      i += lowbit(i);
    }
  }

  int get(int i) const {
    int sum = 0;
    while (i > 0) {
      sum += sums[i];
      i -= lowbit(i);
    }
    return sum;
  }

 private:
  vector<int> sums;

  static inline int lowbit(int i) {
    return i & -i;
  }
};

class Solution {
 public:
  int kBigIndices(vector<int>& nums, int k) {
    const int n = nums.size();
    int ans = 0;
    FenwickTree leftTree(n);
    FenwickTree rightTree(n);
    // left[i] := the number of `nums` < nums[i] with index < i
    vector<int> left(n);
    // right[i] := the number of `nums` < nums[i] with index > i
    vector<int> right(n);

    for (int i = 0; i < n; ++i) {
      left[i] = leftTree.get(nums[i] - 1);
      leftTree.add(nums[i], 1);
    }

    for (int i = n - 1; i >= 0; --i) {
      right[i] = rightTree.get(nums[i] - 1);
      rightTree.add(nums[i], 1);
    }

    for (int i = 0; i < n; ++i)
      if (left[i] >= k && right[i] >= k)
        ++ans;

    return ans;
  }
};
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class FenwickTree {
  public FenwickTree(int n) {
    sums = new int[n + 1];
  }

  public void add(int i, int delta) {
    while (i < sums.length) {
      sums[i] += delta;
      i += lowbit(i);
    }
  }

  public int get(int i) {
    int sum = 0;
    while (i > 0) {
      sum += sums[i];
      i -= lowbit(i);
    }
    return sum;
  }

  private int[] sums;

  private static int lowbit(int i) {
    return i & -i;
  }
}

class Solution {
  public int kBigIndices(int[] nums, int k) {
    final int n = nums.length;
    int ans = 0;
    FenwickTree leftTree = new FenwickTree(n);
    FenwickTree rightTree = new FenwickTree(n);
    // left[i] := the number of `nums` < nums[i] with index < i
    int[] left = new int[n];
    // right[i] := the number of `nums` < nums[i] with index > i
    int[] right = new int[n];

    for (int i = 0; i < n; ++i) {
      left[i] = leftTree.get(nums[i] - 1);
      leftTree.add(nums[i], 1);
    }

    for (int i = n - 1; i >= 0; --i) {
      right[i] = rightTree.get(nums[i] - 1);
      rightTree.add(nums[i], 1);
    }

    for (int i = 0; i < n; ++i)
      if (left[i] >= k && right[i] >= k)
        ++ans;

    return ans;
  }
}
 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
40
class FenwickTree:
  def __init__(self, n: int):
    self.sums = [0] * (n + 1)

  def add(self, i: int, delta: int) -> None:
    while i < len(self.sums):
      self.sums[i] += delta
      i += FenwickTree.lowbit(i)

  def get(self, i: int) -> int:
    summ = 0
    while i > 0:
      summ += self.sums[i]
      i -= FenwickTree.lowbit(i)
    return summ

  @staticmethod
  def lowbit(i: int) -> int:
    return i & -i


class Solution:
  def kBigIndices(self, nums: list[int], k: int) -> int:
    n = len(nums)
    leftTree = FenwickTree(n)
    rightTree = FenwickTree(n)
    # left[i] := the number of `nums` < nums[i] with index < i
    left = [0] * n
    # right[i] := the number of `nums` < nums[i] with index > i
    right = [0] * n

    for i, num in enumerate(nums):
      left[i] = leftTree.get(num - 1)
      leftTree.add(num, 1)

    for i in range(n - 1, -1, -1):
      right[i] = rightTree.get(nums[i] - 1)
      rightTree.add(nums[i], 1)

    return sum(l >= k and r >= k for l, r in zip(left, right))