algorithm

unique permutations of variable length [duplicate]

有些话、适合烂在心里 提交于 2021-01-29 15:44:39
问题 This question already has answers here : Finding the subsets of an array in PHP (4 answers) Closed 6 years ago . I need to get an array of unique permutations of any length: array of values: a, b, c, d, e, f, g, h, i, j, k, l, m (13 total) expected result: a, b, c, ab, ac, bc, abc, .... ab == ba (duplicate) abc == acb == bca == bac == ... (duplicates) What I have so far is kind of a brute force attack at it, but with 13 elements this is kind of optimistic. I need something smarter,

Create a schedule where a group of people all talk to each other - with restrictions

青春壹個敷衍的年華 提交于 2021-01-29 15:31:22
问题 Problem statement I would like to achieve the following: (could be used for example to organize some sort of a speeddating event for students) Create a schedule so people talk to each other one-on-one and this to each member of the group. but with restrictions. Input : list of people. (eg. 30 people) Restrictions : some of the people should not talk to each other (eg. they know each other) Output : List of pairs (separated into sessions) just one solution is ok, no need to know all of the

Quick Sort Time Complexity Best Case Input

一世执手 提交于 2021-01-29 15:31:21
问题 I have to find time complexity of quick sort for BEST CASE INPUT in a c program & i have selected the last element of array as pivot. Now i know what input values i have to enter for best case, i.e., keep 1st middle element at the last place(pivot) & next pivot should be the next middle element. But i have to generate this kind of best case input array of very big sizes like 1000, 5000, 100000.., for quick sort. I can code, but can anyone please help me understand how to generate that kind of

Perfect sum problem with fixed subset size

☆樱花仙子☆ 提交于 2021-01-29 15:09:51
问题 I am looking for a least time-complex algorithm that would solve a variant of the perfect sum problem (initially: finding all variable size subset combinations from an array [*] of integers of size n that sum to a specific number x ) where the subset combination size is of a fixed size k and return the possible combinations without direct and also indirect (when there's a combination containing the exact same elements from another in another order) duplicates. I'm aware this problem is NP

How to improve integer partitioning with n,m,k

我们两清 提交于 2021-01-29 14:52:41
问题 my program counts the integer partitions of n , which have k distinct partition elements, each is smaller or equal to m . How can i improve the performance? I already cache immediate results. public long q(int n, int m, int k){ return q1(n,m,k,0,0,new HashMap()); } private long q1(int n, int m, int k, int level, int last, Map<String, Long> cache){ if(n <0){ return 0; } if(level+1 ==k){ if(n >m){ return 0; } else { return 1; } } int first = (level == 0) ? 1 : last+1; long total = 0; for(int i

Find the number of connected components in undirected graph using Union Find

落爺英雄遲暮 提交于 2021-01-29 14:39:49
问题 I've been staring at this algorithm for a while but couldn't spot what I've missed. Logic: Init all isolated components union frm, to component in each edge and decrement # components if those components aren't already connected. Question: how come that I get this -48 when running this particular test case? def countComponents(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: int """ roots = [i for i in range(n)] # track disconnected components # n isolated islands for u,

Implementation of string::find_first_of

天涯浪子 提交于 2021-01-29 14:14:54
问题 My confusion comes from a solution for problem 792. Number of Matching Subsequences in leetcode, the naive solution is to check all characters in S for each searching word. The time complexity is too high to pass all test cases as outlined in the official answer. The first code piece listed below could beats 100% merely because it calls string::find_first_of function, while my hand written codes uses exactly the same routine(I believe) get TLE as expected. So, why the first one is so fast? //

How to generate all permutations of lenght n from a set of k elements

我的未来我决定 提交于 2021-01-29 13:44:10
问题 For example I have this set k=5 of elements [1,2,3,4,5] and I want all permutations of length n=2 . 1,2 1,3 1,4 1,5 2,1 etc etc. Thing is i can't use STL, external math libraries etc. I've been sitting on this problem for about 3 days now and im about to go crazy. What i tried is generating all permutations of all the elements using Heap's algorithm, and then all the permutations of n elements where contained in the first n numbers of all k-permutations and i could just truncate and delete

Looking for a specific combination algorithm to solve a problem

余生颓废 提交于 2021-01-29 13:13:07
问题 Let’s say I have a purchase total and I have a csv file full of purchases where some of them make up that total and some don’t. Is there a way to search the csv to find the combination or combinations of purchases that make up that total ? Let’s say the purchase total is 155$ and my csv file has the purchases [5.00$,40.00$,7.25$,$100.00,$10.00]. Is there an algorithm that will tell me the combinations of the purchases that make of the total ? Edit: I am still having trouble with the solution

Distance of nearest cell having 1

别说谁变了你拦得住时间么 提交于 2021-01-29 09:50:24
问题 Given a binary matrix of size N x M. The task is to find the distance of nearest 1 in the matrix for each cell. The distance is calculated as |i1 – i2| + |j1 – j2|, where i1, j1 are the row number and column number of the current cell and i2, j2 are the row number and column number of the nearest cell having value 1. Input: The first line of input is an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of 2 lines . The first line of each test case