Scala 分布在各个房间的用户索引
我是stackoverflow的新手,因此如果我不恰当地解释我的问题,请指导我 我有一个长度为N的整数集合(一个Scala 分布在各个房间的用户索引,scala,optimization,sequence,Scala,Optimization,Sequence,我是stackoverflow的新手,因此如果我不恰当地解释我的问题,请指导我 我有一个长度为N的整数集合(一个数组[Int]),其中某个索引I中的每个元素表示房间Ri中的用户数。在房间Ri中,用户也被索引,使得第一个房间中的用户索引从1到R1,第二个房间包含R1+1到R1+R2,依此类推 现在输入的是用户的索引,我需要找出用户所在的房间 我的解决方案如下: def getRoomNumber(userIndex: Int, userDistribution: Array[Int]): Int
数组[Int]
),其中某个索引I
中的每个元素表示房间Ri
中的用户数。在房间Ri
中,用户也被索引,使得第一个房间中的用户索引从1
到R1
,第二个房间包含R1+1到R1+R2
,依此类推
现在输入的是用户的索引,我需要找出用户所在的房间
我的解决方案如下:
def getRoomNumber(userIndex: Int, userDistribution: Array[Int]): Int = {
val cummulativeRooms: Array[Int] =
rooms.tail.scanLeft(rooms.head)((x, y) => x + y)
val roomIndex = cummulativeRooms.takeWhile(_ < userIndex).length + 1
roomIndex
}
def getRoomNumber(userIndex:Int,userDistribution:Array[Int]):Int={
val计算对象:数组[Int]=
房间.尾部.扫描左(房间.头部)((x,y)=>x+y)
val roomIndex=cumulativeRooms.takeWhile(
这里,由于用户4将出现在房间2中(因为房间具有如下用户分布:Room1(U1,U2)
,Room2(U3,U4,U5)
)
我的代码可以很好地用于此输入。但是我有一些隐藏的测试用例,其中有一半通过了。但后半段没有,有些甚至抛出异常
谁能告诉我我的代码有什么问题吗。还有没有比这更有效的算法呢
更多解释-
假设我有10个用户——U1、U2、U3、U4、U5,我们按照userDistribution
array-array(2,3)定义的顺序将它们分成N个房间。这意味着U1和U2用户将出现在房间1中,U3到U5用户将出现在房间2中
现在,如果我想知道用户U4在哪里,那么输出应该是2。输入是用户索引,即本例中的4和userDistribution
array-array(2,3)
编辑:代码更改为函数。输入是我们要查找的用户索引和userDistributions
,其中依次包含每个房间中的用户数
编辑:约束是(我们不必在代码中检查这些约束)-
N和Ui的值都可以在1到10e5之间。
此外,Ui将小于数组的总和。若我正确理解了这个问题,您只需迭代用户的分布数组,并保留您看到的用户数的总和,直到该总和大于或等于目标用户 使用命令式解决方案,您可以非常轻松地做到这一点:
// Return Option because we can not guarantee the constraints.
// Also, ArraySeq is just an immutable array.
def imperativeSolution(userDistribution: ArraySeq[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] = {
var acc = 0
for (i <- userDistribution.indices) {
acc += userDistribution(i)
if (targetUser <= acc) return Some(i + 1)
}
None
}
def functionalSolution(userDistribution: IterableOnce[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] =
userDistribution
.iterator
.scanLeft(0)(_ + _)
.zipWithIndex
.collectFirst {
case (acc, idx) if (targetUser <= acc) => idx
}
这是不可变的,但它有很多样板,我们可以使用功能更强大的解决方案来简化这些样板,使其更具表现力:
// Return Option because we can not guarantee the constraints.
// Also, ArraySeq is just an immutable array.
def imperativeSolution(userDistribution: ArraySeq[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] = {
var acc = 0
for (i <- userDistribution.indices) {
acc += userDistribution(i)
if (targetUser <= acc) return Some(i + 1)
}
None
}
def functionalSolution(userDistribution: IterableOnce[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] =
userDistribution
.iterator
.scanLeft(0)(_ + _)
.zipWithIndex
.collectFirst {
case (acc, idx) if (targetUser <= acc) => idx
}
def functional解决方案(用户分布:IterableOnce[PositiveInt])(目标用户:PositiveInt):选项[PositiveInt]=
用户分布
.迭代器
.scanLeft(0)(+
zipWithIndex先生
.首先{
案例(acc,idx)如果(目标用户idx
}
您可以看到它们正在运行。如果我正确理解了这个问题,您只需迭代用户的分发数组,并保留所看到的用户数的总和,直到该总和大于或等于目标用户 使用命令式解决方案,您可以非常轻松地做到这一点:
// Return Option because we can not guarantee the constraints.
// Also, ArraySeq is just an immutable array.
def imperativeSolution(userDistribution: ArraySeq[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] = {
var acc = 0
for (i <- userDistribution.indices) {
acc += userDistribution(i)
if (targetUser <= acc) return Some(i + 1)
}
None
}
def functionalSolution(userDistribution: IterableOnce[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] =
userDistribution
.iterator
.scanLeft(0)(_ + _)
.zipWithIndex
.collectFirst {
case (acc, idx) if (targetUser <= acc) => idx
}
这是不可变的,但它有很多样板,我们可以使用功能更强大的解决方案来简化这些样板,使其更具表现力:
// Return Option because we can not guarantee the constraints.
// Also, ArraySeq is just an immutable array.
def imperativeSolution(userDistribution: ArraySeq[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] = {
var acc = 0
for (i <- userDistribution.indices) {
acc += userDistribution(i)
if (targetUser <= acc) return Some(i + 1)
}
None
}
def functionalSolution(userDistribution: IterableOnce[PositiveInt])(targetUser: PositiveInt): Option[PositiveInt] =
userDistribution
.iterator
.scanLeft(0)(_ + _)
.zipWithIndex
.collectFirst {
case (acc, idx) if (targetUser <= acc) => idx
}
def functional解决方案(用户分布:IterableOnce[PositiveInt])(目标用户:PositiveInt):选项[PositiveInt]=
用户分布
.迭代器
.scanLeft(0)(+
zipWithIndex先生
.首先{
案例(acc,idx)如果(目标用户idx
}
您可以看到它们正在运行。欢迎使用StackOverflow,Harry。您希望生成的输出有点不清楚。也许您可以定义一个函数来澄清您拥有的输入和您期望的输出。运行时期望的类型和一点解释(可能有几个测试用例)可以帮助您轻松获得相关答案。干杯!@stefanobaghino感谢您的建议。我现在已将我的代码更改为函数。我希望它现在会更清晰。此外,测试用例和异常都是隐藏的,这就是为什么我无法自己解决它的原因。我不清楚我的代码有什么问题。@stefanobaghino如果您愿意的话对于这个问题有不同的解决方法,我也希望看到。getRoomNumber(10000,数组(1))的预期输出是什么??了解其他边缘情况也会很有趣(用户索引不足/溢出、空数组、处理房间之间的“边缘”时的行为等)欢迎使用StackOverflow,Harry。您希望生成的输出有点不清楚。也许您可以定义一个函数来澄清您拥有的输入和您期望的输出。运行时期望的类型和一点解释(可能有几个测试用例)可以帮助您轻松获得相关答案。干杯!@stefanobaghino感谢您的建议。我现在已将我的代码更改为函数。我希望它现在会更清晰。此外,测试用例和异常都是隐藏的,这就是为什么我无法自己解决它的原因。我不清楚我的代码有什么问题。@stefanobaghino如果您愿意的话对于这个问题有不同的解决方法,我也希望看到。getRoomNumber(10000,数组(1))的预期输出是什么??了解其他边缘情况也会很有趣(用户索引不足/溢出、空数组、处理房间之间的“边缘”时的行为等)