返回介绍

solution / 0400-0499 / 0480.Sliding Window Median / README_EN

发布于 2024-06-17 01:04:00 字数 10815 浏览 0 评论 0 收藏 0

480. Sliding Window Median

中文文档

Description

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.

  • For examples, if arr = [2,3,4], the median is 3.
  • For examples, if arr = [1,2,3,4], the median is (2 + 3) / 2 = 2.5.

You are given an integer array nums and an integer k. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return _the median array for each window in the original array_. Answers within 10-5 of the actual value will be accepted.

 

Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
Explanation: 
Window position        Median
---------------        -----
[1  3  -1] -3  5  3  6  7    1
 1 [3  -1  -3] 5  3  6  7     -1
 1  3 [-1  -3  5] 3  6  7     -1
 1  3  -1 [-3  5  3] 6  7    3
 1  3  -1  -3 [5  3  6] 7    5
 1  3  -1  -3  5 [3  6  7]     6

Example 2:

Input: nums = [1,2,3,4,2,3,1,4,2], k = 3
Output: [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]

 

Constraints:

  • 1 <= k <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1

Solutions

Solution 1

class MedianFinder:
  def __init__(self, k: int):
    self.k = k
    self.small = []
    self.large = []
    self.delayed = defaultdict(int)
    self.small_size = 0
    self.large_size = 0

  def add_num(self, num: int):
    if not self.small or num <= -self.small[0]:
      heappush(self.small, -num)
      self.small_size += 1
    else:
      heappush(self.large, num)
      self.large_size += 1
    self.rebalance()

  def find_median(self) -> float:
    return -self.small[0] if self.k & 1 else (-self.small[0] + self.large[0]) / 2

  def remove_num(self, num: int):
    self.delayed[num] += 1
    if num <= -self.small[0]:
      self.small_size -= 1
      if num == -self.small[0]:
        self.prune(self.small)
    else:
      self.large_size -= 1
      if num == self.large[0]:
        self.prune(self.large)
    self.rebalance()

  def prune(self, pq: List[int]):
    sign = -1 if pq is self.small else 1
    while pq and sign * pq[0] in self.delayed:
      self.delayed[sign * pq[0]] -= 1
      if self.delayed[sign * pq[0]] == 0:
        self.delayed.pop(sign * pq[0])
      heappop(pq)

  def rebalance(self):
    if self.small_size > self.large_size + 1:
      heappush(self.large, -heappop(self.small))
      self.small_size -= 1
      self.large_size += 1
      self.prune(self.small)
    elif self.small_size < self.large_size:
      heappush(self.small, -heappop(self.large))
      self.large_size -= 1
      self.small_size += 1
      self.prune(self.large)


class Solution:
  def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
    finder = MedianFinder(k)
    for x in nums[:k]:
      finder.add_num(x)
    ans = [finder.find_median()]
    for i in range(k, len(nums)):
      finder.add_num(nums[i])
      finder.remove_num(nums[i - k])
      ans.append(finder.find_median())
    return ans
class MedianFinder {
  private PriorityQueue<Integer> small = new PriorityQueue<>(Comparator.reverseOrder());
  private PriorityQueue<Integer> large = new PriorityQueue<>();
  private Map<Integer, Integer> delayed = new HashMap<>();
  private int smallSize;
  private int largeSize;
  private int k;

  public MedianFinder(int k) {
    this.k = k;
  }

  public void addNum(int num) {
    if (small.isEmpty() || num <= small.peek()) {
      small.offer(num);
      ++smallSize;
    } else {
      large.offer(num);
      ++largeSize;
    }
    rebalance();
  }

  public double findMedian() {
    return (k & 1) == 1 ? small.peek() : ((double) small.peek() + large.peek()) / 2;
  }

  public void removeNum(int num) {
    delayed.merge(num, 1, Integer::sum);
    if (num <= small.peek()) {
      --smallSize;
      if (num == small.peek()) {
        prune(small);
      }
    } else {
      --largeSize;
      if (num == large.peek()) {
        prune(large);
      }
    }
    rebalance();
  }

  private void prune(PriorityQueue<Integer> pq) {
    while (!pq.isEmpty() && delayed.containsKey(pq.peek())) {
      if (delayed.merge(pq.peek(), -1, Integer::sum) == 0) {
        delayed.remove(pq.peek());
      }
      pq.poll();
    }
  }

  private void rebalance() {
    if (smallSize > largeSize + 1) {
      large.offer(small.poll());
      --smallSize;
      ++largeSize;
      prune(small);
    } else if (smallSize < largeSize) {
      small.offer(large.poll());
      --largeSize;
      ++smallSize;
      prune(large);
    }
  }
}

