难度
基于PYTHON
项目 | 难度 |
---|---|
达到终点 | 3/5 |
各位相加 | 1/5 |
两数组的交集 | 1/5 |
算法学习 | 2/5 |
1.到达终点
描述
A robot is located in a pair of integer coordinates (x, y). It must be moved to a location with another set of coordinates. Though the bot can move any number of times, it can only make the following two types of moves:
From location (x, y) to location (x + y, y)
From location (x, y) to location (x, x + y)
Given the start coordinates and the end coordinates of the target, if the robot can reach the end coordinates according to the given motion rules, you should return true, otherwise you should return false.
x 和 y 的范围是: [1, 1 000 000 000]
移动的次数可以是0次。
示例
示例 1:
输入:
start = [1, 1]
target = [5, 2]
输出:
True
解释:
(1, 1) -> (1, 2) -> (3, 2) -> (5, 2), 你应该返回True
示例 2:
输入:
start = [1, 2]
target = [3, 4]
输出:
False
解释:
[1, 2] 根据移动规则不能到达 [3, 4] 所以你应该返回False.
示例 3:
输入:
start = [2, 1]
target = [4, 1]
输出:
True
解释:
(2, 1) -> (3, 1) -> (4, 1), 你应该返回True.
解题思路
- 类似二叉树,通过目标坐标互减,判断某个树叶是否是起点坐标
- 难点在计算速度,需要加快树叶的生成,
class Solution:
"""
@param start: a point [x, y]
@param target: a point [x, y]
@return: return True and False
"""
#判断某个树叶是否是起点坐标
def is_target(self,target,start):
if (target[0]-start[0]) % target[1] == 0 and target[1] == start[1] and target[0] >= start[0]:
return True
if (target[1]-start[1]) % target[0] == 0 and target[0] == start[0] and target[1] >= start[1]:
return True
return False
#快速生成树叶
def speed_tree(self,target_tree,start):
new_tree = []
for i in target_tree:
if self.is_target(i,start):
return True
if i[0]>i[1] and i[0]%i[1] > 0 and i[0]%i[1] >= start[0] and i[1] >= start[1] :
new_tree.append([i[0]%i[1],i[1]])
if i[1]>i[0] and i[1]%i[0] > 0 and i[0] >= start[0] and i[1]%i[0] >= start[1]:
new_tree.append([i[0],i[1]%i[0]])
return new_tree
def ReachingPoints(self, start, target):
# write your code here
if start == target:
return True
tree = [target]
while len(tree)>0 :
print(tree)
tree = self.speed_tree(tree, start)
if tree == True:
return True
return False
2.各位相加
描述
给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数。
示例
例1:
输入:
num=38
输出:
2
解释:
过程如下: 3 + 8 = 11, 1 + 1 = 2. 因为 2 只有一个数字,返回 2.
例2:
输入:
num=9
输出:
9
解释:
9<10,返回 9.
解题思路
- 通过字符串截取获取各位数字,反复求和
class Solution:
"""
@param num: a non-negative integer
@return: one digit
"""
def addDigits(self, num):
# write your code here
while num >= 10 :
num = self.calculate(num)
return num
def calculate(self, num):
s = 0
for i in range(len(str(num))):
s = s + int(str(num)[i])
return s
3.两数组的交集
描述
给出两个数组,写出一个方法求出它们的交集
结果中的每个元素必须是唯一的。
示例
例1:
输入: nums1 = [1, 2, 2, 1], nums2 = [2, 2],
输出: [2].
例2:
输入: nums1 = [1, 2], nums2 = [2],
输出: [2].
解题思路
- 两个数组分别排序,去重;合成新数组
- 新数组排序,遍历获取交集
- 说明:上述方法较为粗糙,不知是否有更好的解法
class Solution:
"""
@param nums1:
@param nums2:
@return: an integer array
"""
def intersection(self, nums1, nums2):
# write your code here
nums1.sort()
nums2.sort()
i=1
# 去重
while i < len(nums1):
if nums1[i-1] == nums1[i]:
nums1.pop(i)
else:
i=i+1
i=1
# 去重
while i < len(nums2):
if nums2[i-1] == nums2[i]:
nums2.pop(i)
else:
i=i+1
nums3 = nums1 + nums2
nums3.sort()
num_sum= []
i = 1
# 获取交集
while i < len(nums3):
if nums3[i-1] == nums3[i]:
num_sum.append(nums3[i-1])
i=i+1
return num_sum
num_a = [];
num_b = [];
4.算法学习
描述
现在,你有n个任务需要做。每个任务都有对应的奖励val,这意味着你只要完成了这个任务,就能获得对应的奖励。每个任务也有一个完成期限date,你只有在期限内(<=date[i])完成了这个任务,才能获得奖励。每个任务只能被完成一次,一天最多只能做一个任务。请你合理的分配任务,以获得最多的奖励。
0 <= n <= 10000
1 <= val[i] <= 10000
1 <= date[i] <= 10000
示例
例1:
输入:[50,60,70],[3,1,1]
输出:120
解释:
对于第1个任务,你能在第1天,第2天,第3天中选择1天去完成。
对于第2个任务,你只能选择第1天去完成。
对于第3个任务,你只能选择第1天去完成。
所以,我们选择第1天完成任务3,第2天完成任务1,50+70=120。任务2无法完成。
例2:
输入:[1,5,9],[1,1,1]
输出:9
解释:
对于第1个任务,你只能选择第1天去完成。
对于第2个任务,你只能选择第1天去完成。
对于第3个任务,你只能选择第1天去完成。
所以,我们选择第1天完成任务3。任务1和2无法完成。
解题思路
- 按完成天数从大到小排序
- 在时限内任务,优先选择价值高的
- 重点是2个数组排序的效率
class Solution:
"""
@param val: the val of each task
@param date: the deadline of each task
@return: sum of val
"""
def algorithmLearning(self, val, date):
# Write your code here.
list_val,list_date = self.list_sort(val,date)
#print(list_val)
#print(list_date)
val_sum=0
#剩余时间
left_date=list_date[0]
while ( left_date > 0 and len(list_date) > 0) :
index = -1
for i in range(len(list_date)):
if list_date[i] >= left_date :
index = i
#print(left_date)
if index >= 0:
val_current = max(list_val[:index+1])
val_sum = val_sum + val_current
index_need_pop = list_val.index(val_current)
list_val.pop(index_need_pop)
list_date.pop(index_need_pop)
left_date = left_date - 1
else:
left_date = list_date[0]
#print(left_date,index,val_current)
#print(val_sum)
return val_sum
"""
@func: 按天数从大到小排序;冒泡速度无法满足,改为系统排序sort()
"""
def list_sort(self,list_val,list_date):
"""
for i in range(len(list_val)-1):
for j in range(i,len(list_val)):
if list_date[i] < list_date[j]:
list_date[i],list_date[j] = list_date[j],list_date[i]
list_val[i],list_val[j] = list_val[j],list_val[i]
"""
c = [[list_date[i],list_val[i]] for i in range(len(list_val))]
c.sort(reverse=True)
return [c[i][1] for i in range(len(c))],[c[i][0] for i in range(len(c))]
题目来自于互联网平台,供学习交流使用,如有侵权,请联系本人删除,谢谢