diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" index 61a7f927c2a86..1aaad5f77f5cc 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" @@ -198,6 +198,29 @@ class Solution { } ``` +### **C** + +```c +int pivotIndex(int* nums, int numsSize) { + int left, right; + left = 0; + right = 0; + + for (int i = 0; i < numsSize; i++) { + right += nums[i]; + } + + for (int i = 0; i < numsSize; i++) { + right -= nums[i]; + if (right == left) + return i; + left += nums[i]; + } + + return -1; +} +``` + ### **...** ``` diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" new file mode 100644 index 0000000000000..b655283a1d610 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" @@ -0,0 +1,18 @@ +int pivotIndex(int* nums, int numsSize) { + int left, right; + left = 0; + right = 0; + + for (int i = 0; i < numsSize; i++) { + right += nums[i]; + } + + for (int i = 0; i < numsSize; i++) { + right -= nums[i]; + if (right == left) + return i; + left += nums[i]; + } + + return -1; +} \ No newline at end of file diff --git a/solution/0400-0499/0447.Number of Boomerangs/README.md b/solution/0400-0499/0447.Number of Boomerangs/README.md index 4ef4fe7141c00..41fda5e321e0e 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/README.md +++ b/solution/0400-0499/0447.Number of Boomerangs/README.md @@ -49,9 +49,13 @@ -计数器实现。 +**方法一:枚举 + 计数** -对于每个点,计算其他点到该点的距离,然后按照距离进行分组计数。对每个组中的点进行两两排列组合(A n 取 2,即 `n * (n - 1))`)计数即可。 +我们可以枚举 `points` 中的每个点作为回旋镖的点 $i$,然后用一个哈希表 $cnt$ 记录其他点到 $i$ 的距离出现的次数。 + +如果有 $x$ 个点到 $i$ 的距离相等,那么我们可以任选其中 $2$ 个点作为回旋镖的 $j$ 和 $k$,方案数为 $A_x^2 = x \times (x - 1)$。因此,我们对哈希表中的每个值 $x$,都计算并累加 $A_x^2$,就可以得到满足题目要求的回旋镖数量之和。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `points` 的长度。 @@ -63,12 +67,25 @@ class Solution: def numberOfBoomerangs(self, points: List[List[int]]) -> int: ans = 0 - for p in points: - counter = Counter() - for q in points: - distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) - counter[distance] += 1 - ans += sum([val * (val - 1) for val in counter.values()]) + for p1 in points: + cnt = Counter() + for p2 in points: + d = dist(p1, p2) + ans += cnt[d] + cnt[d] += 1 + return ans << 1 +``` + +```python +class Solution: + def numberOfBoomerangs(self, points: List[List[int]]) -> int: + ans = 0 + for p1 in points: + cnt = Counter() + for p2 in points: + d = dist(p1, p2) + cnt[d] += 1 + ans += sum(x * (x - 1) for x in cnt.values()) return ans ``` @@ -80,72 +97,71 @@ class Solution: class Solution { public int numberOfBoomerangs(int[][] points) { int ans = 0; - for (int[] p : points) { - Map counter = new HashMap<>(); - for (int[] q : points) { - int distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]); - counter.put(distance, counter.getOrDefault(distance, 0) + 1); - } - for (int val : counter.values()) { - ans += val * (val - 1); + for (int[] p1 : points) { + Map cnt = new HashMap<>(); + for (int[] p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + ans += cnt.getOrDefault(d, 0); + cnt.merge(d, 1, Integer::sum); } } - return ans; + return ans << 1; } } ``` -### **TypeScript** - -```ts -function numberOfBoomerangs(points: number[][]): number { - let ans = 0; - for (let p1 of points) { - let hashMap: Map = new Map(); - for (let p2 of points) { - const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; - hashMap.set(distance, (hashMap.get(distance) || 0) + 1); - } - for (let [, v] of [...hashMap]) { - ans += v * (v - 1); +```java +class Solution { + public int numberOfBoomerangs(int[][] points) { + int ans = 0; + for (int[] p1 : points) { + Map cnt = new HashMap<>(); + for (int[] p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt.merge(d, 1, Integer::sum); + } + for (int x : cnt.values()) { + ans += x * (x - 1); + } } + return ans; } - return ans; } ``` -### **Go** +### **C++** -```go -func numberOfBoomerangs(points [][]int) int { - ans := 0 - for _, p := range points { - cnt := make(map[int]int) - for _, q := range points { - cnt[(p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1])]++ - } - for _, v := range cnt { - ans += v * (v - 1) - } - } - return ans -} +```cpp +class Solution { +public: + int numberOfBoomerangs(vector>& points) { + int ans = 0; + for (auto& p1 : points) { + unordered_map cnt; + for (auto& p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + ans += cnt[d]; + cnt[d]++; + } + } + return ans << 1; + } +}; ``` -### **C++** - ```cpp class Solution { public: int numberOfBoomerangs(vector>& points) { int ans = 0; - for (const auto& p : points) { + for (auto& p1 : points) { unordered_map cnt; - for (const auto& q : points) { - ++cnt[(p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])]; + for (auto& p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt[d]++; } - for (const auto& [_, v] : cnt) { - ans += v * (v - 1); + for (auto& [_, x] : cnt) { + ans += x * (x - 1); } } return ans; @@ -153,6 +169,73 @@ public: }; ``` +### **Go** + +```go +func numberOfBoomerangs(points [][]int) (ans int) { + for _, p1 := range points { + cnt := map[int]int{} + for _, p2 := range points { + d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]) + ans += cnt[d] + cnt[d]++ + } + } + ans <<= 1 + return +} +``` + +```go +func numberOfBoomerangs(points [][]int) (ans int) { + for _, p1 := range points { + cnt := map[int]int{} + for _, p2 := range points { + d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]) + cnt[d]++ + } + for _, x := range cnt { + ans += x * (x - 1) + } + } + return +} +``` + +### **TypeScript** + +```ts +function numberOfBoomerangs(points: number[][]): number { + let ans = 0; + for (const [x1, y1] of points) { + const cnt: Map = new Map(); + for (const [x2, y2] of points) { + const d = (x1 - x2) ** 2 + (y1 - y2) ** 2; + ans += cnt.get(d) || 0; + cnt.set(d, (cnt.get(d) || 0) + 1); + } + } + return ans << 1; +} +``` + +```ts +function numberOfBoomerangs(points: number[][]): number { + let ans = 0; + for (const [x1, y1] of points) { + const cnt: Map = new Map(); + for (const [x2, y2] of points) { + const d = (x1 - x2) ** 2 + (y1 - y2) ** 2; + cnt.set(d, (cnt.get(d) || 0) + 1); + } + for (const [_, x] of cnt) { + ans += x * (x - 1); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0447.Number of Boomerangs/README_EN.md b/solution/0400-0499/0447.Number of Boomerangs/README_EN.md index edfd91250fb8d..10f1d0d2bbee2 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/README_EN.md +++ b/solution/0400-0499/0447.Number of Boomerangs/README_EN.md @@ -44,6 +44,14 @@ ## Solutions +**Solution 1: Enumeration + Counting** + +We can enumerate each point in `points` as the boomerang's point $i$, and then use a hash table $cnt$ to record the number of times the distance from other points to $i$ appears. + +If there are $x$ points with equal distance to $i$, then we can arbitrarily select two of them as the boomerang's $j$ and $k$. The number of schemes is $A_x^2 = x \times (x - 1)$. Therefore, for each value $x$ in the hash table, we calculate and accumulate $A_x^2$, which gives us the total number of boomerangs that meet the problem's requirements. + +The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the array `points`. + ### **Python3** @@ -52,12 +60,25 @@ class Solution: def numberOfBoomerangs(self, points: List[List[int]]) -> int: ans = 0 - for p in points: - counter = Counter() - for q in points: - distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) - counter[distance] += 1 - ans += sum([val * (val - 1) for val in counter.values()]) + for p1 in points: + cnt = Counter() + for p2 in points: + d = dist(p1, p2) + ans += cnt[d] + cnt[d] += 1 + return ans << 1 +``` + +```python +class Solution: + def numberOfBoomerangs(self, points: List[List[int]]) -> int: + ans = 0 + for p1 in points: + cnt = Counter() + for p2 in points: + d = dist(p1, p2) + cnt[d] += 1 + ans += sum(x * (x - 1) for x in cnt.values()) return ans ``` @@ -67,72 +88,71 @@ class Solution: class Solution { public int numberOfBoomerangs(int[][] points) { int ans = 0; - for (int[] p : points) { - Map counter = new HashMap<>(); - for (int[] q : points) { - int distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]); - counter.put(distance, counter.getOrDefault(distance, 0) + 1); - } - for (int val : counter.values()) { - ans += val * (val - 1); + for (int[] p1 : points) { + Map cnt = new HashMap<>(); + for (int[] p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + ans += cnt.getOrDefault(d, 0); + cnt.merge(d, 1, Integer::sum); } } - return ans; + return ans << 1; } } ``` -### **TypeScript** - -```ts -function numberOfBoomerangs(points: number[][]): number { - let ans = 0; - for (let p1 of points) { - let hashMap: Map = new Map(); - for (let p2 of points) { - const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; - hashMap.set(distance, (hashMap.get(distance) || 0) + 1); - } - for (let [, v] of [...hashMap]) { - ans += v * (v - 1); +```java +class Solution { + public int numberOfBoomerangs(int[][] points) { + int ans = 0; + for (int[] p1 : points) { + Map cnt = new HashMap<>(); + for (int[] p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt.merge(d, 1, Integer::sum); + } + for (int x : cnt.values()) { + ans += x * (x - 1); + } } + return ans; } - return ans; } ``` -### **Go** +### **C++** -```go -func numberOfBoomerangs(points [][]int) int { - ans := 0 - for _, p := range points { - cnt := make(map[int]int) - for _, q := range points { - cnt[(p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1])]++ - } - for _, v := range cnt { - ans += v * (v - 1) - } - } - return ans -} +```cpp +class Solution { +public: + int numberOfBoomerangs(vector>& points) { + int ans = 0; + for (auto& p1 : points) { + unordered_map cnt; + for (auto& p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + ans += cnt[d]; + cnt[d]++; + } + } + return ans << 1; + } +}; ``` -### **C++** - ```cpp class Solution { public: int numberOfBoomerangs(vector>& points) { int ans = 0; - for (const auto& p : points) { + for (auto& p1 : points) { unordered_map cnt; - for (const auto& q : points) { - ++cnt[(p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])]; + for (auto& p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt[d]++; } - for (const auto& [_, v] : cnt) { - ans += v * (v - 1); + for (auto& [_, x] : cnt) { + ans += x * (x - 1); } } return ans; @@ -140,6 +160,73 @@ public: }; ``` +### **Go** + +```go +func numberOfBoomerangs(points [][]int) (ans int) { + for _, p1 := range points { + cnt := map[int]int{} + for _, p2 := range points { + d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]) + ans += cnt[d] + cnt[d]++ + } + } + ans <<= 1 + return +} +``` + +```go +func numberOfBoomerangs(points [][]int) (ans int) { + for _, p1 := range points { + cnt := map[int]int{} + for _, p2 := range points { + d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]) + cnt[d]++ + } + for _, x := range cnt { + ans += x * (x - 1) + } + } + return +} +``` + +### **TypeScript** + +```ts +function numberOfBoomerangs(points: number[][]): number { + let ans = 0; + for (const [x1, y1] of points) { + const cnt: Map = new Map(); + for (const [x2, y2] of points) { + const d = (x1 - x2) ** 2 + (y1 - y2) ** 2; + ans += cnt.get(d) || 0; + cnt.set(d, (cnt.get(d) || 0) + 1); + } + } + return ans << 1; +} +``` + +```ts +function numberOfBoomerangs(points: number[][]): number { + let ans = 0; + for (const [x1, y1] of points) { + const cnt: Map = new Map(); + for (const [x2, y2] of points) { + const d = (x1 - x2) ** 2 + (y1 - y2) ** 2; + cnt.set(d, (cnt.get(d) || 0) + 1); + } + for (const [_, x] of cnt) { + ans += x * (x - 1); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp b/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp index a995317d75c56..4bb88388639f6 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp @@ -1,16 +1,17 @@ -class Solution { -public: - int numberOfBoomerangs(vector>& points) { - int ans = 0; - for (const auto& p : points) { - unordered_map cnt; - for (const auto& q : points) { - ++cnt[(p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])]; - } - for (const auto& [_, v] : cnt) { - ans += v * (v - 1); - } - } - return ans; - } -}; +class Solution { +public: + int numberOfBoomerangs(vector>& points) { + int ans = 0; + for (auto& p1 : points) { + unordered_map cnt; + for (auto& p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt[d]++; + } + for (auto& [_, x] : cnt) { + ans += x * (x - 1); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.go b/solution/0400-0499/0447.Number of Boomerangs/Solution.go index 39a064c0272eb..7ce75d48352ac 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.go +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.go @@ -1,13 +1,13 @@ -func numberOfBoomerangs(points [][]int) int { - ans := 0 - for _, p := range points { - cnt := make(map[int]int) - for _, q := range points { - cnt[(p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1])]++ +func numberOfBoomerangs(points [][]int) (ans int) { + for _, p1 := range points { + cnt := map[int]int{} + for _, p2 := range points { + d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]) + cnt[d]++ } - for _, v := range cnt { - ans += v * (v - 1) + for _, x := range cnt { + ans += x * (x - 1) } } - return ans + return } \ No newline at end of file diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.java b/solution/0400-0499/0447.Number of Boomerangs/Solution.java index 7149ab65c9d1e..eb6e8377486c7 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.java +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.java @@ -1,16 +1,16 @@ -class Solution { - public int numberOfBoomerangs(int[][] points) { - int ans = 0; - for (int[] p : points) { - Map counter = new HashMap<>(); - for (int[] q : points) { - int distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]); - counter.put(distance, counter.getOrDefault(distance, 0) + 1); - } - for (int val : counter.values()) { - ans += val * (val - 1); - } - } - return ans; - } +class Solution { + public int numberOfBoomerangs(int[][] points) { + int ans = 0; + for (int[] p1 : points) { + Map cnt = new HashMap<>(); + for (int[] p2 : points) { + int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + cnt.merge(d, 1, Integer::sum); + } + for (int x : cnt.values()) { + ans += x * (x - 1); + } + } + return ans; + } } \ No newline at end of file diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.py b/solution/0400-0499/0447.Number of Boomerangs/Solution.py index fb9c7b6894ce4..44ab8946074cc 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.py +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.py @@ -1,10 +1,10 @@ -class Solution: - def numberOfBoomerangs(self, points: List[List[int]]) -> int: - ans = 0 - for p in points: - counter = Counter() - for q in points: - distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) - counter[distance] += 1 - ans += sum([val * (val - 1) for val in counter.values()]) - return ans +class Solution: + def numberOfBoomerangs(self, points: List[List[int]]) -> int: + ans = 0 + for p1 in points: + cnt = Counter() + for p2 in points: + d = dist(p1, p2) + cnt[d] += 1 + ans += sum(x * (x - 1) for x in cnt.values()) + return ans diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.ts b/solution/0400-0499/0447.Number of Boomerangs/Solution.ts index 20daebc0130ae..6c45ef22118e4 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.ts +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.ts @@ -1,13 +1,13 @@ function numberOfBoomerangs(points: number[][]): number { let ans = 0; - for (let p1 of points) { - let hashMap: Map = new Map(); - for (let p2 of points) { - const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; - hashMap.set(distance, (hashMap.get(distance) || 0) + 1); + for (const [x1, y1] of points) { + const cnt: Map = new Map(); + for (const [x2, y2] of points) { + const d = (x1 - x2) ** 2 + (y1 - y2) ** 2; + cnt.set(d, (cnt.get(d) || 0) + 1); } - for (let [, v] of [...hashMap]) { - ans += v * (v - 1); + for (const [_, x] of cnt) { + ans += x * (x - 1); } } return ans; diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md index dbabbdf85d957..301f55c4fb0a6 100644 --- a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md @@ -68,7 +68,24 @@ ```python - +class Solution: + def minMovesToCaptureTheQueen( + self, a: int, b: int, c: int, d: int, e: int, f: int + ) -> int: + def check(dirs, sx, sy, bx, by) -> bool: + for dx, dy in pairwise(dirs): + for k in range(1, 8): + x = sx + dx * k + y = sy + dy * k + if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): + break + if (x, y) == (e, f): + return True + return False + + dirs1 = (-1, 0, 1, 0, -1) + dirs2 = (-1, 1, 1, -1, -1) + return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 ``` ### **Java** @@ -76,19 +93,124 @@ ```java - +class Solution { + private final int[] dirs1 = {-1, 0, 1, 0, -1}; + private final int[] dirs2 = {-1, 1, 1, -1, -1}; + private int e, f; + + public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + this.e = e; + this.f = f; + return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; + } + + private boolean check(int[] dirs, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[d] * k; + int y = sy + dirs[d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; + auto check = [&](int i, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[i][d] * k; + int y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + } +}; ``` ### **Go** ```go +func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { + dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} + check := func(i, sx, sy, bx, by int) bool { + for d := 0; d < 4; d++ { + for k := 1; k < 8; k++ { + x := sx + dirs[i][d]*k + y := sy + dirs[i][d+1]*k + if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { + break + } + if x == e && y == f { + return true + } + } + } + return false + } + if check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} +``` +### **TypeScript** + +```ts +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, +): number { + const dirs: number[][] = [ + [-1, 0, 1, 0, -1], + [-1, 1, 1, -1, -1], + ]; + const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { + for (let d = 0; d < 4; ++d) { + for (let k = 1; k < 8; ++k) { + const x = sx + dirs[i][d] * k; + const y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8) { + break; + } + if (x === bx && y === by) { + break; + } + if (x === e && y === f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; +} ``` ### **...** diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md index 8340757c22f7f..077d2c6fb6f86 100644 --- a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md @@ -60,25 +60,147 @@ It is impossible to capture the black queen in less than two moves since it is n ### **Python3** ```python - +class Solution: + def minMovesToCaptureTheQueen( + self, a: int, b: int, c: int, d: int, e: int, f: int + ) -> int: + def check(dirs, sx, sy, bx, by) -> bool: + for dx, dy in pairwise(dirs): + for k in range(1, 8): + x = sx + dx * k + y = sy + dy * k + if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): + break + if (x, y) == (e, f): + return True + return False + + dirs1 = (-1, 0, 1, 0, -1) + dirs2 = (-1, 1, 1, -1, -1) + return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 ``` ### **Java** ```java - +class Solution { + private final int[] dirs1 = {-1, 0, 1, 0, -1}; + private final int[] dirs2 = {-1, 1, 1, -1, -1}; + private int e, f; + + public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + this.e = e; + this.f = f; + return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; + } + + private boolean check(int[] dirs, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[d] * k; + int y = sy + dirs[d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; + auto check = [&](int i, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[i][d] * k; + int y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + } +}; ``` ### **Go** ```go +func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { + dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} + check := func(i, sx, sy, bx, by int) bool { + for d := 0; d < 4; d++ { + for k := 1; k < 8; k++ { + x := sx + dirs[i][d]*k + y := sy + dirs[i][d+1]*k + if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { + break + } + if x == e && y == f { + return true + } + } + } + return false + } + if check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} +``` +### **TypeScript** + +```ts +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, +): number { + const dirs: number[][] = [ + [-1, 0, 1, 0, -1], + [-1, 1, 1, -1, -1], + ]; + const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { + for (let d = 0; d < 4; ++d) { + for (let k = 1; k < 8; ++k) { + const x = sx + dirs[i][d] * k; + const y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8) { + break; + } + if (x === bx && y === by) { + break; + } + if (x === e && y === f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; +} ``` ### **...** diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp new file mode 100644 index 0000000000000..c01429a83a927 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; + auto check = [&](int i, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[i][d] * k; + int y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go new file mode 100644 index 0000000000000..39014a52e806c --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go @@ -0,0 +1,22 @@ +func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { + dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} + check := func(i, sx, sy, bx, by int) bool { + for d := 0; d < 4; d++ { + for k := 1; k < 8; k++ { + x := sx + dirs[i][d]*k + y := sy + dirs[i][d+1]*k + if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { + break + } + if x == e && y == f { + return true + } + } + } + return false + } + if check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java new file mode 100644 index 0000000000000..b5d21f96bcd21 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java @@ -0,0 +1,27 @@ +class Solution { + private final int[] dirs1 = {-1, 0, 1, 0, -1}; + private final int[] dirs2 = {-1, 1, 1, -1, -1}; + private int e, f; + + public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + this.e = e; + this.f = f; + return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; + } + + private boolean check(int[] dirs, int sx, int sy, int bx, int by) { + for (int d = 0; d < 4; ++d) { + for (int k = 1; k < 8; ++k) { + int x = sx + dirs[d] * k; + int y = sy + dirs[d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { + break; + } + if (x == e && y == f) { + return true; + } + } + } + return false; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py new file mode 100644 index 0000000000000..486ccf66eb134 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def minMovesToCaptureTheQueen( + self, a: int, b: int, c: int, d: int, e: int, f: int + ) -> int: + def check(dirs, sx, sy, bx, by) -> bool: + for dx, dy in pairwise(dirs): + for k in range(1, 8): + x = sx + dx * k + y = sy + dy * k + if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): + break + if (x, y) == (e, f): + return True + return False + + dirs1 = (-1, 0, 1, 0, -1) + dirs2 = (-1, 1, 1, -1, -1) + return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts new file mode 100644 index 0000000000000..db4633c886027 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts @@ -0,0 +1,32 @@ +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, +): number { + const dirs: number[][] = [ + [-1, 0, 1, 0, -1], + [-1, 1, 1, -1, -1], + ]; + const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { + for (let d = 0; d < 4; ++d) { + for (let k = 1; k < 8; ++k) { + const x = sx + dirs[i][d] * k; + const y = sy + dirs[i][d + 1] * k; + if (x < 1 || x > 8 || y < 1 || y > 8) { + break; + } + if (x === bx && y === by) { + break; + } + if (x === e && y === f) { + return true; + } + } + } + return false; + }; + return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; +} diff --git a/solution/1300-1399/1321.Restaurant Growth/README.md b/solution/1300-1399/1321.Restaurant Growth/README.md index 1fe8b4e3532d4..174d70118f3b1 100644 --- a/solution/1300-1399/1321.Restaurant Growth/README.md +++ b/solution/1300-1399/1321.Restaurant Growth/README.md @@ -104,4 +104,18 @@ FROM t WHERE rk > 6; ``` +```sql +# Write your MySQL query statement below +SELECT + a.visited_on, + SUM(b.amount) AS amount, + ROUND(SUM(b.amount) / 7, 2) AS average_amount +FROM + (SELECT DISTINCT visited_on FROM customer) AS a + JOIN customer AS b ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6 +WHERE a.visited_on >= (SELECT MIN(visited_on) FROM customer) + 6 +GROUP BY 1 +ORDER BY 1; +``` + diff --git a/solution/1300-1399/1321.Restaurant Growth/README_EN.md b/solution/1300-1399/1321.Restaurant Growth/README_EN.md index 5f60697e754ce..ab91dc98bd5f9 100644 --- a/solution/1300-1399/1321.Restaurant Growth/README_EN.md +++ b/solution/1300-1399/1321.Restaurant Growth/README_EN.md @@ -100,4 +100,18 @@ FROM t WHERE rk > 6; ``` +```sql +# Write your MySQL query statement below +SELECT + a.visited_on, + SUM(b.amount) AS amount, + ROUND(SUM(b.amount) / 7, 2) AS average_amount +FROM + (SELECT DISTINCT visited_on FROM customer) AS a + JOIN customer AS b ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6 +WHERE a.visited_on >= (SELECT MIN(visited_on) FROM customer) + 6 +GROUP BY 1 +ORDER BY 1; +``` +