R:打印所有最长的递增子序列(LIS)

时间:2019-01-25 18:36:50

标签: r dynamic lis

给出向量

        int k = 5; // only k cheapest from array n
        int m = 2; //max same iDs
        Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
        stream(arr).forEach(product -> {
            if (!map.containsKey(product.ID)) {
                PriorityQueue<Integer> integers = new PriorityQueue<>(reverseOrder());
                integers.add(product.price);
                map.put(product.ID, integers);
            } else {
                PriorityQueue<Integer> integers = map.get(product.ID);
                integers.add(product.price);
                map.put(product.ID, integers);
                if (integers.size() > m) {
                    integers.poll();
                }
            }
        });
        PriorityQueue<Integer> priorityQueueK = new PriorityQueue<>(k, reverseOrder());
        for (PriorityQueue<Integer> values : map.values()) {
            for (int i = 0; i < values.size(); ) {
                priorityQueueK.add(values.poll());
                if (priorityQueueK.size() > k) {
                    priorityQueueK.poll();
                }
            }
        }

找到所有最长的增长子序列(LIS)

解决方案应类似于

x <- c(4,5,6,7,8,9,10,11,12,13,15,14,13,12,11,10,9,8,6)

我什至可以处理元素的索引

我使用了JINJING XIE给定的代码,但它只返回一个序列

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

这是一个(缓慢但效率不高)的函数,将对其进行计算(使用RcppAlgos

max_sub_sequences <- function (x) {  
  # x incresing, result is obviously x
  if (all(diff(x) > 0)) return (x)
  N <- length(x)
  n <- N - 1L 
  break_condition <- TRUE
  while (break_condition) {
    # generate all combinations of indices
    combinations <- RcppAlgos::comboGeneral(1:N,n)    
    # get subsequences according to indices
    sub_sequences <- matrix(x[combinations[1:nrow(combinations),]], nrow = nrow(combinations)) ; rm(combinations)
    # check monotony 
    index_subsequence <- vapply(1:nrow(sub_sequences), function (k) any(diff(sub_sequences[k,]) < 1L), logical(1L))
    # keep increasing sequences only
    sub_sequences <- sub_sequences[!index_subsequence,] ; rm(index_subsequence)
    break_condition <- nrow(sub_sequences) == 0L
    n <- n - 1L
  }
  sub_sequences
 }
max_sub_sequences(c(4,5,6,7,8,9,10,11,12,13,15,14,13,12,11,10,9,8,6))    
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
# [1,]    4    5    6    7    8    9   10   11   12    13    15
# [2,]    4    5    6    7    8    9   10   11   12    13    14  

# timing
# Unit: seconds
#                 expr     min       lq     mean   median       uq      max neval
# max_sub_sequences(x) 1.30611 1.462473 1.502727 1.484785 1.522796 1.821037   100

肯定有提高效率的方法,例如其他逻辑,或者通过在c++中编写,然后在整个任务中使用Rcpp来实现。