Skip to content

Commit 1129964

Browse files
committed
feat: add solutions to lc problems: No.2515~2518
* No.2515.Shortest Distance to Target String in a Circular Array * No.2516.Take K of Each Character From Left and Right * No.2517.Maximum Tastiness of Candy Basket * No.2518.Number of Great Partitions
1 parent aae0d64 commit 1129964

File tree

28 files changed

+1076
-38
lines changed

28 files changed

+1076
-38
lines changed

solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def longestSubstring(self, s: str, k: int) -> int:
33
def dfs(l, r):
4-
cnt = Counter(s[l: r + 1])
4+
cnt = Counter(s[l : r + 1])
55
split = next((c for c, v in cnt.items() if v < k), '')
66
if not split:
77
return r - l + 1
@@ -19,5 +19,5 @@ def dfs(l, r):
1919
ans = max(ans, t)
2020
i = j
2121
return ans
22-
23-
return dfs(0, len(s) - 1)
22+
23+
return dfs(0, len(s) - 1)

solution/0600-0699/0697.Degree of an Array/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ def findShortestSubArray(self, nums: List[int]) -> int:
1313
t = right[v] - left[v] + 1
1414
if ans > t:
1515
ans = t
16-
return ans
16+
return ans

solution/1700-1799/1763.Longest Nice Substring/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ def longestNiceSubstring(self, s: str) -> str:
1010
else:
1111
upper |= 1 << (ord(s[j]) - ord('A'))
1212
if lower == upper and len(ans) < j - i + 1:
13-
ans = s[i: j + 1]
13+
ans = s[i : j + 1]
1414
return ans

solution/1900-1999/1947.Maximum Compatibility Score Sum/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def maxCompatibilitySum(self, students: List[List[int]], mentors: List[List[int]]) -> int:
2+
def maxCompatibilitySum(
3+
self, students: List[List[int]], mentors: List[List[int]]
4+
) -> int:
35
def dfs(i, t):
46
if i == m:
57
nonlocal ans

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,89 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
class Solution:
72+
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
73+
n = len(words)
74+
ans = n
75+
for i, w in enumerate(words):
76+
if w == target:
77+
t = abs(i - startIndex)
78+
ans = min(ans, t, n - t)
79+
return -1 if ans == n else ans
7280
```
7381

7482
### **Java**
7583

7684
<!-- 这里可写当前语言的特殊实现逻辑 -->
7785

7886
```java
79-
87+
class Solution {
88+
public int closetTarget(String[] words, String target, int startIndex) {
89+
int n = words.length;
90+
int ans = n;
91+
for (int i = 0; i < n; ++i) {
92+
String w = words[i];
93+
if (w.equals(target)) {
94+
int t = Math.abs(i - startIndex);
95+
ans = Math.min(ans, Math.min(t, n - t));
96+
}
97+
}
98+
return ans == n ? -1 : ans;
99+
}
100+
}
80101
```
81102

82103
### **C++**
83104

84105
```cpp
85-
106+
class Solution {
107+
public:
108+
int closetTarget(vector<string>& words, string target, int startIndex) {
109+
int n = words.size();
110+
int ans = n;
111+
for (int i = 0; i < n; ++i) {
112+
auto w = words[i];
113+
if (w == target) {
114+
int t = abs(i - startIndex);
115+
ans = min(ans, min(t, n - t));
116+
}
117+
}
118+
return ans == n ? -1 : ans;
119+
}
120+
};
86121
```
87122
88123
### **Go**
89124
90125
```go
91-
126+
func closetTarget(words []string, target string, startIndex int) int {
127+
n := len(words)
128+
ans := n
129+
for i, w := range words {
130+
if w == target {
131+
t := abs(i - startIndex)
132+
ans = min(ans, min(t, n-t))
133+
}
134+
}
135+
if ans == n {
136+
return -1
137+
}
138+
return ans
139+
}
140+
141+
func abs(x int) int {
142+
if x < 0 {
143+
return -x
144+
}
145+
return x
146+
}
147+
148+
func min(a, b int) int {
149+
if a < b {
150+
return a
151+
}
152+
return b
153+
}
92154
```
93155

94156
### **...**

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,87 @@ The shortest distance to reach &quot;leetcode&quot; is 1.</pre>
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
68+
n = len(words)
69+
ans = n
70+
for i, w in enumerate(words):
71+
if w == target:
72+
t = abs(i - startIndex)
73+
ans = min(ans, t, n - t)
74+
return -1 if ans == n else ans
6775
```
6876