class Solution {
  public double[] medianSlidingWindow(int[] nums, int k) {
    MedianFinder finder = new MedianFinder(k);
    for (int i = 0; i < k; ++i) {
      finder.addNum(nums[i]);
    }
    int n = nums.length;
    double[] ans = new double[n - k + 1];
    ans[0] = finder.findMedian();
    for (int i = k; i < n; ++i) {
      finder.addNum(nums[i]);
      finder.removeNum(nums[i - k]);
      ans[i - k + 1] = finder.findMedian();
    }
    return ans;
  }
}
class MedianFinder {
public:
  MedianFinder(int k) {
    this->k = k;
  }

  void addNum(int num) {
    if (small.empty() || num <= small.top()) {
      small.push(num);
      ++smallSize;
    } else {
      large.push(num);
      ++largeSize;
    }
    reblance();
  }

  void removeNum(int num) {
    ++delayed[num];
    if (num <= small.top()) {
      --smallSize;
      if (num == small.top()) {
        prune(small);
      }
    } else {
      --largeSize;
      if (num == large.top()) {
        prune(large);
      }
    }
    reblance();
  }

  double findMedian() {
    return k & 1 ? small.top() : ((double) small.top() + large.top()) / 2.0;
  }

private:
  priority_queue<int> small;
  priority_queue<int, vector<int>, greater<int>> large;
  unordered_map<int, int> delayed;
  int smallSize = 0;
  int largeSize = 0;
  int k;

  template <typename T>
  void prune(T& pq) {
    while (!pq.empty() && delayed[pq.top()]) {
      if (--delayed[pq.top()] == 0) {
        delayed.erase(pq.top());
      }
      pq.pop();
    }
  }

  void reblance() {
    if (smallSize > largeSize + 1) {
      large.push(small.top());
      small.pop();
      --smallSize;
      ++largeSize;
      prune(small);
    } else if (smallSize < largeSize) {
      small.push(large.top());
      large.pop();
      ++smallSize;
      --largeSize;
      prune(large);
    }
  }
};

class Solution {
public:
  vector<double> medianSlidingWindow(vector<int>& nums, int k) {
    MedianFinder finder(k);
    for (int i = 0; i < k; ++i) {
      finder.addNum(nums[i]);
    }
    vector<double> ans = {finder.findMedian()};
    for (int i = k; i < nums.size(); ++i) {
      finder.addNum(nums[i]);
      finder.removeNum(nums[i - k]);
      ans.push_back(finder.findMedian());
    }
    return ans;
  }
};
type MedianFinder struct {
  small        hp
  large        hp
  delayed        map[int]int
  smallSize, largeSize int
  k          int
}

func Constructor(k int) MedianFinder {
  return MedianFinder{hp{}, hp{}, map[int]int{}, 0, 0, k}
}

func (this *MedianFinder) AddNum(num int) {
  if this.small.Len() == 0 || num <= -this.small.IntSlice[0] {
    heap.Push(&this.small, -num)
    this.smallSize++
  } else {
    heap.Push(&this.large, num)
    this.largeSize++
  }
  this.rebalance()
}

func (this *MedianFinder) FindMedian() float64 {
  if this.k&1 == 1 {
    return float64(-this.small.IntSlice[0])
  }
  return float64(-this.small.IntSlice[0]+this.large.IntSlice[0]) / 2
}

func (this *MedianFinder) removeNum(num int) {
  this.delayed[num]++
  if num <= -this.small.IntSlice[0] {
    this.smallSize--
    if num == -this.small.IntSlice[0] {
      this.prune(&this.small)
    }
  } else {
    this.largeSize--
    if num == this.large.IntSlice[0] {
      this.prune(&this.large)
    }
  }
  this.rebalance()
}

func (this *MedianFinder) prune(pq *hp) {
  sign := 1
  if pq == &this.small {
    sign = -1
  }
  for pq.Len() > 0 && this.delayed[sign*pq.IntSlice[0]] > 0 {
    this.delayed[sign*pq.IntSlice[0]]--
    if this.delayed[sign*pq.IntSlice[0]] == 0 {
      delete(this.delayed, sign*pq.IntSlice[0])
    }
    heap.Pop(pq)
  }
}

func (this *MedianFinder) rebalance() {
  if this.smallSize > this.largeSize+1 {
    heap.Push(&this.large, -heap.Pop(&this.small).(int))
    this.smallSize--
    this.largeSize++
    this.prune(&this.small)
  } else if this.smallSize < this.largeSize {
    heap.Push(&this.small, -heap.Pop(&this.large).(int))
    this.smallSize++
    this.largeSize--
    this.prune(&this.large)
  }
}

func medianSlidingWindow(nums []int, k int) []float64 {
  finder := Constructor(k)
  for _, num := range nums[:k] {
    finder.AddNum(num)
  }
  ans := []float64{finder.FindMedian()}
  for i := k; i < len(nums); i++ {
    finder.AddNum(nums[i])
    finder.removeNum(nums[i-k])
    ans = append(ans, finder.FindMedian())
  }
  return ans
}

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Push(v any)    { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文