腾讯算法笔试第一题——消消乐

我的思路:我觉得应该是记录每个元素的个数,存为count列表,然后再找这个列表中是不是存在若干个元素之和等于len(count)/2,我这个思路能过测试用例,但是最后还是0,我也不清楚为什么。求大神解答。
代码 0%:
T = int(input())
# T 组数据
for t in range(T):
n = int(input())
a = list(map(int, input().split()))
# 存取个元素个数
dic = {}
for i in range(n):
if a[i] not in dic:
dic[a[i]] = 1
else:
dic[a[i]] += 1
l = len(list(set(a)))
count = list(dic.values())
count.sort()
target = sum(count) // 2
if l == n:
print("YES")
continue
if count[-1] > target:
print("NO")
continue
if target in count:
print("YES")
continue
else:
for start in range(l):
flag = 0
if flag == 0:
for end in range(start+1, l):
if sum(count[start:end+1]) == target:
print("YES")
flag = 1
break
elif sum(count[start:end+1]) < target:
continue
else:
break
else:
break
if flag == 0:
print("NO")
continue
#腾讯##笔试题目#
全部评论
我也是这样的思路 但是只有0.1 不知道为什么
点赞 回复 分享
发布于 2019-09-01 22:20
zushu=int(input()) while(zushu):     arrLen = int(input())     arr = input().split(" ")     dicts = {}     a = 1      for item in arr:         if item in dicts:             dicts[item]+=1          else:             dicts[item]=1     for key in dicts:           if dicts[key] > arrLen//2 :                 a = 0      if a==1:         print("YES")     else:         print("NO")     zushu-=1
点赞 回复 分享
发布于 2019-09-01 22:20
腾讯的题,时间要求特别高
点赞 回复 分享
发布于 2019-09-01 22:18

相关推荐

买蜜雪也用卷:我觉得应该没有哪个人敢说自己熟练使用git,代码分支一复杂还是得慢慢寻思一下的,不过基本的拉代码提交代码还有分支什么的是应该会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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