diff --git a/solution/0900-0999/0977.Squares of a Sorted Array/README.md b/solution/0900-0999/0977.Squares of a Sorted Array/README.md index 7914c669e8f55..42f2dbbded508 100644 --- a/solution/0900-0999/0977.Squares of a Sorted Array/README.md +++ b/solution/0900-0999/0977.Squares of a Sorted Array/README.md @@ -23,7 +23,7 @@ tags: -

 

+

 

示例 1:

@@ -40,22 +40,22 @@ tags: 输出:[4,9,9,49,121] -

 

+

 

提示:

-

 

+

 

进阶:

diff --git a/solution/1800-1899/1891.Cutting Ribbons/README.md b/solution/1800-1899/1891.Cutting Ribbons/README.md index 28f44f27eca95..017e46531b4a8 100644 --- a/solution/1800-1899/1891.Cutting Ribbons/README.md +++ b/solution/1800-1899/1891.Cutting Ribbons/README.md @@ -29,7 +29,7 @@ tags:
  • 切割成四条长度为 1 的绳子。
  • -

    你的任务是找出最大 x 值,要求满足可以裁切出至少 k 条长度均为 x 的绳子。你可以丢弃裁切后剩余的任意长度的绳子。如果不可能切割出 k 条相同长度的绳子,返回 0。

    +

    你的任务是找出最大 x 值,要求满足可以裁切出恰好 k 条长度均为 x 的绳子。你可以丢弃裁切后剩余的任意长度的绳子。如果不可能切割出 k 条相同长度的绳子,返回 0。

     

    diff --git a/solution/3300-3399/3369.Design an Array Statistics Tracker/README.md b/solution/3300-3399/3369.Design an Array Statistics Tracker/README.md index ea1bedc26c2bd..7b79ae1d4f69a 100644 --- a/solution/3300-3399/3369.Design an Array Statistics Tracker/README.md +++ b/solution/3300-3399/3369.Design an Array Statistics Tracker/README.md @@ -3,9 +3,13 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README.md tags: + - 设计 - 队列 - 哈希表 - 二分查找 + - 数据流 + - 有序集合 + - 堆(优先队列) --- diff --git a/solution/3300-3399/3369.Design an Array Statistics Tracker/README_EN.md b/solution/3300-3399/3369.Design an Array Statistics Tracker/README_EN.md index 4ac3c14827b39..0861da2b522ad 100644 --- a/solution/3300-3399/3369.Design an Array Statistics Tracker/README_EN.md +++ b/solution/3300-3399/3369.Design an Array Statistics Tracker/README_EN.md @@ -3,9 +3,13 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README_EN.md tags: + - Design - Queue - Hash Table - Binary Search + - Data Stream + - Ordered Set + - Heap (Priority Queue) --- diff --git a/solution/3300-3399/3376.Minimum Time to Break Locks I/README_EN.md b/solution/3300-3399/3376.Minimum Time to Break Locks I/README_EN.md index 6f00900f768a0..2ca40e558bf1a 100644 --- a/solution/3300-3399/3376.Minimum Time to Break Locks I/README_EN.md +++ b/solution/3300-3399/3376.Minimum Time to Break Locks I/README_EN.md @@ -26,10 +26,10 @@ tags:

    Your task is to determine the minimum time in minutes required for Bob to break all n locks and escape the dungeon.

    @@ -40,7 +40,7 @@ tags:

    Example 1:

    -

    Input: strength = [3,4,1], K = 1

    +

    Input: strength = [3,4,1], k = 1

    Output: 4

    @@ -51,9 +51,9 @@ tags: Time Energy - X + x Action - Updated X + Updated x 0 @@ -99,7 +99,7 @@ tags:

    Example 2:

    -

    Input: strength = [2,5,4], K = 2

    +

    Input: strength = [2,5,4], k = 2

    Output: 5

    @@ -110,9 +110,9 @@ tags: Time Energy - X + x Action - Updated X + Updated x 0 diff --git a/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md b/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md index 7d7cfc892c769..9e0cd301083e2 100644 --- a/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md +++ b/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md +tags: + - 深度优先搜索 + - 图 + - 数组 --- diff --git a/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md b/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md index 1f2d6a75685a8..64bd2bb7a34b6 100644 --- a/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md +++ b/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md +tags: + - Depth-First Search + - Graph + - Array --- diff --git a/solution/3300-3399/3386.Button with Longest Push Time/README.md b/solution/3300-3399/3386.Button with Longest Push Time/README.md index b06cf09ca3845..341254ed130f6 100644 --- a/solution/3300-3399/3386.Button with Longest Push Time/README.md +++ b/solution/3300-3399/3386.Button with Longest Push Time/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README.md +tags: + - 数组 --- diff --git a/solution/3300-3399/3386.Button with Longest Push Time/README_EN.md b/solution/3300-3399/3386.Button with Longest Push Time/README_EN.md index 3a25ba1786b75..8ebc2a7d0de5e 100644 --- a/solution/3300-3399/3386.Button with Longest Push Time/README_EN.md +++ b/solution/3300-3399/3386.Button with Longest Push Time/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README_EN.md +tags: + - Array --- diff --git a/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README.md b/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README.md index 960841d6c22e0..d0c65953f2e38 100644 --- a/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README.md +++ b/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README.md +tags: + - 深度优先搜索 + - 广度优先搜索 + - 图 + - 数组 + - 字符串 --- diff --git a/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README_EN.md b/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README_EN.md index 2f839b4182cbb..3886a097ace8c 100644 --- a/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README_EN.md +++ b/solution/3300-3399/3387.Maximize Amount After Two Days of Conversions/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README_EN.md +tags: + - Depth-First Search + - Breadth-First Search + - Graph + - Array + - String --- diff --git a/solution/3300-3399/3388.Count Beautiful Splits in an Array/README.md b/solution/3300-3399/3388.Count Beautiful Splits in an Array/README.md index 6b0662bd06d3d..f824036dcbc38 100644 --- a/solution/3300-3399/3388.Count Beautiful Splits in an Array/README.md +++ b/solution/3300-3399/3388.Count Beautiful Splits in an Array/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README.md +tags: + - 数组 + - 动态规划 --- @@ -19,8 +22,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3388.Co

    如果数组 nums 的一个分割满足以下条件,我们称它是一个 美丽 分割:

      -
    1. 数组 nums 分为三段 非空子数组nums1 ,nums2 和 nums3 ,三个数组 nums1 ,nums2 和 nums3 按顺序连接可以得到 nums 。
    2. -
    3. 子数组 nums1 是子数组 nums2 的前缀 或者 nums2 是 nums3 的前缀。
    4. +
    5. 数组 nums 分为三段 非空子数组nums1 ,nums2 和 nums3 ,三个数组 nums1 ,nums2 和 nums3 按顺序连接可以得到 nums 。
    6. +
    7. 子数组 nums1 是子数组 nums2 的 前缀 或者 nums2 是 nums3 的 前缀

    请你返回满足以上条件的分割 数目 。

    diff --git a/solution/3300-3399/3388.Count Beautiful Splits in an Array/README_EN.md b/solution/3300-3399/3388.Count Beautiful Splits in an Array/README_EN.md index 92562c3606a5e..e4f3d20a9b36c 100644 --- a/solution/3300-3399/3388.Count Beautiful Splits in an Array/README_EN.md +++ b/solution/3300-3399/3388.Count Beautiful Splits in an Array/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README_EN.md +tags: + - Array + - Dynamic Programming --- diff --git a/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README.md b/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README.md index 47afde02be219..5b0de363c753a 100644 --- a/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README.md +++ b/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README.md +tags: + - 哈希表 + - 字符串 + - 动态规划 + - 计数 + - 枚举 --- diff --git a/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README_EN.md b/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README_EN.md index df08aed560173..90c7c91f8781f 100644 --- a/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README_EN.md +++ b/solution/3300-3399/3389.Minimum Operations to Make Character Frequencies Equal/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README_EN.md +tags: + - Hash Table + - String + - Dynamic Programming + - Counting + - Enumeration --- diff --git a/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README.md b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README.md new file mode 100644 index 0000000000000..73d1970b665e9 --- /dev/null +++ b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README.md @@ -0,0 +1,247 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README.md +--- + + + +# [3391. Design a 3D Binary Matrix with Efficient Layer Tracking 🔒](https://leetcode.cn/problems/design-a-3d-binary-matrix-with-efficient-layer-tracking) + +[English Version](/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README_EN.md) + +## 题目描述 + + + +

    You are given a n x n x n binary 3D array matrix.

    + +

    Implement the matrix3D class:

    + + + +

     

    +

    Example 1:

    + +
    +

    Input:
    +["matrix3D", "setCell", "largestMatrix", "setCell", "largestMatrix", "setCell", "largestMatrix"]
    +[[3], [0, 0, 0], [], [1, 1, 2], [], [0, 0, 1], []]

    + +

    Output:
    +[null, null, 0, null, 1, null, 0]

    + +

    Explanation

    +matrix3D matrix3D = new matrix3D(3); // Initializes a 3 x 3 x 3 3D array matrix, filled with all 0's.
    +matrix3D.setCell(0, 0, 0); // Sets matrix[0][0][0] to 1.
    +matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
    +matrix3D.setCell(1, 1, 2); // Sets matrix[1][1][2] to 1.
    +matrix3D.largestMatrix(); // Returns 1. matrix[0] and matrix[1] tie with the most number of 1's, but index 1 is bigger.
    +matrix3D.setCell(0, 0, 1); // Sets matrix[0][0][1] to 1.
    +matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
    + +

    Example 2:

    + +
    +

    Input:
    +["matrix3D", "setCell", "largestMatrix", "unsetCell", "largestMatrix"]
    +[[4], [2, 1, 1], [], [2, 1, 1], []]

    + +

    Output:
    +[null, null, 2, null, 3]

    + +

    Explanation

    +matrix3D matrix3D = new matrix3D(4); // Initializes a 4 x 4 x 4 3D array matrix, filled with all 0's.
    +matrix3D.setCell(2, 1, 1); // Sets matrix[2][1][1] to 1.
    +matrix3D.largestMatrix(); // Returns 2. matrix[2] has the most number of 1's.
    +matrix3D.unsetCell(2, 1, 1); // Sets matrix[2][1][1] to 0.
    +matrix3D.largestMatrix(); // Returns 3. All indices from 0 to 3 tie with the same number of 1's, but index 3 is the biggest.
    + +

     

    +

    Constraints:

    + + + + + +## 解法 + + + +### 方法一:计数 + 有序集合 + +我们使用一个三维数组 $\textit{g}$ 来表示矩阵,其中 $\textit{g}[x][y][z]$ 表示矩阵中坐标 $(x, y, z)$ 的值,用一个长度为 $n$ 的数组 $\textit{cnt}$ 来记录每一层的 $1$ 的个数,用一个有序集合 $\textit{sl}$ 来维护每一层的 $1$ 的个数和层数,其中 $\textit{sl}$ 中的元素是 $(\textit{cnt}[x], x)$,这样 $\textit{sl}$ 就能按照 $1$ 的个数降序排序,如果 $1$ 的个数相同,则按照层数降序排序。 + +调用 `setCell` 方法时,我们先判断 $(x, y, z)$ 是否已经被设置为 $1$,如果是则直接返回,否则将 $\textit{g}[x][y][z]$ 设置为 $1$,然后将 $(\textit{cnt}[x], x)$ 从 $\textit{sl}$ 中删除,将 $\textit{cnt}[x]$ 加一,再将 $(\textit{cnt}[x], x)$ 加入 $\textit{sl}$。 + +调用 `unsetCell` 方法时,我们先判断 $(x, y, z)$ 是否已经被设置为 $0$,如果是则直接返回,否则将 $\textit{g}[x][y][z]$ 设置为 $0$,然后将 $(\textit{cnt}[x], x)$ 从 $\textit{sl}$ 中删除,将 $\textit{cnt}[x]$ 减一,如果 $\textit{cnt}[x]$ 大于 $0$,则将 $(\textit{cnt}[x], x)$ 加入 $\textit{sl}$。 + +调用 `largestMatrix` 方法时,我们返回 $\textit{sl}$ 中第一个元素的第二个值,如果 $\textit{sl}$ 为空,则返回 $n - 1$。 + +时间复杂度方面,`setCell` 和 `unsetCell` 方法的时间复杂度均为 $O(\log n)$,`largestMatrix` 方法的时间复杂度为 $O(1)$。空间复杂度 $O(n^3)$。 + + + +#### Python3 + +```python +from sortedcontainers import SortedList + + +class matrix3D: + + def __init__(self, n: int): + self.g = [[[0] * n for _ in range(n)] for _ in range(n)] + self.cnt = [0] * n + self.sl = SortedList(key=lambda x: (-x[0], -x[1])) + + def setCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z]: + return + self.g[x][y][z] = 1 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] += 1 + self.sl.add((self.cnt[x], x)) + + def unsetCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z] == 0: + return + self.g[x][y][z] = 0 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] -= 1 + if self.cnt[x]: + self.sl.add((self.cnt[x], x)) + + def largestMatrix(self) -> int: + return self.sl[0][1] if self.sl else len(self.g) - 1 + + +# Your matrix3D object will be instantiated and called as such: +# obj = matrix3D(n) +# obj.setCell(x,y,z) +# obj.unsetCell(x,y,z) +# param_3 = obj.largestMatrix() +``` + +#### Java + +```java +class matrix3D { + private final int[][][] g; + private final int[] cnt; + private final TreeSet sl + = new TreeSet<>((a, b) -> a[0] == b[0] ? b[1] - a[1] : b[0] - a[0]); + + public matrix3D(int n) { + g = new int[n][n][n]; + cnt = new int[n]; + } + + public void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.remove(new int[] {cnt[x], x}); + cnt[x]++; + sl.add(new int[] {cnt[x], x}); + } + + public void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.remove(new int[] {cnt[x], x}); + cnt[x]--; + if (cnt[x] > 0) { + sl.add(new int[] {cnt[x], x}); + } + } + + public int largestMatrix() { + return sl.isEmpty() ? g.length - 1 : sl.first()[1]; + } +} + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D obj = new matrix3D(n); + * obj.setCell(x,y,z); + * obj.unsetCell(x,y,z); + * int param_3 = obj.largestMatrix(); + */ +``` + +#### C++ + +```cpp +class matrix3D { +private: + vector>> g; + vector cnt; + set> sl; + +public: + matrix3D(int n) { + g.resize(n, vector>(n, vector(n, 0))); + cnt.resize(n, 0); + } + + void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.erase({-cnt[x], -x}); + cnt[x]++; + sl.insert({-cnt[x], -x}); + } + + void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.erase({-cnt[x], -x}); + cnt[x]--; + if (cnt[x]) { + sl.insert({-cnt[x], -x}); + } + } + + int largestMatrix() { + return sl.empty() ? g.size() - 1 : -sl.begin()->second; + } +}; + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D* obj = new matrix3D(n); + * obj->setCell(x,y,z); + * obj->unsetCell(x,y,z); + * int param_3 = obj->largestMatrix(); + */ +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README_EN.md b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README_EN.md new file mode 100644 index 0000000000000..1c5a591d8dd62 --- /dev/null +++ b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/README_EN.md @@ -0,0 +1,247 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README_EN.md +--- + + + +# [3391. Design a 3D Binary Matrix with Efficient Layer Tracking 🔒](https://leetcode.com/problems/design-a-3d-binary-matrix-with-efficient-layer-tracking) + +[中文文档](/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README.md) + +## Description + + + +

    You are given a n x n x n binary 3D array matrix.

    + +

    Implement the matrix3D class:

    + +
      +
    • matrix3D(int n) Initializes the object with the 3D binary array matrix, where all elements are initially set to 0.
    • +
    • void setCell(int x, int y, int z) Sets the value at matrix[x][y][z] to 1.
    • +
    • void unsetCell(int x, int y, int z) Sets the value at matrix[x][y][z] to 0.
    • +
    • int largestMatrix() Returns the index x where matrix[x] contains the most number of 1's. If there are multiple such indices, return the largest x.
    • +
    + +

     

    +

    Example 1:

    + +
    +

    Input:
    +["matrix3D", "setCell", "largestMatrix", "setCell", "largestMatrix", "setCell", "largestMatrix"]
    +[[3], [0, 0, 0], [], [1, 1, 2], [], [0, 0, 1], []]

    + +

    Output:
    +[null, null, 0, null, 1, null, 0]

    + +

    Explanation

    +matrix3D matrix3D = new matrix3D(3); // Initializes a 3 x 3 x 3 3D array matrix, filled with all 0's.
    +matrix3D.setCell(0, 0, 0); // Sets matrix[0][0][0] to 1.
    +matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
    +matrix3D.setCell(1, 1, 2); // Sets matrix[1][1][2] to 1.
    +matrix3D.largestMatrix(); // Returns 1. matrix[0] and matrix[1] tie with the most number of 1's, but index 1 is bigger.
    +matrix3D.setCell(0, 0, 1); // Sets matrix[0][0][1] to 1.
    +matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
    + +

    Example 2:

    + +
    +

    Input:
    +["matrix3D", "setCell", "largestMatrix", "unsetCell", "largestMatrix"]
    +[[4], [2, 1, 1], [], [2, 1, 1], []]

    + +

    Output:
    +[null, null, 2, null, 3]

    + +

    Explanation

    +matrix3D matrix3D = new matrix3D(4); // Initializes a 4 x 4 x 4 3D array matrix, filled with all 0's.
    +matrix3D.setCell(2, 1, 1); // Sets matrix[2][1][1] to 1.
    +matrix3D.largestMatrix(); // Returns 2. matrix[2] has the most number of 1's.
    +matrix3D.unsetCell(2, 1, 1); // Sets matrix[2][1][1] to 0.
    +matrix3D.largestMatrix(); // Returns 3. All indices from 0 to 3 tie with the same number of 1's, but index 3 is the biggest.
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= n <= 100
    • +
    • 0 <= x, y, z < n
    • +
    • At most 105 calls are made in total to setCell and unsetCell.
    • +
    • At most 104 calls are made to largestMatrix.
    • +
    + + + +## Solutions + + + +### Solution 1: Counting + Ordered Set + +We use a three-dimensional array $\textit{g}$ to represent the matrix, where $\textit{g}[x][y][z]$ represents the value at coordinate $(x, y, z)$ in the matrix. We use an array $\textit{cnt}$ of length $n$ to record the number of 1s in each layer. We use an ordered set $\textit{sl}$ to maintain the number of 1s and the layer number for each layer. The elements in $\textit{sl}$ are $(\textit{cnt}[x], x)$, so $\textit{sl}$ can be sorted in descending order by the number of 1s, and in descending order by layer number if the number of 1s is the same. + +When calling the `setCell` method, we first check if $(x, y, z)$ has already been set to 1. If it has, we return directly. Otherwise, we set $\textit{g}[x][y][z]$ to 1, remove $(\textit{cnt}[x], x)$ from $\textit{sl}$, increment $\textit{cnt}[x]$ by 1, and add $(\textit{cnt}[x], x)$ to $\textit{sl}$. + +When calling the `unsetCell` method, we first check if $(x, y, z)$ has already been set to 0. If it has, we return directly. Otherwise, we set $\textit{g}[x][y][z]$ to 0, remove $(\textit{cnt}[x], x)$ from $\textit{sl}$, decrement $\textit{cnt}[x]$ by 1, and if $\textit{cnt}[x]$ is greater than 0, add $(\textit{cnt}[x], x)$ to $\textit{sl}$. + +When calling the `largestMatrix` method, we return the second value of the first element in $\textit{sl}$. If $\textit{sl}$ is empty, we return $n - 1$. + +In terms of time complexity, the `setCell` and `unsetCell` methods both have a time complexity of $O(\log n)$, and the `largestMatrix` method has a time complexity of $O(1)$. The space complexity is $O(n^3)$. + + + +#### Python3 + +```python +from sortedcontainers import SortedList + + +class matrix3D: + + def __init__(self, n: int): + self.g = [[[0] * n for _ in range(n)] for _ in range(n)] + self.cnt = [0] * n + self.sl = SortedList(key=lambda x: (-x[0], -x[1])) + + def setCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z]: + return + self.g[x][y][z] = 1 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] += 1 + self.sl.add((self.cnt[x], x)) + + def unsetCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z] == 0: + return + self.g[x][y][z] = 0 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] -= 1 + if self.cnt[x]: + self.sl.add((self.cnt[x], x)) + + def largestMatrix(self) -> int: + return self.sl[0][1] if self.sl else len(self.g) - 1 + + +# Your matrix3D object will be instantiated and called as such: +# obj = matrix3D(n) +# obj.setCell(x,y,z) +# obj.unsetCell(x,y,z) +# param_3 = obj.largestMatrix() +``` + +#### Java + +```java +class matrix3D { + private final int[][][] g; + private final int[] cnt; + private final TreeSet sl + = new TreeSet<>((a, b) -> a[0] == b[0] ? b[1] - a[1] : b[0] - a[0]); + + public matrix3D(int n) { + g = new int[n][n][n]; + cnt = new int[n]; + } + + public void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.remove(new int[] {cnt[x], x}); + cnt[x]++; + sl.add(new int[] {cnt[x], x}); + } + + public void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.remove(new int[] {cnt[x], x}); + cnt[x]--; + if (cnt[x] > 0) { + sl.add(new int[] {cnt[x], x}); + } + } + + public int largestMatrix() { + return sl.isEmpty() ? g.length - 1 : sl.first()[1]; + } +} + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D obj = new matrix3D(n); + * obj.setCell(x,y,z); + * obj.unsetCell(x,y,z); + * int param_3 = obj.largestMatrix(); + */ +``` + +#### C++ + +```cpp +class matrix3D { +private: + vector>> g; + vector cnt; + set> sl; + +public: + matrix3D(int n) { + g.resize(n, vector>(n, vector(n, 0))); + cnt.resize(n, 0); + } + + void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.erase({-cnt[x], -x}); + cnt[x]++; + sl.insert({-cnt[x], -x}); + } + + void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.erase({-cnt[x], -x}); + cnt[x]--; + if (cnt[x]) { + sl.insert({-cnt[x], -x}); + } + } + + int largestMatrix() { + return sl.empty() ? g.size() - 1 : -sl.begin()->second; + } +}; + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D* obj = new matrix3D(n); + * obj->setCell(x,y,z); + * obj->unsetCell(x,y,z); + * int param_3 = obj->largestMatrix(); + */ +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.cpp b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.cpp new file mode 100644 index 0000000000000..1cac3748a555e --- /dev/null +++ b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.cpp @@ -0,0 +1,46 @@ +class matrix3D { +private: + vector>> g; + vector cnt; + set> sl; + +public: + matrix3D(int n) { + g.resize(n, vector>(n, vector(n, 0))); + cnt.resize(n, 0); + } + + void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.erase({-cnt[x], -x}); + cnt[x]++; + sl.insert({-cnt[x], -x}); + } + + void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.erase({-cnt[x], -x}); + cnt[x]--; + if (cnt[x]) { + sl.insert({-cnt[x], -x}); + } + } + + int largestMatrix() { + return sl.empty() ? g.size() - 1 : -sl.begin()->second; + } +}; + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D* obj = new matrix3D(n); + * obj->setCell(x,y,z); + * obj->unsetCell(x,y,z); + * int param_3 = obj->largestMatrix(); + */ diff --git a/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.java b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.java new file mode 100644 index 0000000000000..4647f69771a5f --- /dev/null +++ b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.java @@ -0,0 +1,45 @@ +class matrix3D { + private final int[][][] g; + private final int[] cnt; + private final TreeSet sl + = new TreeSet<>((a, b) -> a[0] == b[0] ? b[1] - a[1] : b[0] - a[0]); + + public matrix3D(int n) { + g = new int[n][n][n]; + cnt = new int[n]; + } + + public void setCell(int x, int y, int z) { + if (g[x][y][z] == 1) { + return; + } + g[x][y][z] = 1; + sl.remove(new int[] {cnt[x], x}); + cnt[x]++; + sl.add(new int[] {cnt[x], x}); + } + + public void unsetCell(int x, int y, int z) { + if (g[x][y][z] == 0) { + return; + } + g[x][y][z] = 0; + sl.remove(new int[] {cnt[x], x}); + cnt[x]--; + if (cnt[x] > 0) { + sl.add(new int[] {cnt[x], x}); + } + } + + public int largestMatrix() { + return sl.isEmpty() ? g.length - 1 : sl.first()[1]; + } +} + +/** + * Your matrix3D object will be instantiated and called as such: + * matrix3D obj = new matrix3D(n); + * obj.setCell(x,y,z); + * obj.unsetCell(x,y,z); + * int param_3 = obj.largestMatrix(); + */ diff --git a/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.py b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.py new file mode 100644 index 0000000000000..cf25b09797800 --- /dev/null +++ b/solution/3300-3399/3391.Design a 3D Binary Matrix with Efficient Layer Tracking/Solution.py @@ -0,0 +1,35 @@ +from sortedcontainers import SortedList + + +class matrix3D: + def __init__(self, n: int): + self.g = [[[0] * n for _ in range(n)] for _ in range(n)] + self.cnt = [0] * n + self.sl = SortedList(key=lambda x: (-x[0], -x[1])) + + def setCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z]: + return + self.g[x][y][z] = 1 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] += 1 + self.sl.add((self.cnt[x], x)) + + def unsetCell(self, x: int, y: int, z: int) -> None: + if self.g[x][y][z] == 0: + return + self.g[x][y][z] = 0 + self.sl.discard((self.cnt[x], x)) + self.cnt[x] -= 1 + if self.cnt[x]: + self.sl.add((self.cnt[x], x)) + + def largestMatrix(self) -> int: + return self.sl[0][1] if self.sl else len(self.g) - 1 + + +# Your matrix3D object will be instantiated and called as such: +# obj = matrix3D(n) +# obj.setCell(x,y,z) +# obj.unsetCell(x,y,z) +# param_3 = obj.largestMatrix() diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 5e0dfeed41cb4..4f51b0f96d75b 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -304,7 +304,7 @@ | 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | `数据库` | 困难 | 🔒 | | 3374 | [首字母大写 II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) | `数据库` | 困难 | | | 3384 | [球队传球成功的优势得分](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README.md) | `数据库` | 困难 | 🔒 | -| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md) | | 困难 | 🔒 | +| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md) | `数据库` | 困难 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index a400f0bc068d8..32d4ee5da95ed 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -302,7 +302,7 @@ Press Control + F(or Command + F on | 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md) | `Database` | Hard | 🔒 | | 3374 | [First Letter Capitalization II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md) | `Database` | Hard | | | 3384 | [Team Dominance by Pass Success](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README_EN.md) | `Database` | Hard | 🔒 | -| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md) | | Hard | 🔒 | +| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md) | `Database` | Hard | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 2339e7fc55718..de98a00e37733 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3379,7 +3379,7 @@ | 3366 | [最小数组和](/solution/3300-3399/3366.Minimum%20Array%20Sum/README.md) | `数组`,`动态规划` | 中等 | 第 425 场周赛 | | 3367 | [移除边之后的权重最大和](/solution/3300-3399/3367.Maximize%20Sum%20of%20Weights%20after%20Edge%20Removals/README.md) | `树`,`深度优先搜索`,`动态规划` | 困难 | 第 425 场周赛 | | 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | `数据库` | 困难 | 🔒 | -| 3369 | [设计数组统计跟踪器](/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README.md) | `队列`,`哈希表`,`二分查找` | 困难 | 🔒 | +| 3369 | [设计数组统计跟踪器](/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README.md) | `设计`,`队列`,`哈希表`,`二分查找`,`数据流`,`有序集合`,`堆(优先队列)` | 困难 | 🔒 | | 3370 | [仅含置位位的最小整数](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README.md) | `位运算`,`数学` | 简单 | 第 426 场周赛 | | 3371 | [识别数组中的最大异常值](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README.md) | `数组`,`哈希表`,`计数`,`枚举` | 中等 | 第 426 场周赛 | | 3372 | [连接两棵树后最大目标节点数目 I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README.md) | `树`,`深度优先搜索`,`广度优先搜索` | 中等 | 第 426 场周赛 | @@ -3395,12 +3395,13 @@ | 3382 | [用点构造面积最大的矩形 II](/solution/3300-3399/3382.Maximum%20Area%20Rectangle%20With%20Point%20Constraints%20II/README.md) | `树状数组`,`线段树`,`几何`,`数组`,`数学`,`排序` | 困难 | 第 427 场周赛 | | 3383 | [施法所需最低符文数量](/solution/3300-3399/3383.Minimum%20Runes%20to%20Add%20to%20Cast%20Spell/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`拓扑排序`,`数组` | 困难 | 🔒 | | 3384 | [球队传球成功的优势得分](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README.md) | `数据库` | 困难 | 🔒 | -| 3385 | [破解锁的最少时间 II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md) | | 困难 | 🔒 | -| 3386 | [按下时间最长的按钮](/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README.md) | | 简单 | 第 428 场周赛 | -| 3387 | [两天自由外汇交易后的最大货币数](/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README.md) | | 中等 | 第 428 场周赛 | -| 3388 | [统计数组中的美丽分割](/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README.md) | | 中等 | 第 428 场周赛 | -| 3389 | [使字符频率相等的最少操作次数](/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README.md) | | 困难 | 第 428 场周赛 | -| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md) | | 困难 | 🔒 | +| 3385 | [破解锁的最少时间 II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md) | `深度优先搜索`,`图`,`数组` | 困难 | 🔒 | +| 3386 | [按下时间最长的按钮](/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README.md) | `数组` | 简单 | 第 428 场周赛 | +| 3387 | [两天自由外汇交易后的最大货币数](/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`数组`,`字符串` | 中等 | 第 428 场周赛 | +| 3388 | [统计数组中的美丽分割](/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README.md) | `数组`,`动态规划` | 中等 | 第 428 场周赛 | +| 3389 | [使字符频率相等的最少操作次数](/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README.md) | `哈希表`,`字符串`,`动态规划`,`计数`,`枚举` | 困难 | 第 428 场周赛 | +| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md) | `数据库` | 困难 | 🔒 | +| 3391 | [Design a 3D Binary Matrix with Efficient Layer Tracking](/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 0d7bd34be9b51..b6cba1e622b60 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3377,7 +3377,7 @@ Press Control + F(or Command + F on | 3366 | [Minimum Array Sum](/solution/3300-3399/3366.Minimum%20Array%20Sum/README_EN.md) | `Array`,`Dynamic Programming` | Medium | Weekly Contest 425 | | 3367 | [Maximize Sum of Weights after Edge Removals](/solution/3300-3399/3367.Maximize%20Sum%20of%20Weights%20after%20Edge%20Removals/README_EN.md) | `Tree`,`Depth-First Search`,`Dynamic Programming` | Hard | Weekly Contest 425 | | 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md) | `Database` | Hard | 🔒 | -| 3369 | [Design an Array Statistics Tracker](/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README_EN.md) | `Queue`,`Hash Table`,`Binary Search` | Hard | 🔒 | +| 3369 | [Design an Array Statistics Tracker](/solution/3300-3399/3369.Design%20an%20Array%20Statistics%20Tracker/README_EN.md) | `Design`,`Queue`,`Hash Table`,`Binary Search`,`Data Stream`,`Ordered Set`,`Heap (Priority Queue)` | Hard | 🔒 | | 3370 | [Smallest Number With All Set Bits](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README_EN.md) | `Bit Manipulation`,`Math` | Easy | Weekly Contest 426 | | 3371 | [Identify the Largest Outlier in an Array](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README_EN.md) | `Array`,`Hash Table`,`Counting`,`Enumeration` | Medium | Weekly Contest 426 | | 3372 | [Maximize the Number of Target Nodes After Connecting Trees I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README_EN.md) | `Tree`,`Depth-First Search`,`Breadth-First Search` | Medium | Weekly Contest 426 | @@ -3393,12 +3393,13 @@ Press Control + F(or Command + F on | 3382 | [Maximum Area Rectangle With Point Constraints II](/solution/3300-3399/3382.Maximum%20Area%20Rectangle%20With%20Point%20Constraints%20II/README_EN.md) | `Binary Indexed Tree`,`Segment Tree`,`Geometry`,`Array`,`Math`,`Sorting` | Hard | Weekly Contest 427 | | 3383 | [Minimum Runes to Add to Cast Spell](/solution/3300-3399/3383.Minimum%20Runes%20to%20Add%20to%20Cast%20Spell/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Topological Sort`,`Array` | Hard | 🔒 | | 3384 | [Team Dominance by Pass Success](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README_EN.md) | `Database` | Hard | 🔒 | -| 3385 | [Minimum Time to Break Locks II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md) | | Hard | 🔒 | -| 3386 | [Button with Longest Push Time](/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README_EN.md) | | Easy | Weekly Contest 428 | -| 3387 | [Maximize Amount After Two Days of Conversions](/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README_EN.md) | | Medium | Weekly Contest 428 | -| 3388 | [Count Beautiful Splits in an Array](/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README_EN.md) | | Medium | Weekly Contest 428 | -| 3389 | [Minimum Operations to Make Character Frequencies Equal](/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README_EN.md) | | Hard | Weekly Contest 428 | -| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md) | | Hard | 🔒 | +| 3385 | [Minimum Time to Break Locks II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md) | `Depth-First Search`,`Graph`,`Array` | Hard | 🔒 | +| 3386 | [Button with Longest Push Time](/solution/3300-3399/3386.Button%20with%20Longest%20Push%20Time/README_EN.md) | `Array` | Easy | Weekly Contest 428 | +| 3387 | [Maximize Amount After Two Days of Conversions](/solution/3300-3399/3387.Maximize%20Amount%20After%20Two%20Days%20of%20Conversions/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Array`,`String` | Medium | Weekly Contest 428 | +| 3388 | [Count Beautiful Splits in an Array](/solution/3300-3399/3388.Count%20Beautiful%20Splits%20in%20an%20Array/README_EN.md) | `Array`,`Dynamic Programming` | Medium | Weekly Contest 428 | +| 3389 | [Minimum Operations to Make Character Frequencies Equal](/solution/3300-3399/3389.Minimum%20Operations%20to%20Make%20Character%20Frequencies%20Equal/README_EN.md) | `Hash Table`,`String`,`Dynamic Programming`,`Counting`,`Enumeration` | Hard | Weekly Contest 428 | +| 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md) | `Database` | Hard | 🔒 | +| 3391 | [Design a 3D Binary Matrix with Efficient Layer Tracking](/solution/3300-3399/3391.Design%20a%203D%20Binary%20Matrix%20with%20Efficient%20Layer%20Tracking/README_EN.md) | | Medium | 🔒 | ## Copyright