algorithm,dynamic-programming,combinatorics,subsequence

If the sum of all your elements is not too big (within millions), then you can do a solution similar to a knapsack problem. Your DP state is a difference between two sets, and for every element you iterate over all the differences you know so far, and update them....

This looks like you're putting consecutive zeros into groups. I would store your results in a Dictionary<string, List<int>> and you'll have to know when you find a zero you create a new group and every consecutive zero will belong to the same group until the sequence is broken. Then a...

You should be able to simply use two nested for loops as follows: // Setup char[] arr = { 'A', 'E', 'D' }; // Generate all subsequences List<char[]> result = new ArrayList<>(); for (int start = 0; start < arr.length; start++) { for (int end = start + 1; end...

I haven't worked with polymorphic recursion much, so I wanted to try this. Here's what I got: {-# LANGUAGE DeriveFunctor #-} import Data.List (subsequences) -- Any multiply-nested list such as a, [a], [[a]], [[[a]]], ... data MultiList a = Leaf a | Nest (MultiList [a]) deriving (Show, Functor) recursive ::...

Perhaps you could do a global alignment with Needleman-Wunsch and a high mismatch penalty, to prefer indels. At the end, merge the alignment into a "parent string" by taking letters from matching positions, and then a letter from either of the inserted letters, e.g.: WW-A || WWS- WWSA Or: -12345...

What you are currently doing is O(n^2) because you loop through the small string and the search for that character in the longer string is a linear search because it is not sorted (all letters in alphabetical order). Below is a O(n) solution. The concept is to have a size...

You are right, there is a linear time algorithm for this. You can compute an array with the sliding maximum and an array with the sliding minimum and find the largest difference between these two arrays. Computing a sliding maximum in linear time is a standard problem, there is a...

One possibility would be to find the cumulative maxima of the vector, and then extract unique elements: unique(cummax(a)) # [1] 2 5 6 8 ...