【问题标题】:In R: Want to find all "subvectors" with a specific length (9) from a large vector (length 300) , such that the elements sum to a given value在 R 中:想要从一个大向量 (长度 300) 中找到所有具有特定长度 (9) 的“子向量”,使得元素总和为给定值
【发布时间】:2015-01-23 22:21:03
【问题描述】:

我想找到一种方法来列出下面薪水向量中的所有子向量,要求它们的总和在 54,000 到 55,000 之间。我已经尝试在 C 中使用 this 方法,但是它花费的时间太长了......我认为是因为它在应用限制之前找到了向量(54,000

我问过一位朋友,他建议开发一种方法来应用总和限制;因为否则,程序必须找到 (300 C 9) ~ 4.8 x 10^16 组合。

谢谢!

这是薪水清单:

Salary
10800
9800
9600
9600
9500
9100
8800
8800
8700
8600
8600
8600
8600
8300
8200
8200
8100
8100
8100
8100
8000
7900
7600
7500
7500
7500
7400
7300
7300
7300
7200
7100
7100
7100
7000
6900
6900
6900
6800
6800
6800
6800
6700
6700
6600
6600
6600
6500
6500
6500
6500
6500
6400
6300
6300
6300
6200
6200
6100
6000
6000
6000
6000
5900
5900
5900
5800
5800
5800
5800
5800
5700
5700
5700
5600
5600
5600
5600
5600
5500
5500
5500
5400
5400
5400
5400
5400
5300
5200
5200
5200
5200
5200
5200
5200
5200
5200
5200
5100
5100
5100
5100
5000
5000
5000
5000
5000
5000
5000
5000
5000
5000
5000
5000
4900
4900
4800
4800
4800
4800
4800
4700
4700
4700
4700
4700
4700
4600
4600
4600
4500
4500
4500
4500
4500
4400
4400
4400
4400
4300
4300
4300
4300
4300
4300
4200
4200
4200
4200
4200
4200
4200
4100
4100
4100
4100
4100
4100
4000
4000
4000
4000
4000
4000
4000
4000
3900
3900
3900
3900
3900
3900
3900
3800
3800
3800
3800
3800
3800
3800
3800
3700
3700
3700
3700
3700
3700
3600
3600
3600
3600
3600
3600
3600
3600
3600
3600
3600
3600
3500
3500
3500
3500
3500
3500
3500
3500
3500
3500
3500
3500
3500
3500
3400
3400
3400
3400
3400
3400
3400
3400
3400
3400
3400
3400
3400
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3300
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3200
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3100
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000

【问题讨论】:

  • 您所说的“随时应用总和限制”是什么意思?您能否澄清“子向量”的含义(即子向量的元素必须是原始向量的连续子集,仅仅是元素的任意组合等)?

标签: r sum subset


【解决方案1】:

有点迟来的回应,但它可能对某人仍然有用。

这个想法是生成所有可能的子向量。由于向量的长度为 320,因此有 1 个长度为 320 的子向量,2 个长度为 319 的子向量,3 个长度为 318 的子向量,...,以及 320 个长度为 1 的子向量。(注意这些子向量不一定是唯一的。)因此,可能的子向量数量“仅”为 321*320/2 = 51360。

鉴于上述情况,并假设x <- c(10800, 9800, 9600, 9600, ..., 3000),解决方案应该是不言自明的:

# All possible start and end positions of subvectors
subvectors <- expand.grid(start=1:length(x), end=1:length(x))
# Of course, start must come before end
subvectors <- subvectors[subvectors$start <= subvectors$end, ]
# All possible subvectors
subvectors$values <- apply(subvectors, MARGIN=1, function(r) x[r[1]:r[2]])
# Subvector lengths (for convenience)
subvectors$length <- sapply(subvectors$values, length)
# Subvector sums
subvectors$sum <- sapply(subvectors$values, sum)
# All subvectors that satisfy the constraint
solutions <- subvectors[subvectors$sum >= 53000 & subvectors$sum <= 54000, ]

有 65 种解决方案,长度从 6 到 17 不等。

与上面提到的 C 解决方案不同,这更像是一种蛮力方法 (O(n3)?),但它足够简单,仍然只需 1 秒即可运行。

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多