Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1705
Browse files Browse the repository at this point in the history
* Add solutions to lc problem: No.1705.Maximum Number of Eaten Apples
* Update python solutions
  • Loading branch information
yanglbme committed Dec 24, 2021
1 parent 770b85e commit 8384416
Show file tree
Hide file tree
Showing 276 changed files with 1,101 additions and 1,003 deletions.
24 changes: 13 additions & 11 deletions lcci/01.02.Check Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Solution:
n1, n2 = len(s1), len(s2)
if n1 != n2:
return False
counter = collections.Counter()
counter = Counter()
for i in range(n1):
counter[s1[i]] += 1
counter[s2[i]] -= 1
Expand Down Expand Up @@ -84,16 +84,18 @@ class Solution {
### **JavaScript**

```js
var CheckPermutation = function(s1, s2) {
let n1 = s1.length, n2 = s2.length;
if (n1 != n2) return false;
let counter = {};
for (let i = 0; i < n1; i++) {
let cur1 = s1.charAt(i), cur2 = s2.charAt(i);
counter[cur1] = (counter[cur1] || 0) + 1;
counter[cur2] = (counter[cur2] || 0) - 1;
}
return Object.values(counter).every(v => v == 0);
var CheckPermutation = function (s1, s2) {
let n1 = s1.length,
n2 = s2.length;
if (n1 != n2) return false;
let counter = {};
for (let i = 0; i < n1; i++) {
let cur1 = s1.charAt(i),
cur2 = s2.charAt(i);
counter[cur1] = (counter[cur1] || 0) + 1;
counter[cur2] = (counter[cur2] || 0) - 1;
}
return Object.values(counter).every((v) => v == 0);
};
```

Expand Down
24 changes: 13 additions & 11 deletions lcci/01.02.Check Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Solution:
n1, n2 = len(s1), len(s2)
if n1 != n2:
return False
counter = collections.Counter()
counter = Counter()
for i in range(n1):
counter[s1[i]] += 1
counter[s2[i]] -= 1
Expand Down Expand Up @@ -82,16 +82,18 @@ class Solution {
### **JavaScript**

```js
var CheckPermutation = function(s1, s2) {
let n1 = s1.length, n2 = s2.length;
if (n1 != n2) return false;
let counter = {};
for (let i = 0; i < n1; i++) {
let cur1 = s1.charAt(i), cur2 = s2.charAt(i);
counter[cur1] = (counter[cur1] || 0) + 1;
counter[cur2] = (counter[cur2] || 0) - 1;
}
return Object.values(counter).every(v => v == 0);
var CheckPermutation = function (s1, s2) {
let n1 = s1.length,
n2 = s2.length;
if (n1 != n2) return false;
let counter = {};
for (let i = 0; i < n1; i++) {
let cur1 = s1.charAt(i),
cur2 = s2.charAt(i);
counter[cur1] = (counter[cur1] || 0) + 1;
counter[cur2] = (counter[cur2] || 0) - 1;
}
return Object.values(counter).every((v) => v == 0);
};
```

Expand Down
2 changes: 1 addition & 1 deletion lcci/01.02.Check Permutation/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def CheckPermutation(self, s1: str, s2: str) -> bool:
n1, n2 = len(s1), len(s2)
if n1 != n2:
return False
counter = collections.Counter()
counter = Counter()
for i in range(n1):
counter[s1[i]] += 1
counter[s2[i]] -= 1
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.04.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
```python
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = collections.Counter(s)
counter = Counter(s)
cnt = 0
for val in counter.values():
if (val & 1) == 1:
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.04.Palindrome Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
```python
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = collections.Counter(s)
counter = Counter(s)
cnt = 0
for val in counter.values():
if (val & 1) == 1:
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.04.Palindrome Permutation/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = collections.Counter(s)
counter = Counter(s)
cnt = 0
for val in counter.values():
if (val & 1) == 1:
Expand Down
2 changes: 1 addition & 1 deletion lcci/10.02.Group Anagrams/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
chars = collections.defaultdict(list)
chars = defaultdict(list)
for s in strs:
k = ''.join(sorted(list(s)))
chars[k].append(s)
Expand Down
2 changes: 1 addition & 1 deletion lcci/10.02.Group Anagrams/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
chars = collections.defaultdict(list)
chars = defaultdict(list)
for s in strs:
k = ''.join(sorted(list(s)))
chars[k].append(s)
Expand Down
2 changes: 1 addition & 1 deletion lcci/10.02.Group Anagrams/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
chars = collections.defaultdict(list)
chars = defaultdict(list)
for s in strs:
k = ''.join(sorted(list(s)))
chars[k].append(s)
Expand Down
2 changes: 1 addition & 1 deletion lcci/16.02.Words Frequency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ wordsFrequency.get("pen"); //返回1
class WordsFrequency:

def __init__(self, book: List[str]):
self.counter = collections.Counter(book)
self.counter = Counter(book)

def get(self, word: str) -> int:
return self.counter[word]
Expand Down
2 changes: 1 addition & 1 deletion lcci/16.02.Words Frequency/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ wordsFrequency.get(&quot;pen&quot;); //returns 1
class WordsFrequency:

def __init__(self, book: List[str]):
self.counter = collections.Counter(book)
self.counter = Counter(book)

def get(self, word: str) -> int:
return self.counter[word]
Expand Down
4 changes: 2 additions & 2 deletions lcci/16.02.Words Frequency/Solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class WordsFrequency:

def __init__(self, book: List[str]):
self.counter = collections.Counter(book)
self.counter = Counter(book)

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

# Your WordsFrequency object will be instantiated and called as such:
# obj = WordsFrequency(book)
# param_1 = obj.get(word)
# param_1 = obj.get(word)
6 changes: 3 additions & 3 deletions lcci/17.07.Baby Names/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
```python
class Solution:
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
mp = collections.defaultdict(int)
p = collections.defaultdict(str)
mp = defaultdict(int)
p = defaultdict(str)

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

def union(a, b):
pa, pb = find(a), find(b)
if pa == pb:
Expand Down
6 changes: 3 additions & 3 deletions lcci/17.07.Baby Names/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
```python
class Solution:
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
mp = collections.defaultdict(int)
p = collections.defaultdict(str)
mp = defaultdict(int)
p = defaultdict(str)

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

def union(a, b):
pa, pb = find(a), find(b)
if pa == pb:
Expand Down
4 changes: 2 additions & 2 deletions lcci/17.07.Baby Names/Solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]:
mp = collections.defaultdict(int)
p = collections.defaultdict(str)
mp = defaultdict(int)
p = defaultdict(str)

def find(x):
if p[x] != x:
Expand Down
4 changes: 2 additions & 2 deletions lcof/面试题37. 序列化二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Codec:
"""
if not root:
return '[]'
queue = collections.deque()
queue = deque()
queue.append(root)
res = []
while queue:
Expand All @@ -74,7 +74,7 @@ class Codec:
"""
if not data or data == '[]':
return None
queue = collections.deque()
queue = deque()
nodes = data[1:-1].split(',')
root = TreeNode(nodes[0])
queue.append(root)
Expand Down
11 changes: 5 additions & 6 deletions lcof/面试题37. 序列化二叉树/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class Codec:

def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root:
return '[]'
queue = collections.deque()
queue = deque()
queue.append(root)
res = []
while queue:
Expand All @@ -27,17 +27,16 @@ def serialize(self, root):
else:
res.append('null')
return f'[{",".join(res)}]'


def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if not data or data == '[]':
return None
queue = collections.deque()
queue = deque()
nodes = data[1:-1].split(',')
root = TreeNode(nodes[0])
queue.append(root)
Expand All @@ -57,4 +56,4 @@ def deserialize(self, data):

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))
# codec.deserialize(codec.serialize(root))
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ s = ""
```python
class Solution:
def firstUniqChar(self, s: str) -> str:
counter = collections.Counter(s)
counter = Counter(s)
for c in s:
if counter[c] == 1:
return c
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def firstUniqChar(self, s: str) -> str:
counter = collections.Counter(s)
counter = Counter(s)
for c in s:
if counter[c] == 1:
return c
Expand Down
2 changes: 1 addition & 1 deletion lcof/面试题59 - I. 滑动窗口的最大值/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ for i in range(n):
```python
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
q, res = collections.deque(), []
q, res = deque(), []
for i, num in enumerate(nums):
if q and i - k + 1 > q[0]:
q.popleft()
Expand Down
2 changes: 1 addition & 1 deletion lcof/面试题59 - I. 滑动窗口的最大值/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
q, res = collections.deque(), []
q, res = deque(), []
for i, num in enumerate(nums):
if q and i - k + 1 > q[0]:
q.popleft()
Expand Down
2 changes: 1 addition & 1 deletion lcof2/剑指 Offer II 033. 变位词组/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
chars = collections.defaultdict(list)
chars = defaultdict(list)
for s in strs:
k = ''.join(sorted(list(s)))
chars[k].append(s)
Expand Down
2 changes: 1 addition & 1 deletion lcof2/剑指 Offer II 033. 变位词组/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
chars = collections.defaultdict(list)
chars = defaultdict(list)
for s in strs:
k = ''.join(sorted(list(s)))
chars[k].append(s)
Expand Down
25 changes: 12 additions & 13 deletions lcof2/剑指 Offer II 042. 最近请求次数/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ recentCounter.ping(3002); // requests = [1, <strong>100</strong>, <strong>3001<

<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>


## 解法

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

def __init__(self):
self.q = collections.deque()
self.q = deque()

def ping(self, t: int) -> int:
self.q.append(t)
Expand All @@ -92,7 +91,7 @@ class RecentCounter {
public RecentCounter() {
q = new LinkedList<>();
}

public int ping(int t) {
q.offerLast(t);
while (q.peekFirst() < t - 3000) {
Expand All @@ -119,7 +118,7 @@ public:
RecentCounter() {

}

int ping(int t) {
q.push_back(t);
while (q.front() < t - 3000) {
Expand Down Expand Up @@ -167,20 +166,20 @@ func (this *RecentCounter) Ping(t int) int {
### **JavaScript**

```js
var RecentCounter = function() {
this.q = [];
var RecentCounter = function () {
this.q = [];
};

/**
/**
* @param {number} t
* @return {number}
*/
RecentCounter.prototype.ping = function(t) {
this.q.push(t);
while (this.q[0] < t - 3000) {
this.q.shift();
}
return this.q.length;
RecentCounter.prototype.ping = function (t) {
this.q.push(t);
while (this.q[0] < t - 3000) {
this.q.shift();
}
return this.q.length;
};

/**
Expand Down
Loading

0 comments on commit 8384416

Please sign in to comment.