鱼C论坛

 找回密码
 立即注册
楼主: zltzlt

[已解决]Python:每日一题 352

[复制链接]
发表于 2020-3-16 14:22:54 | 显示全部楼层
楼主,你的题是从LeetCode里面找的吗?
我哥给我讲过里面的题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 14:43:54 | 显示全部楼层
蒋博文 发表于 2020-3-16 14:22
楼主,你的题是从LeetCode里面找的吗?
我哥给我讲过里面的题

是的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 14:49:36 | 显示全部楼层

还是abc,xyz能接受点。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 14:58:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 15:19:15 | 显示全部楼层    本楼为最佳答案   
zltzlt 发表于 2020-3-16 13:23
解答错误

输入:[1, 2, 99]

已修改 如下
def fun352(arr):
    M = len(arr)
    if M == 1:
        return True
    dictionary = dict()#存储形式 (起始,终止):(先手,后手)  起始终止 和切片一致
    for index in range(0,M-1):
        A = arr[index]
        B = arr[index+1]
        if A < B:
            dictionary[(index,index+2)]=(B,A)
        else:
            dictionary[(index,index+2)]=(A,B)
    for index in range(3,M+1):
        for inner in range(0,M+1-index):
            choose1 = arr[inner]
            before1 = dictionary[(inner+1,inner+index)][0]
            after1 = dictionary[(inner+1,inner+index)][1]
            result1 = (choose1+after1,before1)
            
            choose2 = arr[inner+index-1]
            before2 = dictionary[(inner,inner+index-1)][0]
            after2 = dictionary[(inner,inner+index-1)][1]
            result2 = (choose2+after2,before2)

            if result1[0]>result2[0]:
                dictionary[(inner,inner+index)] = result1
            else:
                dictionary[(inner,inner+index)] = result2
    if dictionary[(0,M)][0]>dictionary[(0,M)][1]:
        return True
    else:
        return False

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 15:21:16 | 显示全部楼层

我觉得平局不应该是True 这题错了就错了 我也不改了。 题目说的是 1号赢家就是True 换言之 没赢 就是False
显然平局是 False 如果我的错了,我也不改了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 16:51:48 | 显示全部楼层
zltzlt 发表于 2020-3-16 13:31
解答错误

输入:[1, 89, 3]

你确定吗?,我输入[1,89,3]是输出的False

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
zltzlt + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 16:57:32 | 显示全部楼层
zltzlt 发表于 2020-3-16 13:34
输入 [1, 1, 1] 结果有误

我第二个答案输入[1,1,1]返回的是True,有问题吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 17:30:03 | 显示全部楼层
难度评级:普通
要素分析:模拟 循环 逻辑
代码:
def solve(lst:'list of int>=0')->bool:
    l = len(lst)
    class Player:
        def __init__(self,game):
            self.game = game
            self.point = 0
        def choise(self):
            nonlocal l
            game = self.game
            if not l:
                return
            if l <= 3:
                self.point += game.pop(0) if game[0]>=game[-1] else game.pop()
            else:
                b1 = game[0]>game[-1]
                b2 = game[1]>game[-2]
                if b1 == b2:#同边
                    if b1:#左大
                        if game[0]+game[-2] < game[-1]+game[1]:
                            self.point += game.pop()
                        else:
                            self.point += game.pop(0)
                    else: #左小
                        if game[0]+game[-2] > game[-1]+game[1]:
                            self.point += game.pop(0)
                        else:
                            self.point += game.pop()
                else:       #异边
                    if b1:#外左大
                        self.point += game.pop(0)
                    else:
                        self.point += game.pop()
            l -= 1
    p1 = Player(lst)
    p2 = Player(lst)
    while lst:
        p1.choise()
        p2.choise()
    return p1.point >= p2.point

if __name__ == '__main__':
    print('示例1 输出:',solve([1,5,2]))
    print('示例2 输出:',solve([1,5,233,7]))
