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 3 months ago.
Improve this question
Please see the code at the link below:
https://stackoverflow.com/a/20614037/1676147
My thoughts:
Time complexity here would depend on two factors:
1) Number of recursive calls: O(n) where n is the number of characters in the original input string
2) The work involved in the two for loops: O(n!)?
Basically, the first for loop is iterating each string in the set 'permSet'
The number of permutations for an n-character string can be n!.
Thus, this set would contain n! strings. Correct?
The second for loop places one character (variable 'a' in the code) at each
of the potential positions in each of these strings.
I am confused at this step.
Your analysis is correct. For a string of length N, you have N-1 recursions, with string length of M, one each for lengths 1 through N-1. Each recursion deals with a list of size (M-1)!, and inserts the next character at each of M positions (0 through M-1).
Each call has time M * (M-1)!, or M! Sum these over M = 1 to N.
1! + 2! + 3! + ... + N!
This sum is O(n!).
Related
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 1 year ago.
Improve this question
Given an array of integers for example, [1 4 3 2 9 8 7 6]
Each integer represents an amount of money you can pick up.
You are traveling through the array, and you can pick up the money.
Every time you pick up some money, you must travel another X amount (minInterval) in order to pick up more money. - that is you must wait X travels in order to pick up any more money. What is the maximum amount of money you can pick up given the minInterval.
note: each travel is essentially just moving to the next index.
you want to maximize the value of the money you pick up.
for example, if the minimum interval is 4 an optimal path would be,
What is a good algorithm to accomplish this?
It's basically an optimization problem.
you can use the dynamic programming to solve the problem.
traverse the array from the last element to first,then you will have two choices for each element:
1.choose the current element ,transit from the i+x th element dp[i]=dp[i+x]+a[i]
2.don't choose the current element,transit from dp[i+1],which means the max value you can get from interval [i+1,n]
then you can get:dp[i]=max(dp[i+1],dp[i+x]+a[i])
so:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,x;
cin>>n>>x;
vector<int> a(n+1);
vector<int> dp(n+1+x);
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=n;i>=1;--i){
dp[i]=max(dp[i+1],dp[i+x]+a[i]);
}
cout<<dp[1]<<endl;
return 0;
}
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
An integer is a lucky number if each number in it is 3 or 7. To give an example, the numbers 7, 73, and 33777 are lucky numbers. Your task is to calculate the number of lucky numbers between int(a) and int(b).
How could one achieve this efficiently (in less than 1 sec) without going through all the numbers? I have attempted many solutions in Java however none of them are fast enough.
Since you have to count the numbers and not list the numbers, you can use permutation and combination to find the answer.
Eg let say find between 1 and 999 where you can use 3, 7
Then you have 3 lengths single, double and triple digits with constraints on single and triple digits.
For single since minimum number is 1 and 3, 7 both are greater there 2 numbers.
For double digits you have no constraints hence you have 2 * 2 = 4 combinations
Similarly for 3 digits as max number allowed is 9 in each place and 3,7 are lesser than them there will be 2 * 2 * 2 = 8
So answer is 14 after summing them all... This algorithm will run fast as it depends on the size of the numbers to generate ie o(n) time complexity where n is max length of number.
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
Regarding the classic problem of putting N identical balls into M distinct bins and printing all the combinations: What if you would want to extend the problem by printing all cases 0< M, N
The brute force method could be done something like this:
for (int i =0; i<M; i++)
{
for (int j =0; j <N; j++)
{
PrintAllCombinations(j,i)
}
}
Now if we study the output of the first couple m and n, we see that the output of each previous iteration is a subset of the next. It seems to me that we can apply a dynamic algorithm to exploit this phenomenon. However, because we still need to partition every n, for example n=3 = 3 +0, 2+1, 1+2. we still need to do alot of redundant combination calculations. Any ideas fir improvments?
Let S[i][j] be the number of combinations for i balls in j bins.
S[0][j] = 1 for all j since the only combination is to have all bins empty.
S[i][1] = 1 for all i since the only combination is to put all the balls in the one bin.
For every other i, j S[i][j] = sum(x = 0 -> i, S[i-x][j-1]). That is for every other position you can compute the number of combinations by assigning every possible number of balls to the last bin and sum the number of combinations you get.
If you want to print out the combinations you can replace the count with the actual combinations and append the value x when you take the internal combinations in the sum. That will take a lot of memory without a lot of gain in speed. Just do the recursion and repeat the computation since you're bound by the number of solutions anyway.
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 8 years ago.
Improve this question
suppose adding two numbers with a and b bits can be done in O(max{a,b}). we want to add n numbers (n 1-bit numbers i.e add n's 0 or 1). the cost of this algoithm is vary from input permutation to another permutation. what is the best and worst case of this algorithm?
i ran into this problem as an old- quiz on Computer Course.
We have two Solution:
1- Best Case and Worst Case can be in O(n)
2- Best in O(n) and Wost Case in O(n lg n)
Anyone could describe any pesudocode or algorithm for above two time order?
Answer is 1- Best and worst case it is O(n).
Best case, when all bits are 0, the order required is n - 1 or simply put O(n).
Worst case, when all bits are 1, the actual order is sum of n * (lg n - lg i) / 2 ^ (lg n - lg i)
i from n to 0. As i tends to 0, the resulting expression value will be small and so can be ignored. So the expression is like n/2 + n/4 * 2 + .... Again a linear growth with n.
Adding more details:
Best case, when all bits are 0, adding them will never result in a no having more than 1 digit. (ie it always gives 0). therefore the order is O(n)
Worst case, when all bits are 1, adding first 2 bit will require O(1) The result will be 10. Now adding it with another bit will require O(2) and this way order can increase in case of worst case. Assume we split the input set into 2 number pairs and add them, in the first iteration all pairs would require O(1) and will result in 10. There are n/2 pairs so it is O(n/2). In the second iteration, we split the first iteration result set into 2 number pairs and add them. (Note now all nos are 10) So the order would be n/4 * O(2). i.e, O(n/2). In this way it is to be proceeded till the result set is exhausted. But when the value of n is very large, the contribution from 3 iteration onward can be ignored. So it just O(n/2) + O(n/2) , i.e O(n)
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 8 years ago.
Improve this question
I am trying to model electricity costs. Usually, for <1000 kWh usage, there is a certain per-kwh cost, which increases if you use >1000kWh a month. I need a single equation that gives the cost per kWh, given a particular model for electricity consumption.
Given number x and a constant N, find a and b where:
a = the portion of the number below N
b = portion of number above N
"if" conditions cannot be used; a single formula is needed.
For example:
For N=1000 and x=500, we have a=500, b=0.
For N=1000 and x=1500, we have a=1000, b=500.
Edit:
To make it more clear:
for x < N, a = x mod N, b = 0;
for x > N, a = N, b = x mod N.
Edit2:
abs() operator is acceptable, since it can be implemented using bit operators. abs(x)=((x >>> 30) | 1)) * x.
How can the two cases be combined into a single equation?
I think I got one after support from defintion of max version provided by you---
max(a,b) = 1/2 (a+b+abs(a-b))
and a minimum defintion from myside
min(a,b) = 1/2 (a+b-abs(a-b)) .
Please improve me wherever I am wrong by criticising :-
b={x-N+abs(x-N)}/2;
a={x+N-abs(x-N)}/2; Or simply, x-b;
In Python
(a,b) = (min(x,N), max(x-N,0))