Leetcode經典面試題 – 第1周
題目來源于Leetcode
報了一個百面機器學習面試的課程
每周都有定時打卡的作業
都是常出現于面試中的題
總結在此,時常溫習,
刷題小能手們覺得寫的不錯可以移步個人首頁
(ps:最近忙着筆試面試,更新太少)
1.雙指針(Leetcode 167)
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
給定一個遞增的數組,查找和等于目标值的兩個數
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
思路
題目設計想用雙指針解決,雙指針複雜度O(n^2),我這裡用字典解決這個問題,能減到O(n)的時間複雜度。具體就是,周遊目前的數組,查找目标值與目前周遊的數的差在不在數組中,在的話傳回索引即可
class Solution:
def twoSum(self, nums, target):
dict = {}
first = -1
second = -1
for i in range(len(nums)):
complement = target - nums[i]
if complement in dict:
first = dict[complement]
second = i
break
dict[nums[i]] = i
return first+1, second+1
2.排序(Leetcode 215)
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
在沒有排序的數組中找到第k大的數
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
思路
題目讓我排序,還是偷懶,直接調用,然後切片索引
(能解決問題幹嘛繞路,但是假如初學還是老實從冒泡到快排都寫一遍吧,嘿嘿)
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
nums.sort()
return nums[-k]
3.桶排序(Leetcode 347)
Given a non-empty array of integers, return the k most frequent elements.
給一個非空數組,傳回前k個頻繁出現的元素
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
思路
題目讓我桶排序,桶排序指的是設定最小值到最大值的區間,指定n個桶,将待排序的數放入這些制定好的區間(桶)中,最後将區間中的數依次拿出即可
還是偷懶,用字典的值對應出現的次數,最後傳回即可
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
d = {}
for i in range(len(nums)):
d[nums[i]] = d.get(nums[i], 0) + 1
res = []
for i in range(k):
res.append(max(d, key=d.get))
d[res[i]] = 0
return res
4.貪心(Leetcode 455)
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
發餅幹,每個孩子可能最少想要gi個餅幹,每塊餅幹j,尺寸為sj,如果sj>=gi,可以給那個孩子,孩子就滿足了。可以盡可能的滿足多少個孩子。
Example 1:
Input: [1,2,3], [1,1]
Output: 1
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.
Example 2:
Input: [1,2], [1,2,3]
Output: 2
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
思路
貪心問題,兩個數組升序排序,盡可能滿足條件的數量,根據貪心算法的解釋,也就是局部最優解,它就是答案
class Solution(object):
def findContentChildren(self, g, s):
g.sort()
s.sort()
i, j = 0, 0
while i < len(g) and j < len(s):
if s[j] >= g[i]:
i += 1
j += 1
return i