Category : permutation

Good day! I am having some trouble with my permutation calculator. For some reason, the end result is always 1. We are not allowed to use recursion at the moment so I opted to use a for loop for finding the factorials. Here is my code: #include <iostream> using namespace std; int fact(int x); int ..

let say I have a total number tN = 12 and a set of elements elem = [1,2,3,4] and a prob for each element to be taken pElem = [0.0, 0.5, 0.75, 0.25] i need to get a random multiset of these elements, such as the taken elements reflects the prob the sum of each ..

Given N – cars, N – cities and the distance between every car and city D. Every car needs to visit one city and every city can be visited by only one car. I need to find the smallest possible sum of the distances traveled by all cars. Is there a faster way than checking ..

I have written a letter case permutation, which perfectly for 8 letters, but gives me an error of time limit exceed in Leetcode for string size = 9. What can be optimized? Works for "L2tcnhpZ" but exceeds limit for "L2tcnhpZK" vector<string> result; vector<string> MyletterCasePermutation(string s) { genPerm(s, 0, s.size()-1); return result; } void genPerm(string s, ..

I’m currently trying to make a very fast anagram solver, and right now it’s bottlenecked by the creation of the permutations. is there another way to do the whole program or to optimize the permutation creation? here’s my code: #include <string> #include <vector> #include <algorithm> #include <iostream> #include <fstream> #include <unordered_set> #include <vector> #include <boost/asio/thread_pool.hpp> ..

I have been working on deadlocks and we are tasked to do a program that would check all possible sequences which are safe and which are not. I was able to display all the combinations however, I am not sure how can I check each sequence or probably how can I store these sequence, maybe? ..

Consider the code snippet: #include <vector> #include <algorithm> #include <stdio.h> int main(){ int szvec = 4; std::vector<int> vecint(szvec); for (size_t i = 0, szi = szvec; i < szi; i++){ vecint[i] = i; } do{ for (size_t i = 0, szi = vecint.size(); i < szi; i++){ printf("%dt", vecint[i]); } printf("n"); } while (std::next_permutation(vecint.begin(), vecint.end())); ..