【2019校招真题】挑选代表(python)

挑选代表_牛客网

https://www.nowcoder.com/practice/c563cc42459d49d5923b3460ba142cf8?tpId=98&tqId=32874&tPage=3&rp=3&ru=/ta/2019test&qru=/ta/2019test/question-ranking

解题思路:

贪心算法

完整代码:

n = int(input())
nums = []
for _ in range(n):
  nums.append(list(map(int, input().split())))
nums.sort(key=lambda x:x[1])
temp = []
temp.append(nums[0][1] - 1)
temp.append(nums[0][1])
for i in range(n - 1):
  if nums[i + 1][0] == temp[-1]:
    temp.append(nums[i + 1][1])
  elif nums[i + 1][0] > temp[-1]:
    temp.append(nums[i + 1][1] - 1)
    temp.append(nums[i + 1][1])
print(len(temp))

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务