Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我不知道';I don’我不认为它会花费那么多时间(对于-if条件检查循环),有更好的方法吗?_Python_For Loop_If Statement - Fatal编程技术网

Python 我不知道';I don’我不认为它会花费那么多时间(对于-if条件检查循环),有更好的方法吗?

Python 我不知道';I don’我不认为它会花费那么多时间(对于-if条件检查循环),有更好的方法吗?,python,for-loop,if-statement,Python,For Loop,If Statement,我认为这不会花太多时间,但所花的时间比我预期的要多 我认为如果是问题 是什么让它花费了很多时间 有没有更好的方法来减少时间 问题是 n=花的数量 a、 b=花的开花日期(月、日) c、 d=花卉的秋季日期(月、日) (当花落下的时候,你看不到那朵花) (如果0530是秋天,那么你在0530看不到那朵花) 从0301年到1130年,我每天都想看到鲜花 打印所需花的最小值 有人告诉我这是贪心算法的问题 我不知道如何减少时间了 我认为这是解决这个问题的正确方法 下面是我的代码 flo_list=[]

我认为这不会花太多时间,但所花的时间比我预期的要多

我认为如果是问题

是什么让它花费了很多时间

有没有更好的方法来减少时间

问题是

n=花的数量

a、 b=花的开花日期(月、日) c、 d=花卉的秋季日期(月、日)

(当花落下的时候,你看不到那朵花) (如果0530是秋天,那么你在0530看不到那朵花)

从0301年到1130年,我每天都想看到鲜花

打印所需花的最小值

有人告诉我这是贪心算法的问题

我不知道如何减少时间了

我认为这是解决这个问题的正确方法

下面是我的代码
flo_list=[]
n=int(输入())
对于范围(n)中的i:
a、 b,c,d=map(int,input().split())
flo_list.append((a*100+b,c*100+d))
flo_list.sort(reverse=True)
cnt=0
srt_flo=排序(flo_列表,key=lambda i:i[1],reverse=True)
如果flo_列表[-1][0]>301:
打印(0)
退出()
如果srt_flo[0][1]<1201:
打印(0)
退出()
对于srt_flo中的(i,j):

如果我你应该试试这样的东西:

from itertools import combinations

for comb_len in range(1,n) :    # `n` -- is the total number of flowers
    for comb in combinations( range(n), comb_len ) :
        if this_combination_covers_all_dates( comb ) :
            print 'need', comb_len, 'flowers', comb

这个组合的实现覆盖了所有日期(comb)
取决于您=)

您应该尝试以下方法:

from itertools import combinations

for comb_len in range(1,n) :    # `n` -- is the total number of flowers
    for comb in combinations( range(n), comb_len ) :
        if this_combination_covers_all_dates( comb ) :
            print 'need', comb_len, 'flowers', comb
此组合的实现覆盖了所有日期(comb)
由您决定=)