我理解Big-O表示法,但我不知道如何为许多函数计算它.特别是,我一直试图弄清楚Fibonacci序列的幼稚版本的计算复杂性:
int Fibonacci(int n)
{
if (n <= 1)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
Run Code Online (Sandbox Code Playgroud)
Fibonacci序列的计算复杂度是多少以及如何计算?
我正在为想要学习动态编程的人寻找一个易于理解的例子.关于什么是动态编程,这里有很好的答案.斐波那契序列是一个很好的例子,但它太小而不能划伤表面.它看起来是一个很好的主题,虽然我尚未参加算法课程,但希望它在我的春季名单上.
我想编写一个 Python 代码来计算输入中两个字符串之间的最长公共子串。
例子:
word1 = input('Give 1. word: xlaqseabcitt')
word2 = input('Give 2. word: peoritabcpeor')
Run Code Online (Sandbox Code Playgroud)
想要的输出:
abc
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这样的代码:
word1 = input("Give 1. word: ")
word2 = input("Give 2. word: ")
longestSegment = ""
tempSegment = ""
for i in range(len(word1)):
if word1[i] == word2[i]:
tempSegment += word1[i]
else:
tempSegment = ""
if len(tempSegment) > len(longestSegment):
longestSegment = tempSegment
print(longestSegment)
Run Code Online (Sandbox Code Playgroud)
当 word2 比 word1 短时,我最终会遇到 IndexError,并且它没有给我公共子字符串。
编辑:我找到了这个解决方案:
string1 = input('Give 1. word: ')
string2 = input('Give 2. word: ') …Run Code Online (Sandbox Code Playgroud) 在性能关键的并行代码中,我有一个向量,其元素是:
目前,我预先计算了向量的所有可能值,因此竞争条件不应成为问题.为了提高性能,我正在考虑创建一个惰性向量,这样代码只在请求向量元素时执行计算.在并行区域中,可能会发生多个线程同时请求并且可能同时计算相同元素的情况.我如何处理这种可能的竞争条件?
下面是我想要实现的一个例子.它在Windows 10,Visual Studio 17下编译和运行.我使用C++ 17.
// Lazy.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <math.h>
const double START_SUM = 1;
const double END_SUM = 1000;
//base object responsible for providing the values
class Evaluator
{
public:
Evaluator() {};
~Evaluator() {};
//Function with deterministic output, depending on the position
virtual double expensiveFunction(int pos) const = 0;
};
// …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段,我需要(大规模)加速.它是非常低效的.
possible_combos.append([comb for comb in
itertools.combinations_with_replacement(input_list, target_number_of_packages)
if np.sum([j[1] for j in comb])==target_number_of_packages])
Run Code Online (Sandbox Code Playgroud)
拆解:
possible_combos
Run Code Online (Sandbox Code Playgroud)
是输出
input_list
Run Code Online (Sandbox Code Playgroud)
是一个元组列表([...],number_of_packages)
target_number_of_packages
Run Code Online (Sandbox Code Playgroud)
是我需要达到的包数.我可以根据需要组合列表"input_list"的多个元素(允许重复),但是在添加"number_of_packages"时需要准确到达target_number_of_packages,否则组合无效.
我在考虑类似的事情
possible_combos.append([comb for comb in
itertools.combinations_with_replacement(input_list, lambda x:x=...)))
Run Code Online (Sandbox Code Playgroud)
但无法填补空白.
我的问题是,是否可以这样使用lambda?我不需要如何处理这个特定用例的答案,因为我已经解决了不同的问题(使用itertools产品作为递归生成器函数),但我仍然想知道,是否有解决方案?
简而言之:是否有可能使用lambda来改变另一个函数内的值对飞?
input_list=[1,2,3,4,5,6] #in minmal form
target_number_of_packages=4
possible_combos should be [[1,1,1,1],[2,1,1],[2,2],[3,1],[4]]
Run Code Online (Sandbox Code Playgroud)
我正在寻找大致相当于,但速度快于的东西,
possible_combos=[comb for comb in
itertools.combinations_with_replacement(input_list) if np.sum(comb)==target_number_of_packages]
Run Code Online (Sandbox Code Playgroud)
只需将np.sum(comb)== target放入itertools.combinations_with_replacement中 - 如果可能的话.
我已经改变了这个问题,因为我以不同的方式解决了潜在的问题,但其中的一部分仍然是我想知道的.由于没有答案,我认为编辑是适当的.
每当我看到计算机竞赛的解决方案时,我总会看到"动态编程"这个术语.我用Google搜索了这个术语并阅读了一些文章,但它们都没有提供编程VS"动态"编程的简单示例.那么"动态"编程与"普通"编程有何不同?(请简单说明!)
我的C代码中有以下等式
k * dl * (1.0 + pHold / centre
+ (pHold * pHold) / (2.0 * centre * centre)
- square / (2.0 * centre))
Run Code Online (Sandbox Code Playgroud)
我知道浮点除法比乘法要贵得多,而且我已经和它搏斗了一段时间.有没有办法重新排列这个来划分一个师?
谢谢
algorithm ×2
python ×2
big-o ×1
c ×1
c++ ×1
c++17 ×1
fibonacci ×1
lambda ×1
math ×1
openmp ×1
optimization ×1
overlapping ×1
performance ×1
python-3.x ×1