返回介绍

solution / 0500-0599 / 0519.Random Flip Matrix / README_EN

发布于 2024-06-17 01:03:59 字数 3942 浏览 0 评论 0 收藏 0

519. Random Flip Matrix

中文文档

Description

There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned.

Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity.

Implement the Solution class:

  • Solution(int m, int n) Initializes the object with the size of the binary matrix m and n.
  • int[] flip() Returns a random index [i, j] of the matrix where matrix[i][j] == 0 and flips it to 1.
  • void reset() Resets all the values of the matrix to be 0.

 

Example 1:

Input
["Solution", "flip", "flip", "flip", "reset", "flip"]
[[3, 1], [], [], [], [], []]
Output
[null, [1, 0], [2, 0], [0, 0], null, [2, 0]]

Explanation
Solution solution = new Solution(3, 1);
solution.flip();  // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
solution.flip();  // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]
solution.flip();  // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.
solution.reset(); // All the values are reset to 0 and can be returned.
solution.flip();  // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.

 

Constraints:

  • 1 <= m, n <= 104
  • There will be at least one free cell for each call to flip.
  • At most 1000 calls will be made to flip and reset.

Solutions

Solution 1

class Solution:
  def __init__(self, m: int, n: int):
    self.m = m
    self.n = n
    self.total = m * n
    self.mp = {}

  def flip(self) -> List[int]:
    self.total -= 1
    x = random.randint(0, self.total)
    idx = self.mp.get(x, x)
    self.mp[x] = self.mp.get(self.total, self.total)
    return [idx // self.n, idx % self.n]

  def reset(self) -> None:
    self.total = self.m * self.n
    self.mp.clear()


# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()
class Solution {
  private int m;
  private int n;
  private int total;
  private Random rand = new Random();
  private Map<Integer, Integer> mp = new HashMap<>();

  public Solution(int m, int n) {
    this.m = m;
    this.n = n;
    this.total = m * n;
  }

  public int[] flip() {
    int x = rand.nextInt(total--);
    int idx = mp.getOrDefault(x, x);
    mp.put(x, mp.getOrDefault(total, total));
    return new int[] {idx / n, idx % n};
  }

  public void reset() {
    total = m * n;
    mp.clear();
  }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(m, n);
 * int[] param_1 = obj.flip();
 * obj.reset();
 */

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

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

发布评论

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