Reputation: 23
Okay I am Solving https://leetcode.com/problems/longest-increasing-subsequence/ My code is Correct and runs fine without memonization(TLE), but after memonized it gives error :
LLVM ERROR: out of memory
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
My code:
class Solution {
public:
int dp[2555][25000005];
int fun(vector<int>& a, vector<int>& v, int i, int count) {
int n = a.size();
if(i == n) return count;
if(dp[i][count] != -1) return dp[i][count];
if(v.back() < a[i]) {
v.push_back(a[i]);
int val1 = fun(a, v, i+1, count+1);
v.pop_back();
int val2 = fun(a,v,i+1,count);
return max(val1, val2);
}
else
return fun(a,v,i+1,count);
}
int lengthOfLIS(vector<int>& a) {
vector<int> v;
v.push_back(INT_MIN);
memset(dp, -1, sizeof(dp));
return fun(a, v, 0, 0);
}
};
Upvotes: 1
Views: 17775
Reputation: 2626
As for me - in the logs this @objc method UI_USER_INTERFACE_IDIOM was highlighted. So I've replaced it to Swift approach - and generation started working again.
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {}
if UIDevice.current.userInterfaceIdiom == .pad {}
Upvotes: 1
Reputation: 910
Your int dp[2555][25000005];
array is probably way too large for your stack as it is approximately 2555*25000005*4=
255 GB (if an int is 4 bytes). If you have enough memory (physical and pageable) you can simply allocate all that memory on the heap by using malloc()/free()
or an STL container like std::vector
. Otherwise, you should probably try to make your program allocate less memory by changing your algorithm.
For further information on the stack/heap and memory and general you can look that this previous question What and where are the stack and heap?.
Upvotes: 3