KK's blog

每天积累多一些

0%

LeetCode



Design a hit counter which counts the number of hits received in the past 5 minutes (i.e., the past 300 seconds).

Your system should accept a timestamp parameter (in seconds granularity), and you may assume that calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing). Several hits may arrive roughly at the same time.

Implement the HitCounter class:

HitCounter() Initializes the object of the hit counter system. void hit(int timestamp) Records a hit that happened at timestamp (in seconds). Several hits may happen at the same timestamp.
int getHits(int timestamp) Returns the number of hits in the past 5 minutes from timestamp (i.e., the past 300 seconds).

Example 1:

Input
[“HitCounter”, “hit”, “hit”, “hit”, “getHits”, “hit”, “getHits”, “getHits”]
[[], [1], [2], [3], [4], [300], [300], [301]]
Output
[null, null, null, null, 3, null, 4, 3]

Explanation
HitCounter hitCounter = new HitCounter();
hitCounter.hit(1); // hit at timestamp 1.
hitCounter.hit(2); // hit at timestamp 2.
hitCounter.hit(3); // hit at timestamp 3.
hitCounter.getHits(4); // get hits at timestamp 4, return 3.
hitCounter.hit(300); // hit at timestamp 300.
hitCounter.getHits(300); // get hits at timestamp 300, return 4.
hitCounter.getHits(301); // get hits at timestamp 301, return 3.


Constraints:
1 <= timestamp <= 2 * 10<sup>9</sup>
All the calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing). At most 300 calls will be made to hit and getHits.

Follow up: What if the number of hits per second could be huge? Does your design scale?

题目大意:

设计统计hits系统。题目要求:同一个时间可以有多个hits,hit是按时间顺序的。

解题思路:

用一个固定大小为300的数组来记录timestamp和对应的hits的总数

解题步骤:

N/A

注意事项:

  1. 题目要求:同一个时间可以有多个hits,hit是按时间顺序的。所以固定数组只要比较现在的timestamp是否和last_timestamp一样,不是的话reset hit。用循环数组记录
  2. getHist是统计300以内(不包括300)的hit数。

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class HitCounter(TestCases):

def __init__(self):
self.hits = [(0, 0)] * 300

def hit(self, timestamp: int) -> None:
last_timestamp, count = self.hits[timestamp % 300]
if last_timestamp and timestamp != last_timestamp:
self.hits[timestamp % 300] = (timestamp, 0)
count = 0
count += 1
self.hits[timestamp % 300] = (timestamp, count)

def getHits(self, timestamp: int) -> int:
res = 0
for t, count in self.hits:
if t and timestamp - t < 300:
res += count
return res

算法分析:

hit时间复杂度为O(1),getHits时间复杂度为O(1),空间复杂度O(1)

LeetCode



Given an array of strings words and an integer k, return the k most frequent strings.

Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.

Example 1:

Input: words = [“i”,”love”,”leetcode”,”i”,”love”,”coding”], k = 2
Output: [“i”,”love”]
Explanation: “i” and “love” are the two most frequent words.
Note that “i” comes before “love” due to a lower alphabetical order.


Example 2:

Input: words = [“the”,”day”,”is”,”sunny”,”the”,”the”,”the”,”sunny”,”is”,”is”], k = 4
Output: [“the”,”is”,”sunny”,”day”]
Explanation: “the”, “is”, “sunny” and “day” are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.


Constraints:

1 <= words.length <= 500 1 <= words[i] <= 10
words[i] consists of lowercase English letters. k is in the range [1, The number of **unique** words[i]]

Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?

题目大意:

求k个最高频率的单词

解题思路:

N/A

解题步骤:

N/A

注意事项:

  1. 若频率一样,就按字母顺序lexicographical. 所以用大小为k的heap做比较困难。直接用排序即可

Python代码:

1
2
3
4
5
def topKFrequent(self, words: List[str], k: int) -> List[str]:
freq_dict = collections.Counter(words)
li = [(freq, word) for word, freq in freq_dict.items()]
li.sort(key=lambda x : (-x[0], x[1]))
return [pair[1] for pair in li[:k]]

算法分析:

时间复杂度为O(nlogn),空间复杂度O(n)

LeetCode



Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000


For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

Example 1:

Input: num = 3
Output: “III”
Explanation: 3 is represented as 3 ones.


Example 2:

Input: num = 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.


Example 3:

Input: num = 1994
Output: “MCMXCIV”
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.


Constraints:
1 <= num <= 3999

题目大意:

阿拉伯数字转罗马数字

解题思路(推荐):

本质上和算法二一样,但优化了代码。map的内容是一样的但变成list保证顺序,然后从大到小遍历这个map,商对应的symbol放入结果,余数进入下一轮

注意事项:

  1. map的内容是一样的但变成list保证顺序,然后从大到小遍历这个map,商对应的symbol放入结果,余数进入下一轮

Python代码:

1
2
3
4
5
6
7
8
9
def intToRoman(self, num: int) -> str:
INT_TO_ROMAN = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'),
(90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'),
(4, 'IV'), (1, 'I')]
res = ''
for n, symbol in INT_TO_ROMAN:
count, num = num // n, num % n
res += symbol * count
return res

算法分析:

时间复杂度为O(1),空间复杂度O(1)


算法II解题思路:

N/A

解题步骤:

N/A

