You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala Spark中两个列表/数组的比较实现方案问询

问题描述

我有两个列表:

# 耗时
x1 = List(10, 20, 30, 40, 50)

# 分配时长
y1 = List(15, 30)

以下是更多示例:

+-------------------+----------------------+----------+
|       time_taken  |       time_alloted   | result   |
+-------------------+----------------------+----------+
|         160-80-40 |            [20,40,80]|        80|
|            520-90 |           [45,90,130]|       130|
|    60-120-240-480 |              [30,120]|       120|
| 10-10-10-20-20-30 |               [12,30]|        12|
| 10-20-30-40-50-60 |               [12,30]|        30|
| 45-40-40-40-40-40 |           [45,90,130]|        45|
+-------------------+----------------------+----------+

我需要按照以下规则比较time_taken列和time_alloted列中的秒数:

  • 若time_taken中的元素能被time_alloted中的元素(从大到小遍历)整除,则取最大的那个时长,例如结果为80(3个80单位和1个40单位,选取80)
  • 若time_taken中的元素大于time_alloted中的最大元素,则将time_alloted中的元素乘以2、3、4……直到能被整除(如130*5=520)
  • 若元素无法整除,则检查哪个元素的余数更小

本质上,我希望比较ListA(含1到n个元素)和ListB(含1到m个元素)中的每个元素,得到合适的结果。

补充说明

我目前使用SQL进行计算,现寻求基于数据结构或高阶函数的解决方案。
我已从两个列表中计算出大小、最大值、最小值,并将每个元素存储在单独的列中:

  • time_taken列:tim_size, tim_max, tim_min, dt1, dt2, dt3
  • time_alloted列:alt_size, alt_max_sec, alt_min_sec, t1, t2, t3

当前SQL逻辑如下:

when tim_size ==1 and alt_size == 1 then alt_max_sec 
when mod(tim_min, alt_max_sec) == 0 then alt_max_sec 
when tim_size ==1 and alt_size == 2 and MOD(tim_max, alt_max_sec) == 0 then alt_max_sec
when tim_size ==1 and alt_size == 2 and MOD(tim_max, alt_min_sec) == 0 then alt_min_sec
when tim_size ==1 and alt_size == 3 and MOD (tim_max, alt_max_sec) == 0 then alt_max_sec
when tim_size ==1 and alt_size == 3 and MOD (tim_max, t2) == 0 then t2
when tim_size ==1 and alt_size == 3 and MOD (tim_max, alt_min_sec) == 0 then alt_min_sec
when tim_size ==2 and alt_size == 1 then alt_max_sec
when tim_size ==2 and alt_size == 2 and MOD(tim_max, alt_max_sec) == 0 and MOD(tim_min, alt_max_sec) == 0 then alt_max_sec
when tim_size ==2 and alt_size == 2 and MOD(tim_max, alt_min_sec) == 0 and MOD(tim_min, alt_min_sec) == 0 then alt_min_sec
when tim_size ==2 and alt_size == 2 and MOD(tim_min, alt_min_sec) == 0 then alt_min_sec
when tim_size ==2 and alt_size == 3 and MOD(tim_max, alt_max_sec) == 0 and MOD(tim_min, alt_max_sec) == 0 then alt_max_sec
when tim_size ==2 and alt_size == 3 and MOD(tim_max, t2) == 0 and MOD(tim_min, t2) == 0 then t2
when tim_size ==2 and alt_size == 3 and MOD(tim_max, alt_max_sec) == 0 and MOD(tim_min, t2) == 0 and ((tim_max/alt_max_sec) > (tim_min/t2)) THEN alt_max_sec
WHEN tim_size ==2 and alt_size == 3 and (tim_max/t2) == 1 and (tim_min/t1) == 1 then alt_min_sec
WHEN tim_size ==3 and alt_size == 3 and mod(tim_max, alt_max_sec) == 0 and mod(dt2, alt_max_sec) == 0 then alt_max_sec
WHEN tim_size ==3 and alt_size == 3 and mod(tim_max, alt_max_sec) == 0 and mod(dt2, alt_max_sec) == 0 then alt_max_sec
WHEN tim_size ==3 and alt_size == 2 and MOD(tim_max, alt_max_sec) == 0 and mod(dt2, alt_max_sec) == 0 then alt_max_sec
when alt_size == 3 and mod(tim_min, t2) == 0 then t2
else 'TBC' end as result

内容的提问来源于stack exchange,提问作者nagraj036

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 20:24:59