Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have written this recursion for the problem https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
I want to know how can we memoize this solution (using a dp array).
or do we have to write recursion in a specific way to memoize it?
class Solution {
public:
int solve(vector<int>& prices,int fee,int i,int profit,int buy,int sell)
{
if(i==prices.size())
{
if(buy==sell)
return profit;
else
return 0;
}
int ans = 0;
ans = max(ans,solve(prices,fee,i+1,profit,buy,sell));
if(buy>sell)
{
ans = max(ans,solve(prices,fee,i+1,profit+prices[i]-fee,buy,sell+1));
}
else
{
ans = max(ans,solve(prices,fee,i+1,profit-prices[i],buy+1,sell));
}
return ans;
}
int maxProfit(vector<int>& prices, int fee) {
vector<int> diff;
int sum = 0;
sum = solve(prices,fee,0,0,0,0);
return sum;
}
};
You can just create an array where element i is equal to solve(i). Then, inside your function, you can pass this array through by reference to each call. You add an if/else structure in your function testing if the input you got was defined in the array, if so return arr[input] and if not, run through your normal function except just before you return, you initialize arr[input] to the value you will return.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Let's say I got this Array:
int myArray[] = {2,5,8,3,2,1,9};
is ther any way I could check if some of the contents can add up to 20? I managed to check if any two values add up to 20 but I just don't know how to handle it if is irrelevant how many values it needs.
Thank you for your help.
Most of the time you have a situation where you test if any combination of values satisfy a condition, you need to think of using recursion. You recurse through the elements and at each point branch off in two directions: one that considers that element, and one that doesn't. This can be short-circuited to stop looking if one of the branches does satisfy that condition.
Here's a potential solution to your problem
bool can_sum(const int* ptr, int size, int target, int total = 0)
{
// check success
if (total == target)
return true;
// check failure
if (total > target || size == 0)
return false;
return can_sum(ptr+1, size-1, target, total + *ptr) // check with *ptr
|| can_sum(ptr+1, size-1, target, total); // check without *ptr
}
int main()
{
int arr[] = {2,5,8,3,2,1,9};
bool result = can_sum(arr, 7, 20);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have problem to sort array with objects which are private and they are encapsulated(have getter and setters).I am using my own bubble sort function.
void BubbleSort(apvector <int> &num)
{
int i, j, flag = 1;
int temp;
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
flag = 1;
}
}
}
The problem is there that eclipse IDE sends me warning to use getters and setter in declaration of my class.
Why is better to use getters and setters?
P.S
Sorry for my bad asked question(this was one of my first questions) :)
void bubbleSort(Student* student, int size)
{ [...] }
Variable student is a pointer to an array.
You must also specific the size of the array.
To call it:
Student* myClass=new Student[5];
bubbleSort(myClass, 5); // Pass the array, and the size of the array both.
You need to create the said array before sending it as a parameter to the function. Alternatively you can create it inside the function but I think it's not what you want to go for here.
Student* students = new Student[5];
You should have written this somewhere before calling your function. Then, your function signature will have to turn to the following:
void bubbleSort(Student* student)
A logical thing to do would be to use a std::vector here though, it's much better than the method you're going for. See: http://en.cppreference.com/w/cpp/container/vector
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is the part that brakes, zod1 zod2 zodA are vectors. In vectors I store int. Cant tell why it crashes cause it gives no error
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
Questions:
1 Is if statement over complicated
2 Can I use vectors like that for solving math in if statement (sorry can't explain better)
3 If 2 question is true how should I solve it
Explanations:
variable names are in my own language (sorry if it looks weird)
Values are zero for all (ex zod1[u]=0)
added whole function (variables going to the function are passed correctly and I know I use some unnecessary thing)
void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{int i,keliamas=0;
int k =0;
vector<int> zod1(0);
vector<int> zod2(0);
vector<int> zodA(0);
for(i=0;i<zodis1.size();i++)
{
zod1.push_back(0);
}
for(i=0;i<zodis2.size();i++)
{
zod2.push_back(0);
}
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0);
}
int u=zodis1.size()-1;
int y=zodis2.size()-1;
int t=zodisAts.size()-1;
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
{//if((zod1[u]+zod2[y])/10==1)
{
keliamas=1;
}
if(u==0||y==0||t!=0)
{
if(keliamas==1)
{
}
}
u--;
y--;
t--;
}
else
{if(zod1[u]!=9)
zod1[u]=zod1[u]+1;
else
{ if(u!=zodis1.size()-1)
u++;
else
{
cout<<"something wrong man";
}
}
}
}
EDIT:
Error is here:
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0); // should be zodA
}
Is if statement over complicated
No, I think it is very simple with just one 'or'. However, if zodA[t] is always less than 10, then your if condition can be written as:
if ( (zod1[u]+zod2[y])%10==zodA[t] )
Can I use vectors like that for solving math in if statement (sorry
can't explain better)
Yes you can.
If 2 question is true how should I solve it
If it compiles but then crashes, then probably you are accessing out of bounds indices. Check that your indices are less than the vector sizes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}
void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
can someone look at this code and tell me if I am creating the pointer and object correctly please.
int main()
{
Square<int>* originalSquare = new Square<int>(3, 3);
for(int r = 0; r < originalSquare -> rowSize; r++)
{
for(int c = 0; c < originalSquare -> colSize; c++)
{
int num= 0;
originalSquare -> setElement(r, c, num);
}
}
return 0;
}
//quick_sort function
void quick_sort(Square<int>* square)
{
//nothing yet.
}
I keep getting a access violation error for somer reason... Program works fine before I changed this from stack to heap...
Any help will be greatful.
Thanks
This is not the code that shows your problem. Although, I would guess Square allocates a dynamically sized array, and setElement sets it? Can we see your constructor, and the code for setElement?