|
10 | 10 |
|
11 | 11 | <ul>
|
12 | 12 | <li><code>int query(int a, int b, int c, int d)</code>:其中 <code>0 <= a < b < c < d < ArrayReader.length()</code> 。此函数查询以这四个参数为下标的元素并返回:
|
13 |
| - |
14 | 13 | <ul>
|
15 | 14 | <li><strong>4 </strong>: 当这四个元素相同(0 或 1)时。</li>
|
16 | 15 | <li><strong>2</strong> : 当其中三个元素的值等于 0 且一个元素等于 1 时,或当其中三个元素的值等于 1 且一个元素等于 0 时。</li>
|
17 | 16 | <li><strong>0 </strong>: 当其中两个元素等于 0 且两个元素等于 1 时。</li>
|
18 | 17 | </ul>
|
19 | 18 | </li>
|
20 | 19 | <li><code>int length()</code>:返回数组的长度。</li>
|
21 |
| - |
22 | 20 | </ul>
|
23 | 21 |
|
24 | 22 | <p>你可以调用 <code>query()</code> 最多 <strong>2 * n 次</strong>,其中 n 等于 <code>ArrayReader.length()</code>。</p>
|
@@ -72,22 +70,318 @@ reader.query(4,5,6,7) // 返回 4,因为 nums[4], nums[5], nums[6], nums[7]
|
72 | 70 |
|
73 | 71 | <!-- 这里可写通用的实现逻辑 -->
|
74 | 72 |
|
| 73 | +**方法一:脑筋急转弯** |
| 74 | + |
| 75 | +我们先调用 `reader.query(0, 1, 2, 3)`,将得到的结果记为 $x$。 |
| 76 | + |
| 77 | +接下来,我们从下标 $4$ 开始遍历,每次调用 `reader.query(0, 1, 2, i)`,如果结果与 $x$ 相同,我们就将 $a$ 的值加一,否则将 $b$ 的值加一,同时将 $k$ 的值更新为 $i$。 |
| 78 | + |
| 79 | +然后,我们还需要判断下标 $0, 1, 2$ 与下标 $3$ 的元素是否相同,如果相同,我们将 $a$ 的值加一,否则将 $b$ 的值加一,同时将 $k$ 的值更新为对应的下标。 |
| 80 | + |
| 81 | +最后,如果 $a=b$,说明数组中 $0$ 和 $1$ 的个数相同,我们返回 $-1$;否则,如果 $a \gt b$,返回 $3$,否则返回 $k$。 |
| 82 | + |
| 83 | +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 |
| 84 | + |
75 | 85 | <!-- tabs:start -->
|
76 | 86 |
|
77 | 87 | ### **Python3**
|
78 | 88 |
|
79 | 89 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
80 | 90 |
|
81 | 91 | ```python
|
82 |
| - |
| 92 | +# """ |
| 93 | +# This is the ArrayReader's API interface. |
| 94 | +# You should not implement it, or speculate about its implementation |
| 95 | +# """ |
| 96 | +# class ArrayReader(object): |
| 97 | +# # Compares 4 different elements in the array |
| 98 | +# # return 4 if the values of the 4 elements are the same (0 or 1). |
| 99 | +# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. |
| 100 | +# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. |
| 101 | +# def query(self, a: int, b: int, c: int, d: int) -> int: |
| 102 | +# |
| 103 | +# # Returns the length of the array |
| 104 | +# def length(self) -> int: |
| 105 | +# |
| 106 | + |
| 107 | + |
| 108 | +class Solution: |
| 109 | + def guessMajority(self, reader: "ArrayReader") -> int: |
| 110 | + n = reader.length() |
| 111 | + x = reader.query(0, 1, 2, 3) |
| 112 | + a, b = 1, 0 |
| 113 | + k = 0 |
| 114 | + for i in range(4, n): |
| 115 | + if reader.query(0, 1, 2, i) == x: |
| 116 | + a += 1 |
| 117 | + else: |
| 118 | + b += 1 |
| 119 | + k = i |
| 120 | + |
| 121 | + y = reader.query(0, 1, 2, 4) |
| 122 | + if reader.query(1, 2, 3, 4) == y: |
| 123 | + a += 1 |
| 124 | + else: |
| 125 | + b += 1 |
| 126 | + k = 0 |
| 127 | + if reader.query(0, 2, 3, 4) == y: |
| 128 | + a += 1 |
| 129 | + else: |
| 130 | + b += 1 |
| 131 | + k = 1 |
| 132 | + if reader.query(0, 1, 3, 4) == y: |
| 133 | + a += 1 |
| 134 | + else: |
| 135 | + b += 1 |
| 136 | + k = 2 |
| 137 | + |
| 138 | + if a == b: |
| 139 | + return -1 |
| 140 | + return 3 if a > b else k |
83 | 141 | ```
|
84 | 142 |
|
85 | 143 | ### **Java**
|
86 | 144 |
|
87 | 145 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
88 | 146 |
|
89 | 147 | ```java
|
| 148 | +/** |
| 149 | + * // This is the ArrayReader's API interface. |
| 150 | + * // You should not implement it, or speculate about its implementation |
| 151 | + * interface ArrayReader { |
| 152 | + * public: |
| 153 | + * // Compares 4 different elements in the array |
| 154 | + * // return 4 if the values of the 4 elements are the same (0 or 1). |
| 155 | + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. |
| 156 | + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. |
| 157 | + * public int query(int a, int b, int c, int d); |
| 158 | + * |
| 159 | + * // Returns the length of the array |
| 160 | + * public int length(); |
| 161 | + * }; |
| 162 | + */ |
| 163 | + |
| 164 | +class Solution { |
| 165 | + public int guessMajority(ArrayReader reader) { |
| 166 | + int n = reader.length(); |
| 167 | + int x = reader.query(0, 1, 2, 3); |
| 168 | + int a = 1, b = 0; |
| 169 | + int k = 0; |
| 170 | + for (int i = 4; i < n; ++i) { |
| 171 | + if (reader.query(0, 1, 2, i) == x) { |
| 172 | + ++a; |
| 173 | + } else { |
| 174 | + ++b; |
| 175 | + k = i; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + int y = reader.query(0, 1, 2, 4); |
| 180 | + if (reader.query(1, 2, 3, 4) == y) { |
| 181 | + ++a; |
| 182 | + } else { |
| 183 | + ++b; |
| 184 | + k = 0; |
| 185 | + } |
| 186 | + if (reader.query(0, 2, 3, 4) == y) { |
| 187 | + ++a; |
| 188 | + } else { |
| 189 | + ++b; |
| 190 | + k = 1; |
| 191 | + } |
| 192 | + if (reader.query(0, 1, 3, 4) == y) { |
| 193 | + ++a; |
| 194 | + } else { |
| 195 | + ++b; |
| 196 | + k = 2; |
| 197 | + } |
| 198 | + if (a == b) { |
| 199 | + return -1; |
| 200 | + } |
| 201 | + return a > b ? 3 : k; |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### **C++** |
| 207 | + |
| 208 | +```cpp |
| 209 | +/** |
| 210 | + * // This is the ArrayReader's API interface. |
| 211 | + * // You should not implement it, or speculate about its implementation |
| 212 | + * class ArrayReader { |
| 213 | + * public: |
| 214 | + * // Compares 4 different elements in the array |
| 215 | + * // return 4 if the values of the 4 elements are the same (0 or 1). |
| 216 | + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. |
| 217 | + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. |
| 218 | + * int query(int a, int b, int c, int d); |
| 219 | + * |
| 220 | + * // Returns the length of the array |
| 221 | + * int length(); |
| 222 | + * }; |
| 223 | + */ |
| 224 | + |
| 225 | +class Solution { |
| 226 | +public: |
| 227 | + int guessMajority(ArrayReader &reader) { |
| 228 | + int n = reader.length(); |
| 229 | + int x = reader.query(0, 1, 2, 3); |
| 230 | + int a = 1, b = 0; |
| 231 | + int k = 0; |
| 232 | + for (int i = 4; i < n; ++i) { |
| 233 | + if (reader.query(0, 1, 2, i) == x) { |
| 234 | + ++a; |
| 235 | + } else { |
| 236 | + ++b; |
| 237 | + k = i; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + int y = reader.query(0, 1, 2, 4); |
| 242 | + if (reader.query(1, 2, 3, 4) == y) { |
| 243 | + ++a; |
| 244 | + } else { |
| 245 | + ++b; |
| 246 | + k = 0; |
| 247 | + } |
| 248 | + if (reader.query(0, 2, 3, 4) == y) { |
| 249 | + ++a; |
| 250 | + } else { |
| 251 | + ++b; |
| 252 | + k = 1; |
| 253 | + } |
| 254 | + if (reader.query(0, 1, 3, 4) == y) { |
| 255 | + ++a; |
| 256 | + } else { |
| 257 | + ++b; |
| 258 | + k = 2; |
| 259 | + } |
| 260 | + if (a == b) { |
| 261 | + return -1; |
| 262 | + } |
| 263 | + return a > b ? 3 : k; |
| 264 | + } |
| 265 | +}; |
| 266 | +``` |
| 267 | + |
| 268 | +### **Go** |
| 269 | + |
| 270 | +```go |
| 271 | +/** |
| 272 | + * // This is the ArrayReader's API interface. |
| 273 | + * // You should not implement it, or speculate about its implementation |
| 274 | + * type ArrayReader struct { |
| 275 | + * } |
| 276 | + * // Compares 4 different elements in the array |
| 277 | + * // return 4 if the values of the 4 elements are the same (0 or 1). |
| 278 | + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. |
| 279 | + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. |
| 280 | + * func (this *ArrayReader) query(a, b, c, d int) int {} |
| 281 | + * |
| 282 | + * // Returns the length of the array |
| 283 | + * func (this *ArrayReader) length() int {} |
| 284 | + */ |
| 285 | + |
| 286 | +func guessMajority(reader *ArrayReader) int { |
| 287 | + n := reader.length() |
| 288 | + x := reader.query(0, 1, 2, 3) |
| 289 | + a, b := 1, 0 |
| 290 | + k := 0 |
| 291 | + for i := 4; i < n; i++ { |
| 292 | + if reader.query(0, 1, 2, i) == x { |
| 293 | + a++ |
| 294 | + } else { |
| 295 | + b++ |
| 296 | + k = i |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + y := reader.query(0, 1, 2, 4) |
| 301 | + if reader.query(1, 2, 3, 4) == y { |
| 302 | + a++ |
| 303 | + } else { |
| 304 | + b++ |
| 305 | + k = 0 |
| 306 | + } |
| 307 | + if reader.query(0, 2, 3, 4) == y { |
| 308 | + a++ |
| 309 | + } else { |
| 310 | + b++ |
| 311 | + k = 1 |
| 312 | + } |
| 313 | + if reader.query(0, 1, 3, 4) == y { |
| 314 | + a++ |
| 315 | + } else { |
| 316 | + b++ |
| 317 | + k = 2 |
| 318 | + } |
| 319 | + if a == b { |
| 320 | + return -1 |
| 321 | + } |
| 322 | + if a > b { |
| 323 | + return 3 |
| 324 | + } |
| 325 | + return k |
| 326 | +} |
| 327 | +``` |
90 | 328 |
|
| 329 | +### **TypeScript** |
| 330 | + |
| 331 | +```ts |
| 332 | +/** |
| 333 | + * // This is the ArrayReader's API interface. |
| 334 | + * // You should not implement it, or speculate about its implementation |
| 335 | + * class ArrayReader { |
| 336 | + * // Compares 4 different elements in the array |
| 337 | + * // return 4 if the values of the 4 elements are the same (0 or 1). |
| 338 | + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. |
| 339 | + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. |
| 340 | + * query(a: number, b: number, c: number, d: number): number { }; |
| 341 | + * |
| 342 | + * // Returns the length of the array |
| 343 | + * length(): number { }; |
| 344 | + * }; |
| 345 | + */ |
| 346 | + |
| 347 | +function guessMajority(reader: ArrayReader): number { |
| 348 | + const n = reader.length(); |
| 349 | + const x = reader.query(0, 1, 2, 3); |
| 350 | + let a = 1; |
| 351 | + let b = 0; |
| 352 | + let k = 0; |
| 353 | + for (let i = 4; i < n; ++i) { |
| 354 | + if (reader.query(0, 1, 2, i) === x) { |
| 355 | + ++a; |
| 356 | + } else { |
| 357 | + ++b; |
| 358 | + k = i; |
| 359 | + } |
| 360 | + } |
| 361 | + const y = reader.query(0, 1, 2, 4); |
| 362 | + if (reader.query(1, 2, 3, 4) === y) { |
| 363 | + ++a; |
| 364 | + } else { |
| 365 | + ++b; |
| 366 | + k = 0; |
| 367 | + } |
| 368 | + if (reader.query(0, 2, 3, 4) === y) { |
| 369 | + ++a; |
| 370 | + } else { |
| 371 | + ++b; |
| 372 | + k = 1; |
| 373 | + } |
| 374 | + if (reader.query(0, 1, 3, 4) === y) { |
| 375 | + ++a; |
| 376 | + } else { |
| 377 | + ++b; |
| 378 | + k = 2; |
| 379 | + } |
| 380 | + if (a === b) { |
| 381 | + return -1; |
| 382 | + } |
| 383 | + return a > b ? 3 : k; |
| 384 | +} |
91 | 385 | ```
|
92 | 386 |
|
93 | 387 | ### **...**
|
|
0 commit comments