Skip to content

Commit 8384416

Browse files
committed
feat: add solutions to lc problem: No.1705
* Add solutions to lc problem: No.1705.Maximum Number of Eaten Apples * Update python solutions
1 parent 770b85e commit 8384416

File tree

276 files changed

+1101
-1003
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

276 files changed

+1101
-1003
lines changed

lcci/01.02.Check Permutation/README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Solution:
4444
n1, n2 = len(s1), len(s2)
4545
if n1 != n2:
4646
return False
47-
counter = collections.Counter()
47+
counter = Counter()
4848
for i in range(n1):
4949
counter[s1[i]] += 1
5050
counter[s2[i]] -= 1
@@ -84,16 +84,18 @@ class Solution {
8484
### **JavaScript**
8585

8686
```js
87-
var CheckPermutation = function(s1, s2) {
88-
let n1 = s1.length, n2 = s2.length;
89-
if (n1 != n2) return false;
90-
let counter = {};
91-
for (let i = 0; i < n1; i++) {
92-
let cur1 = s1.charAt(i), cur2 = s2.charAt(i);
93-
counter[cur1] = (counter[cur1] || 0) + 1;
94-
counter[cur2] = (counter[cur2] || 0) - 1;
95-
}
96-
return Object.values(counter).every(v => v == 0);
87+
var CheckPermutation = function (s1, s2) {
88+
let n1 = s1.length,
89+
n2 = s2.length;
90+
if (n1 != n2) return false;
91+
let counter = {};
92+
for (let i = 0; i < n1; i++) {
93+
let cur1 = s1.charAt(i),
94+
cur2 = s2.charAt(i);
95+
counter[cur1] = (counter[cur1] || 0) + 1;
96+
counter[cur2] = (counter[cur2] || 0) - 1;
97+
}
98+
return Object.values(counter).every((v) => v == 0);
9799
};
98100
```
99101

lcci/01.02.Check Permutation/README_EN.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Solution:
4444
n1, n2 = len(s1), len(s2)
4545
if n1 != n2:
4646
return False
47-
counter = collections.Counter()
47+
counter = Counter()
4848
for i in range(n1):
4949
counter[s1[i]] += 1
5050
counter[s2[i]] -= 1
@@ -82,16 +82,18 @@ class Solution {
8282
### **JavaScript**
8383

8484
```js
85-
var CheckPermutation = function(s1, s2) {
86-
let n1 = s1.length, n2 = s2.length;
87-
if (n1 != n2) return false;
88-
let counter = {};
89-
for (let i = 0; i < n1; i++) {
90-
let cur1 = s1.charAt(i), cur2 = s2.charAt(i);
91-
counter[cur1] = (counter[cur1] || 0) + 1;
92-
counter[cur2] = (counter[cur2] || 0) - 1;
93-
}
94-
return Object.values(counter).every(v => v == 0);
85+
var CheckPermutation = function (s1, s2) {
86+
let n1 = s1.length,
87+
n2 = s2.length;
88+
if (n1 != n2) return false;
89+
let counter = {};
90+
for (let i = 0; i < n1; i++) {
91+
let cur1 = s1.charAt(i),
92+
cur2 = s2.charAt(i);
93+
counter[cur1] = (counter[cur1] || 0) + 1;
94+
counter[cur2] = (counter[cur2] || 0) - 1;
95+
}
96+
return Object.values(counter).every((v) => v == 0);
9597
};
9698
```
9799

lcci/01.02.Check Permutation/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def CheckPermutation(self, s1: str, s2: str) -> bool:
33
n1, n2 = len(s1), len(s2)
44
if n1 != n2:
55
return False
6-
counter = collections.Counter()
6+
counter = Counter()
77
for i in range(n1):
88
counter[s1[i]] += 1
99
counter[s2[i]] -= 1

lcci/01.04.Palindrome Permutation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
```python
3737
class Solution:
3838
def canPermutePalindrome(self, s: str) -> bool:
39-
counter = collections.Counter(s)
39+
counter = Counter(s)
4040
cnt = 0
4141
for val in counter.values():
4242
if (val & 1) == 1:

lcci/01.04.Palindrome Permutation/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
```python
2828
class Solution:
2929
def canPermutePalindrome(self, s: str) -> bool:
30-
counter = collections.Counter(s)
30+
counter = Counter(s)
3131
cnt = 0
3232
for val in counter.values():
3333
if (val & 1) == 1:

lcci/01.04.Palindrome Permutation/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def canPermutePalindrome(self, s: str) -> bool:
3-
counter = collections.Counter(s)
3+
counter = Counter(s)
44
cnt = 0
55
for val in counter.values():
66
if (val & 1) == 1:

lcci/10.02.Group Anagrams/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
```python
5050
class Solution:
5151
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
52-
chars = collections.defaultdict(list)
52+
chars = defaultdict(list)
5353
for s in strs:
5454
k = ''.join(sorted(list(s)))
5555
chars[k].append(s)

lcci/10.02.Group Anagrams/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
```python
4949
class Solution:
5050
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
51-
chars = collections.defaultdict(list)
51+
chars = defaultdict(list)
5252
for s in strs:
5353
k = ''.join(sorted(list(s)))
5454
chars[k].append(s)

lcci/10.02.Group Anagrams/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3-
chars = collections.defaultdict(list)
3+
chars = defaultdict(list)
44
for s in strs:
55
k = ''.join(sorted(list(s)))
66
chars[k].append(s)

lcci/16.02.Words Frequency/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ wordsFrequency.get("pen"); //返回1
4343
class WordsFrequency:
4444

4545
def __init__(self, book: List[str]):
46-
self.counter = collections.Counter(book)
46+
self.counter = Counter(book)
4747

4848
def get(self, word: str) -> int:
4949
return self.counter[word]

lcci/16.02.Words Frequency/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ wordsFrequency.get(&quot;pen&quot;); //returns 1
5050
class WordsFrequency:
5151

5252
def __init__(self, book: List[str]):
53-
self.counter = collections.Counter(book)
53+
self.counter = Counter(book)
5454

5555
def get(self, word: str) -> int:
5656
return self.counter[word]
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class WordsFrequency:
22

33
def __init__(self, book: List[str]):
4-
self.counter = collections.Counter(book)
4+
self.counter = Counter(book)
55

66
def get(self, word: str) -> int:
77
return self.counter[word]
88

99
# Your WordsFrequency object will be instantiated and called as such:
1010
# obj = WordsFrequency(book)
11-
# param_1 = obj.get(word)
11+
# param_1 = obj.get(word)

lcci/17.07.Baby Names/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
```python
3636
class Solution:
3737
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
38-
mp = collections.defaultdict(int)
39-
p = collections.defaultdict(str)
38+
mp = defaultdict(int)
39+
p = defaultdict(str)
4040

4141
def find(x):
4242
if p[x] != x:
4343
p[x] = find(p[x])
4444
return p[x]
45-
45+
4646
def union(a, b):
4747
pa, pb = find(a), find(b)
4848
if pa == pb:

lcci/17.07.Baby Names/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
```python
3030
class Solution:
3131
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
32-
mp = collections.defaultdict(int)
33-
p = collections.defaultdict(str)
32+
mp = defaultdict(int)
33+
p = defaultdict(str)
3434

3535
def find(x):
3636
if p[x] != x:
3737
p[x] = find(p[x])
3838
return p[x]
39-
39+
4040
def union(a, b):
4141
pa, pb = find(a), find(b)
4242
if pa == pb:

lcci/17.07.Baby Names/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
3-
mp = collections.defaultdict(int)
4-
p = collections.defaultdict(str)
3+
mp = defaultdict(int)
4+
p = defaultdict(str)
55

66
def find(x):
77
if p[x] != x:

lcof/面试题37. 序列化二叉树/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Codec:
5252
"""
5353
if not root:
5454
return '[]'
55-
queue = collections.deque()
55+
queue = deque()
5656
queue.append(root)
5757
res = []
5858
while queue:
@@ -74,7 +74,7 @@ class Codec:
7474
"""
7575
if not data or data == '[]':
7676
return None
77-
queue = collections.deque()
77+
queue = deque()
7878
nodes = data[1:-1].split(',')
7979
root = TreeNode(nodes[0])
8080
queue.append(root)

lcof/面试题37. 序列化二叉树/Solution.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class Codec:
99

1010
def serialize(self, root):
1111
"""Encodes a tree to a single string.
12-
12+
1313
:type root: TreeNode
1414
:rtype: str
1515
"""
1616
if not root:
1717
return '[]'
18-
queue = collections.deque()
18+
queue = deque()
1919
queue.append(root)
2020
res = []
2121
while queue:
@@ -27,17 +27,16 @@ def serialize(self, root):
2727
else:
2828
res.append('null')
2929
return f'[{",".join(res)}]'
30-
3130

3231
def deserialize(self, data):
3332
"""Decodes your encoded data to tree.
34-
33+
3534
:type data: str
3635
:rtype: TreeNode
3736
"""
3837
if not data or data == '[]':
3938
return None
40-
queue = collections.deque()
39+
queue = deque()
4140
nodes = data[1:-1].split(',')
4241
root = TreeNode(nodes[0])
4342
queue.append(root)
@@ -57,4 +56,4 @@ def deserialize(self, data):
5756

5857
# Your Codec object will be instantiated and called as such:
5958
# codec = Codec()
60-
# codec.deserialize(codec.serialize(root))
59+
# codec.deserialize(codec.serialize(root))

lcof/面试题50. 第一个只出现一次的字符/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ s = ""
3333
```python
3434
class Solution:
3535
def firstUniqChar(self, s: str) -> str:
36-
counter = collections.Counter(s)
36+
counter = Counter(s)
3737
for c in s:
3838
if counter[c] == 1:
3939
return c

lcof/面试题50. 第一个只出现一次的字符/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def firstUniqChar(self, s: str) -> str:
3-
counter = collections.Counter(s)
3+
counter = Counter(s)
44
for c in s:
55
if counter[c] == 1:
66
return c

lcof/面试题59 - I. 滑动窗口的最大值/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ for i in range(n):
5757
```python
5858
class Solution:
5959
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
60-
q, res = collections.deque(), []
60+
q, res = deque(), []
6161
for i, num in enumerate(nums):
6262
if q and i - k + 1 > q[0]:
6363
q.popleft()

lcof/面试题59 - I. 滑动窗口的最大值/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
3-
q, res = collections.deque(), []
3+
q, res = deque(), []
44
for i, num in enumerate(nums):
55
if q and i - k + 1 > q[0]:
66
q.popleft()

lcof2/剑指 Offer II 033. 变位词组/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
```python
6767
class Solution:
6868
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
69-
chars = collections.defaultdict(list)
69+
chars = defaultdict(list)
7070
for s in strs:
7171
k = ''.join(sorted(list(s)))
7272
chars[k].append(s)

lcof2/剑指 Offer II 033. 变位词组/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3-
chars = collections.defaultdict(list)
3+
chars = defaultdict(list)
44
for s in strs:
55
k = ''.join(sorted(list(s)))
66
chars[k].append(s)

lcof2/剑指 Offer II 042. 最近请求次数/README.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ recentCounter.ping(3002); // requests = [1, <strong>100</strong>, <strong>3001<
4848

4949
<p><meta charset="UTF-8" />注意:本题与主站 933&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/number-of-recent-calls/">https://leetcode-cn.com/problems/number-of-recent-calls/</a></p>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
@@ -67,7 +66,7 @@ recentCounter.ping(3002); // requests = [1, <strong>100</strong>, <strong>3001<
6766
class RecentCounter:
6867

6968
def __init__(self):
70-
self.q = collections.deque()
69+
self.q = deque()
7170

7271
def ping(self, t: int) -> int:
7372
self.q.append(t)
@@ -92,7 +91,7 @@ class RecentCounter {
9291
public RecentCounter() {
9392
q = new LinkedList<>();
9493
}
95-
94+
9695
public int ping(int t) {
9796
q.offerLast(t);
9897
while (q.peekFirst() < t - 3000) {
@@ -119,7 +118,7 @@ public:
119118
RecentCounter() {
120119

121120
}
122-
121+
123122
int ping(int t) {
124123
q.push_back(t);
125124
while (q.front() < t - 3000) {
@@ -167,20 +166,20 @@ func (this *RecentCounter) Ping(t int) int {
167166
### **JavaScript**
168167

169168
```js
170-
var RecentCounter = function() {
171-
this.q = [];
169+
var RecentCounter = function () {
170+
this.q = [];
172171
};
173172

174-
/**
173+
/**
175174
* @param {number} t
176175
* @return {number}
177176
*/
178-
RecentCounter.prototype.ping = function(t) {
179-
this.q.push(t);
180-
while (this.q[0] < t - 3000) {
181-
this.q.shift();
182-
}
183-
return this.q.length;
177+
RecentCounter.prototype.ping = function (t) {
178+
this.q.push(t);
179+
while (this.q[0] < t - 3000) {
180+
this.q.shift();
181+
}
182+
return this.q.length;
184183
};
185184

186185
/**

0 commit comments

Comments
 (0)