Skip to content

Commit 0d58266

Browse files
committed
249_Group_Shifted_Strings
359_Logger_Rate_Limiter
1 parent 44ef4ca commit 0d58266

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/228_Summary_Ranges.py) | Detect start and jump, O(n) and O(1) |
8686
| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/243_Shortest_Word_Distance.py) | Update index1 and index2, and check distance, O(n) and O(1) |
8787
| 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/246_Strobogrammatic_Number.py) | Hash table and reverse string, O(n) and O(n) |
88+
| 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/249_Group_Shifted_Strings.py) | Hash and generate hash code for each string, O(n) and O(n) |
8889
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/252_Meeting_Rooms.py) | 1. Sort and compare intervals[i].end with intervals[i+1], O(nlogn) and O(1)<br>2. Sort and check intervals when count >= 2, O(nlogn) and O(n) |
8990
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/259_3Sum_Smaller.py) | 1. Reduce to two sum smaller, then binary search, O(n^2lgn) and O(1)<br>2. Reduce to two sum smaller, then two points, O(n^2) and O(1)|
9091
| 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/266_Palindrome_Permutation.py) | Compute frequency, check number of odd occurrences <= 1 then palindrome, O(n) and O(n)|
@@ -105,6 +106,7 @@
105106
| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) &hearts; |
106107
[Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) |
107108
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
109+
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n)|
108110
| 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/368_Largest_Divisible_Subset.py) | Sort and generate x subset with previous results, O(n^2) and O(n^2) |
109111
| 370 | [Range Addition](https://leetcode.com/problems/range-addition/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/370_Range_Addition.py) | Interval problem with cumulative sums, O(n + k) and O(n) |
110112
| 384 | [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/384_Shuffle_an_Array.py) | Fisher–Yates shuffle, O(n) and O(n) |

python/124_Binary_Tree_Maximum_Path_Sum.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# self.left = None
66
# self.right = None
77

8-
class Solution(object):
8+
9+
class Solution(object):
910
def __init__(self):
1011
self.result = -2147483647
1112

@@ -28,4 +29,4 @@ def getNodeMaxValue(self, node):
2829
# if max left or right < 0 then return 0
2930
if ret > 0:
3031
return ret
31-
return 0
32+
return 0

python/249_Group_Shifted_Strings.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def groupStrings(self, strings):
3+
"""
4+
:type strings: List[str]
5+
:rtype: List[List[str]]
6+
"""
7+
dic = {}
8+
for s in strings:
9+
key = self.hashCode(s)
10+
try:
11+
dic[key].append(s)
12+
except KeyError:
13+
dic[key] = [s]
14+
return dic.values()
15+
16+
def hashCode(self, string):
17+
if string is None or len(string) == 0:
18+
return ''
19+
if len(string) == 1:
20+
return 'a'
21+
step = abs(ord(string[0]) - ord('a'))
22+
if step == 0:
23+
return string
24+
key = 'a'
25+
for ch in string[1:]:
26+
curr = ord(ch) - step
27+
if ord(ch) - step < ord('a'):
28+
curr += 26
29+
key += chr(curr)
30+
return key

python/359_Logger_Rate_Limiter.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# class Logger(object):
2+
3+
# def __init__(self):
4+
# """
5+
# Initialize your data structure here.
6+
# """
7+
# self.timestamps = {}
8+
9+
# def shouldPrintMessage(self, timestamp, message):
10+
# """
11+
# Returns true if the message should be printed in the given timestamp, otherwise returns false.
12+
# If this method returns false, the message will not be printed.
13+
# The timestamp is in seconds granularity.
14+
# :type timestamp: int
15+
# :type message: str
16+
# :rtype: bool
17+
# """
18+
# if timestamp < self.timestamps.get(message, 0):
19+
# return False
20+
# self.timestamps[message] = timestamp + 10
21+
# return True
22+
23+
24+
import heapq
25+
26+
27+
class Logger(object):
28+
29+
def __init__(self):
30+
"""
31+
Initialize your data structure here.
32+
"""
33+
self.heap = []
34+
self.cache = {}
35+
36+
def shouldPrintMessage(self, timestamp, message):
37+
"""
38+
Returns true if the message should be printed in the given timestamp, otherwise returns false.
39+
If this method returns false, the message will not be printed.
40+
The timestamp is in seconds granularity.
41+
:type timestamp: int
42+
:type message: str
43+
:rtype: bool
44+
"""
45+
while len(self.heap):
46+
if self.heap[0][0] <= timestamp:
47+
temp = heapq.heappop(self.heap)
48+
self.cache.pop(temp[1])
49+
else:
50+
break
51+
if timestamp < self.cache.get(message, 0):
52+
return False
53+
self.cache[message] = timestamp + 10
54+
heapq.heappush(self.heap, (timestamp + 10, message))
55+
return True
56+
57+
58+
# Your Logger object will be instantiated and called as such:
59+
# obj = Logger()
60+
# param_1 = obj.shouldPrintMessage(timestamp,message)

0 commit comments

Comments
 (0)