Skip to content

Commit 78a17b0

Browse files
committed
feat: update solutions to lc problems: No.2515~2517
* 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
1 parent b75db0f commit 78a17b0

File tree

6 files changed

+34
-4
lines changed
  • solution
    • 0600-0699/0617.Merge Two Binary Trees
    • 1100-1199/1152.Analyze User Website Visit Pattern
    • 1700-1799/1759.Count Number of Homogenous Substrings
    • 2500-2599
      • 2515.Shortest Distance to Target String in a Circular Array
      • 2516.Take K of Each Character From Left and Right
      • 2517.Maximum Tastiness of Candy Basket

6 files changed

+34
-4
lines changed

solution/0600-0699/0617.Merge Two Binary Trees/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
8+
def mergeTrees(
9+
self, root1: Optional[TreeNode], root2: Optional[TreeNode]
10+
) -> Optional[TreeNode]:
911
if root1 is None:
1012
return root2
1113
if root2 is None:

solution/1100-1199/1152.Analyze User Website Visit Pattern/Solution.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
class Solution:
2-
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
2+
def mostVisitedPattern(
3+
self, username: List[str], timestamp: List[int], website: List[str]
4+
) -> List[str]:
35
d = defaultdict(list)
4-
for user, _, site in sorted(zip(username, timestamp, website), key=lambda x: x[1]):
6+
for user, _, site in sorted(
7+
zip(username, timestamp, website), key=lambda x: x[1]
8+
):
59
d[user].append(site)
610

711
cnt = Counter()

solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ int countHomogenous(char *s) {
262262
for (int i = 0, j = 0; s[j]; j++) {
263263
if (s[i] != s[j]) {
264264
i = j;
265-
}
265+
}
266266
ans = (ans + j - i + 1) % MOD;
267267
}
268268
return ans;

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

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:一次遍历**
65+
66+
遍历数组,找到与 target 相等的单词,计算其与 startIndex 的距离 $t$,则此时的最短距离为 $min(t, n - t)$,我们只需要不断更新最小值即可。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
69+
6470
<!-- tabs:start -->
6571

6672
### **Python3**

solution/2500-2599/2516.Take K of Each Character From Left and Right/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:滑动窗口**
50+
51+
我们先用哈希表或者一个长度为 $3$ 的数组 `cnt` 统计字符串 $s$ 中每个字符的个数,如果有字符的个数小于 $k$ 个,则无法取到,提前返回 $-1$。
52+
53+
题目要我们在字符串左侧以及右侧取走字符,最终取到的每种字符的个数都不少于 $k$ 个。我们不妨反着考虑问题,取走中间某个窗口大小的字符串,使得剩下的两侧字符串中,每种字符的个数都不少于 $k$ 个。
54+
55+
因此,我们维护一个滑动窗口,用指针 $j$ 和 $i$ 分别表示窗口的左右边界,窗口内的字符串是我们要取走的。我们每一次移动右边界 $i$,将对应的字符 $s[i]$ 加入到窗口中(也即取走一个字符 $s[i]$),此时如果 $cnt[s[i]]$ 个数小于 $k$,那么我们循环移动左边界 $j$,直到 $cnt[s[i]]$ 个数不小于 $k$ 为止。此时的窗口大小为 $i - j + 1$,更新最大窗口。
56+
57+
最终的答案就是字符串 $s$ 的长度减去最大窗口的大小。
58+
59+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**

solution/2500-2599/2517.Maximum Tastiness of Candy Basket/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:二分查找**
60+
61+
我们先对数组 `price` 进行排序,然后二分枚举甜蜜度,找到最大的且满足至少有 $k$ 类糖果的甜蜜度。
62+
63+
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `price` 的长度,而 $M$ 为数组 `price` 中的最大值。本题中我们取 $M = 10^9$。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**

0 commit comments

Comments
 (0)