如何解决错误:第924行:字符9:运行时错误:引用绑定到类型为'int'stl_vector.h的空指针
我正在尝试解决leetcode问题maximum profit scheduling。我认为我的逻辑上没有错误,但我不明白为什么会收到上述错误,我花了很多时间试图理解该错误,我已经在本地运行了此错误,但遇到了段错误但是我无法发现我的代码中任何数据结构的界限。我在下面附上代码片段。请帮助我理解我的错误。
class Solution {
public:
typedef pair<pair<int,int>,int> ppi;
static bool comparator(ppi a,ppi b)
{
if(a.first.second<b.first.second)
return true;
if(a.first.second==b.first.second)
return a.second<b.second;
return false;
}
int jobScheduling(vector<int>& startTime,vector<int>& endTime,vector<int>& profit) {
if(startTime.size()==0) return 0;
int n=profit.size();
vector<ppi> in;
vector<int> dp(0,n);
int i=0,j=0;
for(i=0;i<n;i++)
{dp[i]=profit[i];
}
for(i=0;i<n;i++)
{in.push_back({{startTime[i],endTime[i]},profit[i]});
}
sort(in.begin(),in.end(),comparator);
for(i=1;i<n;i++)
{
for(j=i-1;j>=0;j--)
{
if(in[j].first.second<=in[i].first.first)
{ dp[i]=max(in[i].second+dp[j],dp[i-1]);
break;
}
}
dp[i]=max(dp[i],dp[i-1]);
}
return dp[n-1];
}
};
解决方法
这将通过map传递O(N Log N)运行时和O(N)空间,
// The following block might trivially improve the exec time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <vector>
#include <algorithm>
#include <map>
#include <iterator>
const static struct Solution {
using SizeType = std::int_fast32_t;
static int jobScheduling(
const std::vector<int>& start_times,const std::vector<int>& end_times,const std::vector<int>& profits
) {
const SizeType start_length = std::size(start_times);
std::vector<std::vector<SizeType>> jobs;
for (SizeType index = 0; index < start_length; index++) {
jobs.emplace_back(std::vector<SizeType> {
end_times[index],start_times[index],profits[index]
});
}
std::sort(std::begin(jobs),std::end(jobs));
map<SizeType,SizeType> job_profit_map = {{0,0}};
for (const auto& job : jobs) {
const SizeType curr = std::prev(job_profit_map.upper_bound(job[1]))->second + job[2];
if (curr > std::rbegin(job_profit_map)->second) {
job_profit_map[job[0]] = curr;
}
}
return std::rbegin(job_profit_map)->second;
}
};
参考文献
- 有关其他详细信息,请参见Sagemaker TensorFlow estimators documentation,在这里您可以找到许多具有各种Discussion Board且已被广泛接受的解决方案,包括低复杂度算法和渐近languages / {{ 3}}分析runtime,memory 。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。