注意事项:

  1. 用int to english的递归方法,将固定值放入到Map中
  2. 分界点为[4, 5, 9 10], [40, 50, 90, 100]进行递归

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def intToRoman2(self, num: int) -> str:
INT_TO_ROMAN = {
0: '',
4: 'IV',
5: 'V',
9: 'IX',
10: 'X',
40: 'XL',
50: 'L',
90: 'XC',
100: 'C',
400: 'CD',
500: 'D',
900: 'CM',
1000: 'M',
}
if num in INT_TO_ROMAN:
return INT_TO_ROMAN[num]
elif num < 4:
return 'I' * num
elif num < 9:
return self.intToRoman(5) + self.intToRoman(num - 5)
elif num < 40:
return 'X' * (num // 10) + self.intToRoman(num % 10)
elif num < 50:
return self.intToRoman(40) + self.intToRoman(num - 40)
elif num < 90:
return self.intToRoman(50) + self.intToRoman(num - 50)
elif num < 100:
return self.intToRoman(90) + self.intToRoman(num % 90)
elif num < 400:
return 'C' * (num // 100) + self.intToRoman(num % 100)
elif num < 500:
return self.intToRoman(400) + self.intToRoman(num - 400)
elif num < 900:
return self.intToRoman(500) + self.intToRoman(num - 500)
elif num < 1000:
return self.intToRoman(900) + self.intToRoman(num % 900)
else:
return 'M' * (num // 1000) + self.intToRoman(num % 1000)

算法分析:

时间复杂度为O(1),空间复杂度O(1)

LeetCode 138 Copy List with Random Pointer

A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

  • val: an integer representing Node.val
  • random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.

Your code will only be given the head of the original linked list.

Example 1:

**Input:** head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]]

Example 2:

**Input:** head = [[1,1],[2,1]]
**Output:** [[1,1],[2,1]]

Example 3:

**Input:** head = [[3,null],[3,0],[3,null]]
**Output:** [[3,null],[3,0],[3,null]]

Example 4:

**Input:** head = []
**Output:** []
**Explanation:** The given linked list is empty (null pointer), so return null.

Constraints:

  • 0 <= n <= 1000
  • -10000 <= Node.val <= 10000
  • Node.random is null or is pointing to some node in the linked list.

题目大意:

复制含next和random的链表。

解题思路(推荐):

此法较容易实现。先复制next指针,然后利用HashMap存储旧新节点,来复制random指针。

注意事项:

  1. 复制next指针和Map中。clone题均用此法。
  2. Random指针不空才copy
  3. 加it = it.next,否则死循环
  4. 如果创建新Node用while it.next表示用它的父节点,否则某个field赋值如random用while it

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def copyRandomList(self, head: 'Node') -> 'Node':
node_map = {}
fake_head, fake_head_copy = Node(0), Node(0)
fake_head.next = head
it, it_copy = fake_head, fake_head_copy
while it.next:
it_copy.next = Node(it.next.val)
node_map[it.next] = it_copy.next
it, it_copy = it.next, it_copy.next

it, it_copy = fake_head.next, fake_head_copy.next
while it:
if it.random:
node_map[it].random = node_map[it.random]
it, it_copy = it.next, it_copy.next
return fake_head_copy.next

梅花间竹解题思路:

第二种方法,梅花间竹,分3部走。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def copyRandomList(self, head: 'Node') -> 'Node':
fake_head, fake_head_copy = Node(0), Node(0)
fake_head.next = head

# insert
it = fake_head.next
while it:
temp = it.next
it.next = Node(it.val)
it.next.next = temp
it = it.next.next

# copy random
it = fake_head.next
while it:
if it.random is not None:
it.next.random = it.random.next
it = it.next.next

# delete
it, it_copy = fake_head.next, fake_head_copy
while it:
temp = it.next
it.next = it.next.next
it_copy.next = temp
temp.next = None
it, it_copy = it.next, it_copy.next
return fake_head_copy.next

算法分析:

  1. 时间复杂度为O(n),空间复杂度O(n)
  2. 时间复杂度为O(n),空间复杂度O(1)

LeetCode



You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]:

numberOfBoxes<sub>i</sub> is the number of boxes of type i. numberOfUnitsPerBox<sub>i</sub>is the number of units in each box of the type i.

You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.

Return the maximum total number of units that can be put on the truck.

Example 1:

Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation: There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 3) + (2 2) + (1 1) = 8.


Example 2:

Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91


Constraints:
1 <= boxTypes.length <= 1000
1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000 1 <= truckSize <= 10<sup>6</sup>

题目大意:

货车能装的最大unit数,每种类型的盒都能装一定数量的units,而每种盒子占地方一样。

解题思路:

由于每种盒子占地一样,所以当然是先放unit大的。贪婪法。

解题步骤:

N/A

注意事项:

  1. 按unit数倒序排序
  2. pair是一个数组,要加pair[i][0]

Python代码:

1
2
3
4
5
6
7
8
9
10
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
pairs = [(li[1], li[0]) for li in boxTypes]
pairs.sort(reverse=True)
res, i = 0, 0
for pair in pairs:
res += pair[0] * min(pair[1], truckSize)
truckSize -= pair[1]
if truckSize <= 0:
break
return res

算法分析:

时间复杂度为O(nlogn),空间复杂度O(n)

Free mock interview