- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在练习一些动态规划,并且遇到了Circus Tower 问题。
我用动态规划解决了这个问题,并使用递归实现了它。我已经用很少的输入对其进行了测试,它似乎工作正常。
现在,我已经苦苦挣扎了几个小时,试图找出如何将内存添加到我的解决方案中。
问题
马戏团塔问题:马戏团正在设计一座塔,人们站在彼此的肩膀上。每个人都必须比他下面的人更矮更轻。给定马戏团中每个人的高度和体重,编写一个方法来计算这样一个塔中的最大可能人数。
我的解决方案和代码
动态规划
OPT[N,P] = highest tower with N given persons and person P is at the top
----------------------------------------------------------
OPT[0,P] = 0
OPT[i,P] where person i can be above P = max(OPT[i-1,i]+1,OPT[i-1,P])
OPT[i,P] else = OPT[i-1,P]
代码:
struct Person{
int ht;
int wt;
};
// Without Memoization
int circusTower(int n, Person* top, std::vector<Person>& persons){
if (n == 0)
return 0;
if (top == NULL || top->ht > persons[n - 1].ht && top->wt > persons[n - 1].wt)
return max(circusTower(n - 1, &persons[n - 1], persons) + 1, circusTower(n - 1, top, persons));
else
return circusTower(n - 1, top, persons);
}
// With Memoization
int circusTower(int n, Person* top, std::vector<Person>& persons, std::vector<int>& memo){
if (n == 0)
return 0;
int result;
if (memo[n-1] == 0) {
if (top == NULL || top->ht > persons[n - 1].ht && top->wt > persons[n - 1].wt)
result = max(circusTower(n - 1, &persons[n - 1], persons, memo) + 1,
circusTower(n - 1, top, persons, memo));
else
result = circusTower(n - 1, top, persons, memo);
memo[n - 1] = result;
return result;
} else {
return memo[n-1];
}
}
主要 - 测试:
int main(){
std::vector<Person> persons = { {65, 100},{100, 150},{56, 90}, {75, 190}, {60, 95},{68, 110} };
std::stable_sort(persons.begin(), persons.end(), sortByWt);
std::stable_sort(persons.begin(), persons.end(), sortByHt);
std::vector<int> memo(6,0);
//Without memoization
cout << circusTower(6, NULL, persons) << endl;
//With memoization
cout << circusTower(6, NULL, persons, memo) << endl;
}
在上面 main 的例子中,正确的结果是 5。我的常规解决方案(没有内存)打印 5,但是有内存它打印 6。
最佳答案
您的方法依赖于 3 个参数但你只从第一个参数 n
确实在你的情况下,第三个(persons
)是不变的,但第二个 (top
) 在递归调用期间发生变化。
所以由于你的内存,以下两个错误地返回相同的值:
circusTower(n - 1, &persons[n - 1], persons, memo)
circusTower(n - 1, top, persons, memo)
关于c++ - 添加内存 - 动态规划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062202/
我正在创建我的第一个 WAR 文件。我一直在试验 ant buildfile 语法,我的 buildfile 的第一部分从我的 Eclipse 项目中获取内容并将其放入 /dist 文件夹中,然后将其
我是一名学习 SQL 和 PHP 的学生,我接到了一项任务,要使用 PHP 和 mySQLi 创建学生反馈表,我真的一直在思考如何为项目设计数据库! 我正在创建一个系统,用户可以在其中登录网页,如果用
这个问题在这里已经有了答案: Is it possbile to test for expected errors when the testee exits with failure using
我目前正在设计和开发一个 Web 应用程序,该应用程序有可能快速增长。我将提供一些一般信息,然后继续我的问题。我会说我是一名中级网络程序员。 以下是一些规范:MySQL - 数据库后端PHP - 用于
我不知何故无法在我的日志解析器应用程序中实现报告功能。 这是我目前所做的: 我正在编写一个应用程序,它读取日志文件并在字符串中搜索可以在用户配置文件中定义的多个正则表达式。对于从配置中解析的每个所谓的
我有兴趣学习如何在多开发团队场景中设计/规划 Web 应用程序开发。 假设“项目经理/负责人”的角色: 成功的 Web 应用程序开发需要哪些“文档”? 需要什么 UML 图,需要什么程度? 在设计/计
table a (t_a): id name last first email state country 0 sklass klass steve
我们建立了一个广泛使用 JQuery UI 的 AJAX 网站。我们有 30 多个自制的 JQuery UI 小部件(动态加载)。我们到处都使用 JQuery native 小部件:对话框、 slid
我是一名优秀的程序员,十分优秀!