6977
### **Java**
7078

7179
```java
72-
80+
class Solution {
81+
public int closetTarget(String[] words, String target, int startIndex) {
82+
int n = words.length;
83+
int ans = n;
84+
for (int i = 0; i < n; ++i) {
85+
String w = words[i];
86+
if (w.equals(target)) {
87+
int t = Math.abs(i - startIndex);
88+
ans = Math.min(ans, Math.min(t, n - t));
89+
}
90+
}
91+
return ans == n ? -1 : ans;
92+
}
93+
}
7394
```
7495

7596
### **C++**
7697

7798
```cpp
78-
99+
class Solution {
100+
public:
101+
int closetTarget(vector<string>& words, string target, int startIndex) {
102+
int n = words.size();
103+
int ans = n;
104+
for (int i = 0; i < n; ++i) {
105+
auto w = words[i];
106+
if (w == target) {
107+
int t = abs(i - startIndex);
108+
ans = min(ans, min(t, n - t));
109+
}
110+
}
111+
return ans == n ? -1 : ans;
112+
}
113+
};
79114
```
80115
81116
### **Go**
82117
83118
```go
84-
119+
func closetTarget(words []string, target string, startIndex int) int {
120+
n := len(words)
121+
ans := n
122+
for i, w := range words {
123+
if w == target {
124+
t := abs(i - startIndex)
125+
ans = min(ans, min(t, n-t))
126+
}
127+
}
128+
if ans == n {
129+
return -1
130+
}
131+
return ans
132+
}
133+
134+
func abs(x int) int {
135+
if x < 0 {
136+
return -x
137+
}
138+
return x
139+
}
140+
141+
func min(a, b int) int {
142+
if a < b {
143+
return a
144+
}
145+
return b
146+
}
85147
```
86148

87149
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int closetTarget(vector<string>& words, string target, int startIndex) {
4+
int n = words.size();
5+
int ans = n;
6+
for (int i = 0; i < n; ++i) {
7+
auto w = words[i];
8+
if (w == target) {
9+
int t = abs(i - startIndex);
10+
ans = min(ans, min(t, n - t));
11+
}
12+
}
13+
return ans == n ? -1 : ans;
14+
}
15+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func closetTarget(words []string, target string, startIndex int) int {
2+
n := len(words)
3+
ans := n
4+
for i, w := range words {
5+
if w == target {
6+
t := abs(i - startIndex)
7+
ans = min(ans, min(t, n-t))
8+
}
9+
}
10+
if ans == n {
11+
return -1
12+
}
13+
return ans
14+
}
15+
16+
func abs(x int) int {
17+
if x < 0 {
18+
return -x
19+
}
20+
return x
21+
}
22+
23+
func min(a, b int) int {
24+
if a < b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int closetTarget(String[] words, String target, int startIndex) {
3+
int n = words.length;
4+
int ans = n;
5+
for (int i = 0; i < n; ++i) {
6+
String w = words[i];
7+
if (w.equals(target)) {
8+
int t = Math.abs(i - startIndex);
9+
ans = Math.min(ans, Math.min(t, n - t));
10+
}
11+
}
12+
return ans == n ? -1 : ans;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
3+
n = len(words)
4+
ans = n
5+
for i, w in enumerate(words):
6+
if w == target:
7+
t = abs(i - startIndex)
8+
ans = min(ans, t, n - t)
9+
return -1 if ans == n else ans

0 commit comments

Comments
 (0)