|
46 | 46 |
|
47 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 48 |
|
| 49 | +**方法一:哈希表计数** |
| 50 | + |
| 51 | +我们可以用哈希表 $cnt$ 统计 $source$ 和 $target$ 中每个数字出现的次数之差。对于 $cnt$ 中的每个数字 $x$,如果 $x$ 的出现次数为正数,那么说明 $x$ 在 $target$ 中出现的次数多,我们需要将 $x$ 出现的次数减少到 $0$。因此,我们只需要累加所有出现次数为正数的数字的出现次数之和,即为答案。 |
| 52 | + |
| 53 | +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是 $source$ 和 $target$ 的行数和列数。 |
| 54 | + |
49 | 55 | <!-- tabs:start -->
|
50 | 56 |
|
51 | 57 | ### **Python3**
|
52 | 58 |
|
53 | 59 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 60 |
|
55 | 61 | ```python
|
56 |
| - |
| 62 | +class Solution: |
| 63 | + def minimumSwitchingTimes( |
| 64 | + self, source: List[List[int]], target: List[List[int]] |
| 65 | + ) -> int: |
| 66 | + cnt = Counter() |
| 67 | + for row in source: |
| 68 | + for x in row: |
| 69 | + cnt[x] += 1 |
| 70 | + for row in target: |
| 71 | + for x in row: |
| 72 | + cnt[x] -= 1 |
| 73 | + return sum(abs(x) for x in cnt.values()) // 2 |
57 | 74 | ```
|
58 | 75 |
|
59 | 76 | ### **Java**
|
60 | 77 |
|
61 | 78 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 79 |
|
63 | 80 | ```java
|
| 81 | +class Solution { |
| 82 | + public int minimumSwitchingTimes(int[][] source, int[][] target) { |
| 83 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 84 | + for (int[] row : source) { |
| 85 | + for (int x : row) { |
| 86 | + cnt.merge(x, 1, Integer::sum); |
| 87 | + } |
| 88 | + } |
| 89 | + for (int[] row : target) { |
| 90 | + for (int x : row) { |
| 91 | + cnt.merge(x, -1, Integer::sum); |
| 92 | + } |
| 93 | + } |
| 94 | + int ans = 0; |
| 95 | + for (int v : cnt.values()) { |
| 96 | + ans += Math.abs(v); |
| 97 | + } |
| 98 | + return ans / 2; |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### **C++** |
| 104 | + |
| 105 | +```cpp |
| 106 | +class Solution { |
| 107 | +public: |
| 108 | + int minimumSwitchingTimes(vector<vector<int>>& source, vector<vector<int>>& target) { |
| 109 | + unordered_map<int, int> cnt; |
| 110 | + for (auto& row : source) { |
| 111 | + for (int& x : row) { |
| 112 | + ++cnt[x]; |
| 113 | + } |
| 114 | + } |
| 115 | + for (auto& row : target) { |
| 116 | + for (int& x : row) { |
| 117 | + --cnt[x]; |
| 118 | + } |
| 119 | + } |
| 120 | + int ans = 0; |
| 121 | + for (auto& [_, v] : cnt) { |
| 122 | + ans += abs(v); |
| 123 | + } |
| 124 | + return ans / 2; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
| 128 | +
|
| 129 | +### **Go** |
| 130 | +
|
| 131 | +```go |
| 132 | +func minimumSwitchingTimes(source [][]int, target [][]int) (ans int) { |
| 133 | + cnt := map[int]int{} |
| 134 | + for _, row := range source { |
| 135 | + for _, x := range row { |
| 136 | + cnt[x]++ |
| 137 | + } |
| 138 | + } |
| 139 | + for _, row := range target { |
| 140 | + for _, x := range row { |
| 141 | + cnt[x]-- |
| 142 | + } |
| 143 | + } |
| 144 | + for _, v := range cnt { |
| 145 | + if v > 0 { |
| 146 | + ans += v |
| 147 | + } |
| 148 | + } |
| 149 | + return |
| 150 | +} |
| 151 | +``` |
64 | 152 |
|
| 153 | +### **TypeScript** |
| 154 | + |
| 155 | +```ts |
| 156 | +function minimumSwitchingTimes(source: number[][], target: number[][]): number { |
| 157 | + const cnt: Map<number, number> = new Map(); |
| 158 | + for (const row of source) { |
| 159 | + for (const x of row) { |
| 160 | + cnt.set(x, (cnt.get(x) || 0) + 1); |
| 161 | + } |
| 162 | + } |
| 163 | + for (const row of target) { |
| 164 | + for (const x of row) { |
| 165 | + cnt.set(x, (cnt.get(x) || 0) - 1); |
| 166 | + } |
| 167 | + } |
| 168 | + let ans = 0; |
| 169 | + for (const [_, v] of cnt) { |
| 170 | + ans += Math.abs(v); |
| 171 | + } |
| 172 | + return Math.floor(ans / 2); |
| 173 | +} |
65 | 174 | ```
|
66 | 175 |
|
67 | 176 | ### **...**
|
|
0 commit comments