diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README.md b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README.md index 0b2f5ad4dbb23..1bdf9e7616d7f 100644 --- a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README.md +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README.md @@ -10,7 +10,6 @@

你可以调用 query() 最多 2 * n 次,其中 n 等于 ArrayReader.length()

@@ -72,6 +70,18 @@ reader.query(4,5,6,7) // 返回 4,因为 nums[4], nums[5], nums[6], nums[7] +**方法一:脑筋急转弯** + +我们先调用 `reader.query(0, 1, 2, 3)`,将得到的结果记为 $x$。 + +接下来,我们从下标 $4$ 开始遍历,每次调用 `reader.query(0, 1, 2, i)`,如果结果与 $x$ 相同,我们就将 $a$ 的值加一,否则将 $b$ 的值加一,同时将 $k$ 的值更新为 $i$。 + +然后,我们还需要判断下标 $0, 1, 2$ 与下标 $3$ 的元素是否相同,如果相同,我们将 $a$ 的值加一,否则将 $b$ 的值加一,同时将 $k$ 的值更新为对应的下标。 + +最后,如果 $a=b$,说明数组中 $0$ 和 $1$ 的个数相同,我们返回 $-1$;否则,如果 $a \gt b$,返回 $3$,否则返回 $k$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -79,7 +89,55 @@ reader.query(4,5,6,7) // 返回 4,因为 nums[4], nums[5], nums[6], nums[7] ```python - +# """ +# This is the ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +# class ArrayReader(object): +# # Compares 4 different elements in the array +# # return 4 if the values of the 4 elements are the same (0 or 1). +# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. +# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. +# def query(self, a: int, b: int, c: int, d: int) -> int: +# +# # Returns the length of the array +# def length(self) -> int: +# + + +class Solution: + def guessMajority(self, reader: "ArrayReader") -> int: + n = reader.length() + x = reader.query(0, 1, 2, 3) + a, b = 1, 0 + k = 0 + for i in range(4, n): + if reader.query(0, 1, 2, i) == x: + a += 1 + else: + b += 1 + k = i + + y = reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 0 + if reader.query(0, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 1 + if reader.query(0, 1, 3, 4) == y: + a += 1 + else: + b += 1 + k = 2 + + if a == b: + return -1 + return 3 if a > b else k ``` ### **Java** @@ -87,7 +145,243 @@ reader.query(4,5,6,7) // 返回 4,因为 nums[4], nums[5], nums[6], nums[7] ```java +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * public int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * public int length(); + * }; + */ + +class Solution { + public int guessMajority(ArrayReader reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +} +``` + +### **C++** + +```cpp +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * int length(); + * }; + */ + +class Solution { +public: + int guessMajority(ArrayReader &reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +}; +``` + +### **Go** + +```go +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * type ArrayReader struct { + * } + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * func (this *ArrayReader) query(a, b, c, d int) int {} + * + * // Returns the length of the array + * func (this *ArrayReader) length() int {} + */ + +func guessMajority(reader *ArrayReader) int { + n := reader.length() + x := reader.query(0, 1, 2, 3) + a, b := 1, 0 + k := 0 + for i := 4; i < n; i++ { + if reader.query(0, 1, 2, i) == x { + a++ + } else { + b++ + k = i + } + } + + y := reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y { + a++ + } else { + b++ + k = 0 + } + if reader.query(0, 2, 3, 4) == y { + a++ + } else { + b++ + k = 1 + } + if reader.query(0, 1, 3, 4) == y { + a++ + } else { + b++ + k = 2 + } + if a == b { + return -1 + } + if a > b { + return 3 + } + return k +} +``` +### **TypeScript** + +```ts +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * query(a: number, b: number, c: number, d: number): number { }; + * + * // Returns the length of the array + * length(): number { }; + * }; + */ + +function guessMajority(reader: ArrayReader): number { + const n = reader.length(); + const x = reader.query(0, 1, 2, 3); + let a = 1; + let b = 0; + let k = 0; + for (let i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) === x) { + ++a; + } else { + ++b; + k = i; + } + } + const y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) === y) { + ++a; + } else { + ++b; + k = 2; + } + if (a === b) { + return -1; + } + return a > b ? 3 : k; +} ``` ### **...** diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README_EN.md b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README_EN.md index 6446d8f7f3fd1..9fc0b5779c1d4 100644 --- a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README_EN.md +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/README_EN.md @@ -8,7 +8,6 @@

