& stones) {
+ sort(stones.begin(), stones.end());
+ int n = stones.size();
+ int mi = n;
+ int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
+ for (int i = 0, j = 0; j < n; ++j) {
+ while (stones[j] - stones[i] + 1 > n) {
+ ++i;
+ }
+ if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
+ mi = min(mi, 2);
+ } else {
+ mi = min(mi, n - (j - i + 1));
+ }
+ }
+ return {mi, mx};
+ }
+};
\ No newline at end of file
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go
new file mode 100644
index 0000000000000..b3f712ddbc7c0
--- /dev/null
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go
@@ -0,0 +1,32 @@
+func numMovesStonesII(stones []int) []int {
+ sort.Ints(stones)
+ n := len(stones)
+ mi := n
+ mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1)
+ i := 0
+ for j, x := range stones {
+ for x-stones[i]+1 > n {
+ i++
+ }
+ if j-i+1 == n-1 && stones[j]-stones[i] == n-2 {
+ mi = min(mi, 2)
+ } else {
+ mi = min(mi, n-(j-i+1))
+ }
+ }
+ return []int{mi, mx}
+}
+
+func max(a, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
\ No newline at end of file
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java
new file mode 100644
index 0000000000000..d5d8e6a54835f
--- /dev/null
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java
@@ -0,0 +1,19 @@
+class Solution {
+ public int[] numMovesStonesII(int[] stones) {
+ Arrays.sort(stones);
+ int n = stones.length;
+ int mi = n;
+ int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
+ for (int i = 0, j = 0; j < n; ++j) {
+ while (stones[j] - stones[i] + 1 > n) {
+ ++i;
+ }
+ if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
+ mi = Math.min(mi, 2);
+ } else {
+ mi = Math.min(mi, n - (j - i + 1));
+ }
+ }
+ return new int[] {mi, mx};
+ }
+}
\ No newline at end of file
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py
new file mode 100644
index 0000000000000..9aa212f569ce4
--- /dev/null
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py
@@ -0,0 +1,14 @@
+class Solution:
+ def numMovesStonesII(self, stones: List[int]) -> List[int]:
+ stones.sort()
+ mi = n = len(stones)
+ mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1)
+ i = 0
+ for j, x in enumerate(stones):
+ while x - stones[i] + 1 > n:
+ i += 1
+ if j - i + 1 == n - 1 and x - stones[i] == n - 2:
+ mi = min(mi, 2)
+ else:
+ mi = min(mi, n - (j - i + 1))
+ return [mi, mx]
From 3531802c0163dece5a31a3d080d1751fc39d232d Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Thu, 6 Apr 2023 23:45:30 +0800
Subject: [PATCH 3/7] feat: add solutions to lc problems: No.1041
---
.../1041.Robot Bounded In Circle/README.md | 22 +++++++++++++++++++
.../1041.Robot Bounded In Circle/README_EN.md | 22 +++++++++++++++++++
.../1041.Robot Bounded In Circle/Solution.ts | 17 ++++++++++++++
3 files changed, 61 insertions(+)
create mode 100644 solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts
diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README.md b/solution/1000-1099/1041.Robot Bounded In Circle/README.md
index 8000f8ff91e6a..5e9dc8f969881 100644
--- a/solution/1000-1099/1041.Robot Bounded In Circle/README.md
+++ b/solution/1000-1099/1041.Robot Bounded In Circle/README.md
@@ -178,6 +178,28 @@ func isRobotBounded(instructions string) bool {
}
```
+### **TypeScript**
+
+```ts
+function isRobotBounded(instructions: string): boolean {
+ const direction = new Array(4).fill(0);
+ let cur = 0;
+ for (const c of instructions.split('')) {
+ if (c === 'L') {
+ cur = (cur + 1) % 4;
+ } else if (c === 'R') {
+ cur = (cur + 3) % 4;
+ } else {
+ ++direction[cur];
+ }
+ }
+ return (
+ cur !== 0 ||
+ (direction[0] === direction[2] && direction[1] === direction[3])
+ );
+}
+```
+
### **...**
```
diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md
index 906194839e41a..caee4aff6baf2 100644
--- a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md
+++ b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md
@@ -163,6 +163,28 @@ func isRobotBounded(instructions string) bool {
}
```
+### **TypeScript**
+
+```ts
+function isRobotBounded(instructions: string): boolean {
+ const direction = new Array(4).fill(0);
+ let cur = 0;
+ for (const c of instructions.split('')) {
+ if (c === 'L') {
+ cur = (cur + 1) % 4;
+ } else if (c === 'R') {
+ cur = (cur + 3) % 4;
+ } else {
+ ++direction[cur];
+ }
+ }
+ return (
+ cur !== 0 ||
+ (direction[0] === direction[2] && direction[1] === direction[3])
+ );
+}
+```
+
### **...**
```
diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts
new file mode 100644
index 0000000000000..fd8d424f77da0
--- /dev/null
+++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts
@@ -0,0 +1,17 @@
+function isRobotBounded(instructions: string): boolean {
+ const direction = new Array(4).fill(0);
+ let cur = 0;
+ for (const c of instructions.split('')) {
+ if (c === 'L') {
+ cur = (cur + 1) % 4;
+ } else if (c === 'R') {
+ cur = (cur + 3) % 4;
+ } else {
+ ++direction[cur];
+ }
+ }
+ return (
+ cur !== 0 ||
+ (direction[0] === direction[2] && direction[1] === direction[3])
+ );
+}
From e14f8a8e5402cf7ad808f65e183b10f4ff1b39a5 Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Thu, 6 Apr 2023 23:51:58 +0800
Subject: [PATCH 4/7] feat: add solutions to lc problems: No.1040
---
.../README.md | 24 +++++++++++++++++++
.../README_EN.md | 24 +++++++++++++++++++
.../Solution.ts | 19 +++++++++++++++
3 files changed, 67 insertions(+)
create mode 100644 solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md
index f11d1ec6945d8..7081c8369522d 100644
--- a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md
@@ -176,6 +176,30 @@ func min(a, b int) int {
}
```
+### **TypeScript**
+
+```ts
+function numMovesStonesII(stones: number[]): number[] {
+ stones.sort((a, b) => a - b);
+ const n = stones.length;
+ let mi = n;
+ const mx =
+ Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) -
+ (n - 1);
+ for (let i = 0, j = 0; j < n; ++j) {
+ while (stones[j] - stones[i] + 1 > n) {
+ ++i;
+ }
+ if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
+ mi = Math.min(mi, 2);
+ } else {
+ mi = Math.min(mi, n - (j - i + 1));
+ }
+ }
+ return [mi, mx];
+}
+```
+
### **...**
```
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md
index 9532713c48920..ebfd2b254cf93 100644
--- a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md
@@ -159,6 +159,30 @@ func min(a, b int) int {
}
```
+### **TypeScript**
+
+```ts
+function numMovesStonesII(stones: number[]): number[] {
+ stones.sort((a, b) => a - b);
+ const n = stones.length;
+ let mi = n;
+ const mx =
+ Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) -
+ (n - 1);
+ for (let i = 0, j = 0; j < n; ++j) {
+ while (stones[j] - stones[i] + 1 > n) {
+ ++i;
+ }
+ if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
+ mi = Math.min(mi, 2);
+ } else {
+ mi = Math.min(mi, n - (j - i + 1));
+ }
+ }
+ return [mi, mx];
+}
+```
+
### **...**
```
diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts
new file mode 100644
index 0000000000000..ecd444c136472
--- /dev/null
+++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts
@@ -0,0 +1,19 @@
+function numMovesStonesII(stones: number[]): number[] {
+ stones.sort((a, b) => a - b);
+ const n = stones.length;
+ let mi = n;
+ const mx =
+ Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) -
+ (n - 1);
+ for (let i = 0, j = 0; j < n; ++j) {
+ while (stones[j] - stones[i] + 1 > n) {
+ ++i;
+ }
+ if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
+ mi = Math.min(mi, 2);
+ } else {
+ mi = Math.min(mi, n - (j - i + 1));
+ }
+ }
+ return [mi, mx];
+}
From 8cc9aa22546f6e536ad4e7b7be2a01a8c652e7b8 Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Thu, 6 Apr 2023 23:58:24 +0800
Subject: [PATCH 5/7] feat: update solutions to lc problems: No.0006
---
solution/0000-0099/0006.Zigzag Conversion/README.md | 4 ++--
solution/0000-0099/0006.Zigzag Conversion/README_EN.md | 4 ++--
solution/0000-0099/0006.Zigzag Conversion/Solution.js | 4 ++--
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md
index a5668dd8a258c..86415cacdbde7 100644
--- a/solution/0000-0099/0006.Zigzag Conversion/README.md
+++ b/solution/0000-0099/0006.Zigzag Conversion/README.md
@@ -211,7 +211,7 @@ func convert(s string, numRows int) string {
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
- let arr = new Array(numRows);
+ const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
@@ -225,7 +225,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
- for (let item of arr) {
+ for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md
index 471c1be5b271f..303851237fa12 100644
--- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md
+++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md
@@ -205,7 +205,7 @@ func convert(s string, numRows int) string {
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
- let arr = new Array(numRows);
+ const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
@@ -219,7 +219,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
- for (let item of arr) {
+ for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.js b/solution/0000-0099/0006.Zigzag Conversion/Solution.js
index c6d4dcd721ae1..e291e38cb6b18 100644
--- a/solution/0000-0099/0006.Zigzag Conversion/Solution.js
+++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.js
@@ -5,7 +5,7 @@
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
- let arr = new Array(numRows);
+ const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
@@ -19,7 +19,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
- for (let item of arr) {
+ for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
From 8c7e7a2ba39b9781ebabafdc3fc2a62b9d6bba48 Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Fri, 7 Apr 2023 00:03:15 +0800
Subject: [PATCH 6/7] style: update desciption to lc problem: No.33
---
.../0000-0099/0033.Search in Rotated Sorted Array/README.md | 4 ++--
solution/main.py | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
index 5763f2f1203bb..177b710b7ed04 100644
--- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
+++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
@@ -19,14 +19,14 @@
示例 1:
-输入:nums = [4,5,6,7,0,1,2]
, target = 0
+输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
示例 2:
-输入:nums = [4,5,6,7,0,1,2]
, target = 3
+输入:nums = [4,5,6,7,0,1,2], target = 3
输出:-1
示例 3:
diff --git a/solution/main.py b/solution/main.py
index d9a248683f6ae..2a2591f3c14b4 100644
--- a/solution/main.py
+++ b/solution/main.py
@@ -134,7 +134,7 @@ def generate_summary(result):
def refresh(result):
"""update problems"""
pattern = re.compile("src=\"(.*?)\"")
- skip_question_ids = {3, 1599}
+ skip_question_ids = {3, 33, 1599}
for question in result:
front_question_id = question['frontend_question_id']
From 4a27e7be6f5336b798d4e56966d3445c4a27084f Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Fri, 7 Apr 2023 00:07:32 +0800
Subject: [PATCH 7/7] style: update desciption to lc problem: No.34
---
.../README.md | 4 ++--
solution/main.py | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md
index a1550b28c8c39..f8d23d79a642a 100644
--- a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md
+++ b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md
@@ -17,13 +17,13 @@
示例 1:
-输入:nums = [5,7,7,8,8,10]
, target = 8
+输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
-输入:nums = [5,7,7,8,8,10]
, target = 6
+输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
diff --git a/solution/main.py b/solution/main.py
index 2a2591f3c14b4..eb1e311112c61 100644
--- a/solution/main.py
+++ b/solution/main.py
@@ -134,7 +134,7 @@ def generate_summary(result):
def refresh(result):
"""update problems"""
pattern = re.compile("src=\"(.*?)\"")
- skip_question_ids = {3, 33, 1599}
+ skip_question_ids = {3, 33, 34, 1599}
for question in result:
front_question_id = question['frontend_question_id']