题目来源于Leetcode
报了一个百面机器学习面试的课程
每周都有定时打卡的作业
都是常出现于面试中的题
总结在此,时常温习,
刷题小能手们觉得写的不错可以移步个人主页
(ps:最近忙着笔试面试,更新太少)
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
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]
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
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