You are allowed to call query() 2 * n times at most where n is equal to ArrayReader.length().

@@ -70,13 +68,297 @@ Index 2, 4, 6, 7 is also a correct answer. ### **Python3** ```python +# """ +# This is the ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +# class ArrayReader(object): +# # Compares 4 different elements in the array +# # return 4 if the values of the 4 elements are the same (0 or 1). +# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. +# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. +# def query(self, a: int, b: int, c: int, d: int) -> int: +# +# # Returns the length of the array +# def length(self) -> int: +# + + +class Solution: + def guessMajority(self, reader: "ArrayReader") -> int: + n = reader.length() + x = reader.query(0, 1, 2, 3) + a, b = 1, 0 + k = 0 + for i in range(4, n): + if reader.query(0, 1, 2, i) == x: + a += 1 + else: + b += 1 + k = i + + y = reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 0 + if reader.query(0, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 1 + if reader.query(0, 1, 3, 4) == y: + a += 1 + else: + b += 1 + k = 2 + if a == b: + return -1 + return 3 if a > b else k ``` ### **Java** ```java +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * public int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * public int length(); + * }; + */ + +class Solution { + public int guessMajority(ArrayReader reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +} +``` + +### **C++** + +```cpp +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * int length(); + * }; + */ + +class Solution { +public: + int guessMajority(ArrayReader &reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +}; +``` + +### **Go** + +```go +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * type ArrayReader struct { + * } + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * func (this *ArrayReader) query(a, b, c, d int) int {} + * + * // Returns the length of the array + * func (this *ArrayReader) length() int {} + */ + +func guessMajority(reader *ArrayReader) int { + n := reader.length() + x := reader.query(0, 1, 2, 3) + a, b := 1, 0 + k := 0 + for i := 4; i < n; i++ { + if reader.query(0, 1, 2, i) == x { + a++ + } else { + b++ + k = i + } + } + + y := reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y { + a++ + } else { + b++ + k = 0 + } + if reader.query(0, 2, 3, 4) == y { + a++ + } else { + b++ + k = 1 + } + if reader.query(0, 1, 3, 4) == y { + a++ + } else { + b++ + k = 2 + } + if a == b { + return -1 + } + if a > b { + return 3 + } + return k +} +``` + +### **TypeScript** + +```ts +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * query(a: number, b: number, c: number, d: number): number { }; + * + * // Returns the length of the array + * length(): number { }; + * }; + */ +function guessMajority(reader: ArrayReader): number { + const n = reader.length(); + const x = reader.query(0, 1, 2, 3); + let a = 1; + let b = 0; + let k = 0; + for (let i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) === x) { + ++a; + } else { + ++b; + k = i; + } + } + const y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) === y) { + ++a; + } else { + ++b; + k = 2; + } + if (a === b) { + return -1; + } + return a > b ? 3 : k; +} ``` ### **...** diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.cpp b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.cpp new file mode 100644 index 0000000000000..b8c8ce0476e4e --- /dev/null +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.cpp @@ -0,0 +1,57 @@ +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * int length(); + * }; + */ + +class Solution { +public: + int guessMajority(ArrayReader &reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +}; \ No newline at end of file diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go new file mode 100644 index 0000000000000..113480adfd98a --- /dev/null +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go @@ -0,0 +1,56 @@ +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * type ArrayReader struct { + * } + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * func (this *ArrayReader) query(a, b, c, d int) int {} + * + * // Returns the length of the array + * func (this *ArrayReader) length() int {} + */ + +func guessMajority(reader *ArrayReader) int { + n := reader.length() + x := reader.query(0, 1, 2, 3) + a, b := 1, 0 + k := 0 + for i := 4; i < n; i++ { + if reader.query(0, 1, 2, i) == x { + a++ + } else { + b++ + k = i + } + } + + y := reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y { + a++ + } else { + b++ + k = 0 + } + if reader.query(0, 2, 3, 4) == y { + a++ + } else { + b++ + k = 1 + } + if reader.query(0, 1, 3, 4) == y { + a++ + } else { + b++ + k = 2 + } + if a == b { + return -1 + } + if a > b { + return 3 + } + return k +} \ No newline at end of file diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.java b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.java new file mode 100644 index 0000000000000..bce678334e465 --- /dev/null +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.java @@ -0,0 +1,56 @@ +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public: + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * public int query(int a, int b, int c, int d); + * + * // Returns the length of the array + * public int length(); + * }; + */ + +class Solution { + public int guessMajority(ArrayReader reader) { + int n = reader.length(); + int x = reader.query(0, 1, 2, 3); + int a = 1, b = 0; + int k = 0; + for (int i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) == x) { + ++a; + } else { + ++b; + k = i; + } + } + + int y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) == y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) == y) { + ++a; + } else { + ++b; + k = 2; + } + if (a == b) { + return -1; + } + return a > b ? 3 : k; + } +} \ No newline at end of file diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.py b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.py new file mode 100644 index 0000000000000..7a070bf3ff478 --- /dev/null +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.py @@ -0,0 +1,49 @@ +# """ +# This is the ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +# class ArrayReader(object): +# # Compares 4 different elements in the array +# # return 4 if the values of the 4 elements are the same (0 or 1). +# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. +# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. +# def query(self, a: int, b: int, c: int, d: int) -> int: +# +# # Returns the length of the array +# def length(self) -> int: +# + + +class Solution: + def guessMajority(self, reader: "ArrayReader") -> int: + n = reader.length() + x = reader.query(0, 1, 2, 3) + a, b = 1, 0 + k = 0 + for i in range(4, n): + if reader.query(0, 1, 2, i) == x: + a += 1 + else: + b += 1 + k = i + + y = reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 0 + if reader.query(0, 2, 3, 4) == y: + a += 1 + else: + b += 1 + k = 1 + if reader.query(0, 1, 3, 4) == y: + a += 1 + else: + b += 1 + k = 2 + + if a == b: + return -1 + return 3 if a > b else k diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.ts b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.ts new file mode 100644 index 0000000000000..1ea0329025b89 --- /dev/null +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.ts @@ -0,0 +1,53 @@ +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * class ArrayReader { + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * query(a: number, b: number, c: number, d: number): number { }; + * + * // Returns the length of the array + * length(): number { }; + * }; + */ + +function guessMajority(reader: ArrayReader): number { + const n = reader.length(); + const x = reader.query(0, 1, 2, 3); + let a = 1; + let b = 0; + let k = 0; + for (let i = 4; i < n; ++i) { + if (reader.query(0, 1, 2, i) === x) { + ++a; + } else { + ++b; + k = i; + } + } + const y = reader.query(0, 1, 2, 4); + if (reader.query(1, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 0; + } + if (reader.query(0, 2, 3, 4) === y) { + ++a; + } else { + ++b; + k = 1; + } + if (reader.query(0, 1, 3, 4) === y) { + ++a; + } else { + ++b; + k = 2; + } + if (a === b) { + return -1; + } + return a > b ? 3 : k; +} diff --git a/solution/config.py b/solution/config.py index 23cd460724f74..91cbf29c6f9db 100644 --- a/solution/config.py +++ b/solution/config.py @@ -36,6 +36,7 @@ 1266, 1396, 1511, + 1538, 1555, 1565, 1599,