小编Gsq*_*are的帖子

创建最低成本数组

我有一个大小为n + 1的数组,我希望存储购买n物品的最低成本(i^th索引商店的i^th物品成本).

m不同的卖家:每个卖家提供项目L到项目R(其中L,R> = 1和L,R<= n),每个卖家的成本C.

要创建成本最低的数组,我执行了以下操作:

for (int i=1; i<=m; i++) {
    int L = L of i^th seller
    int R = R of i^th seller
    int C = selling price of i^th seller
    for (int j=L; j<=R; j++) {
        if (leastCost[j]==0 || leastCost[j]>C) {
            leastCost[j]=C
    }
}
Run Code Online (Sandbox Code Playgroud)

构造这个数组是O(n*m)访问这个数组O(1).

对于非常大的n和 …

arrays algorithm data-structures

5
推荐指数
1
解决办法
180
查看次数

将dataframe列转换为元组列表

我有:

val DF1 = sparkSession.sql("select col1,col2,col3 from table");
val tupleList = DF1.select("col1","col2").rdd.map(r => (r(0),r(1))).collect()

tupleList.foreach(x=> x.productIterator.foreach(println))
Run Code Online (Sandbox Code Playgroud)

但是我没有得到输出中的所有元组.问题在哪里?

col1 col2
AA  CCC
AA  BBB 
DD  CCC 
AB  BBB 
Others  BBB 
GG  ALL 
EE  ALL 
Others  ALL 
ALL BBB 
NU FFF 
NU  Others 
Others  Others 
C   FFF 
Run Code Online (Sandbox Code Playgroud)

我得到的输出是: CCC AA BBB AA Others AA Others DD ALL Others ALL GG ALL ALL

scala tuples dataframe apache-spark

3
推荐指数
1
解决办法
5248
查看次数