附上一个输入数据随机生成器,送给有需要的人。
>>> import random
>>> g = lambda m,n:[random.randint(0,m)for i in range(n)]
>>> g(5,10)
[2, 3, 0, 5, 3, 0, 0, 4, 4, 0]
>>> 
其中,m参数为列表中出现的最大值,n为列表长度,因为我不确认我的解法是否绝对正确,所以无法给出正解的对照答案,请参照确认正解的答案。

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
zltzlt + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 17:49:22 | 显示全部楼层

解答错误

输入:[3606449, 6, 5, 9, 452429, 7, 9580316, 9857582, 8514433, 9, 6, 6614512, 753594, 5474165, 4, 2697293, 8, 7, 1]
输出:True
预期结果:False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 17:50:58 | 显示全部楼层
阴阳神万物主 发表于 2020-3-16 17:30
难度评级:普通
要素分析:模拟 循环 逻辑
代码:

解答错误

输入:[3606449, 6, 5, 9, 452429, 7, 9580316, 9857582, 8514433, 9, 6, 6614512, 753594, 5474165, 4, 2697293, 8, 7, 1]
输出:True
预期结果:False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 17:53:12 | 显示全部楼层
TJBEST 发表于 2020-3-16 15:21
我觉得平局不应该是True 这题错了就错了 我也不改了。 题目说的是 1号赢家就是True 换言之 没赢 就是Fals ...

嗯,一开始没说清楚
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 17:54:21 | 显示全部楼层

如果排除平局的错误答案,44 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 17:54:30 | 显示全部楼层
zltzlt 发表于 2020-3-16 13:33
第二个有错,第一个 6184 ms

什么错误?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 17:55:35 | 显示全部楼层

与示例 1 不相符
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 17:59:11 | 显示全部楼层
zltzlt 发表于 2020-3-16 17:49
解答错误

输入:[3606449, 6, 5, 9, 452429, 7, 9580316, 9857582, 8514433, 9, 6, 6614512, 753594,  ...

我16楼的答案是输出的False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 18:15:28 | 显示全部楼层
26楼的答案有问题,暂时不要去管它
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 19:37:04 | 显示全部楼层
zltzlt 发表于 2020-3-16 13:28
解答错误

输入:[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
def fun352(scores:list):
    if(len(scores) == 0):
        return None
    elif(len(scores) == 1):
        return True
    player1 = 0
    player2 = 0
    Sum = sum(scores)
    def choice(scores:list, Cur:int, Next:int):
        length = len(scores)
        if(length == 0):
            return None
        elif(length == 1):
            return 0
        elif(1 < length < 4):
            return scores.index(max(scores[0],scores[-1]))
        if(scores[1] >= scores[-2]):
            if((scores[1] + min(scores[0],scores[-1]) > scores[-2] + max(scores[0],scores[-1]))):
                return -1
            else:
                return scores.index(max(scores[0],scores[-1]))
        else:
            if((scores[-2] + min(scores[0],scores[-1]) > scores[1] + max(scores[0],scores[-1]))):
                return 0
            else:
                return scores.index(max(scores[0],scores[-1]))
    
    while(scores != []):
        index = choice(scores, player1, player2)
        player1 += scores.pop(index)
        index = choice(scores, player2, player1)
        if(index == None):
            break
        else:
            player2 += scores.pop(index)
    if(player1 >= player2):
        return True
    else:
        return False
忘删一个条件了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-16 19:39:41 | 显示全部楼层
fan1993423 发表于 2020-3-16 17:59
我16楼的答案是输出的False

这就不知道了,我这里测试输出的是 True,你再试一下呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-16 20:02:42 | 显示全部楼层
zltzlt 发表于 2020-3-16 17:50
解答错误

输入:[3606449, 6, 5, 9, 452429, 7, 9580316, 9857582, 8514433, 9, 6, 6614512, 753594,  ...

这……看来是我对“每个玩家的玩法都会使他的分数最大化。”的理解出了问题?!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-26 13:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表