首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据开发商给出的每个项目的投标金额计算项目总最小成本:面试问题

根据开发商给出的每个项目的投标金额计算项目总最小成本:面试问题
EN

Stack Overflow用户
提问于 2022-06-24 10:05:57
回答 2查看 74关注 0票数 1

根据开发商给出的每个项目的投标金额计算项目总最小成本,如果没有对任何项目进行投标,则返回-1,这意味着,每个项目必须至少有一个投标或-1的回报。

代码语言:javascript
复制
    -:Input:-

    val numOfProjects = 3
    val projectId = arrayOf(1, 2, 0, 1, 2, 0)
    val bids = arrayOf(35, 10, 8, 20, 25, 100)

    -:Output:-
    
    For Project 0 : Comparing bid amount of Dollar 8 with Dollar 100
    For Project 1 : Comparing bid amount of Dollar 35 with Dollar 20
    For Project 2 : Comparing bid amount of Dollar 10 with Dollar 25
    Min cost for All projects is 38 dollars

如果输入类似于

代码语言:javascript
复制
val numOfProjects = 3
val projectId = arrayOf(1, 2, 1, 1, 2, 1)
val bids = arrayOf(35, 10, 8, 20, 25, 100)

那么输出是

代码语言:javascript
复制
No project assigned  

理解

代码语言:javascript
复制
Project 0 min cost is 8
Project 1 min cost is 20
Project 2 min cost is 10
So, total amount is 8+20+10 = 38
EN

回答 2

Stack Overflow用户

发布于 2022-06-24 10:05:57

Kotlin中代码的

代码语言:javascript
复制
    fun main() {
    val numOfProjects = 3
    val projectId = arrayOf(1, 2, 0, 1, 2, 0)
    val bids = arrayOf(35, 10, 8, 20, 25, 100)

    val cost = minCost(numOfProjects, projectId, bids)
    if (cost < 0) {
        println("No project assigned")
    } else {
        println("Min cost for All projects is $cost dollars")
    }
}

fun minCost(numOfProjects: Int, projectId: Array<Int>, bids: Array<Int>): Long {
    var cost = 0L
    // If project id don't contains all the the projects then return -1 or calculate min cost
    for (i in 0 until numOfProjects) {
        if (projectId.contains(i)) {
            cost = calculateMinCost(projectId, bids)
            break
        } else {
            return -1
        }
    }
    return cost
}

// Calculate min cost for all the project based on bid amount given by developer for each project
fun calculateMinCost(projectArray: Array<Int>, bidArray: Array<Int>): Long {
    val developerArray = ArrayList<DeveloperDetails>()

    // For assign unique key to each project and bid data

    val projectHashMap = HashMap<Int, Int>()
    val bidsHashMap = HashMap<Int, Int>()

    for (i in projectArray.indices) {
        projectHashMap[i] = projectArray[i]
    }

    for (i in bidArray.indices) {
        bidsHashMap[i] = bidArray[i]
    }

    for ((key, _) in bidsHashMap) {
        val projectId = projectHashMap.getValue(key)
        val bidAmount = bidsHashMap.getValue(key)

        // Store ProjectId and BidAmount to developer object
        developerArray.add(
            DeveloperDetails(
                projectId = projectId,
                bidAmount = bidAmount
            )
        )
    }
    // Sort developer data based on Project Id
    developerArray.sortBy { it.projectId }

    var minCost = 0
    // Compare each object with other to find out min bid amount of each Project and, add it's amount to minCoast and return it.
    for (i in 0 until developerArray.size) {
        for (j in i + 1 until developerArray.size) {
            if (developerArray[i].projectId == developerArray[j].projectId) {
                println("For Project ${developerArray[i].projectId} : Comparing bid amount of Dollar ${developerArray[i].bidAmount} with Dollar ${developerArray[j].bidAmount}")
                minCost += if (developerArray[i].bidAmount < developerArray[j].bidAmount) {
                    developerArray[i].bidAmount
                } else {
                    developerArray[j].bidAmount
                }
            }
        }
    }
    return minCost.toLong()
}

// For store project and bid amount details
data class DeveloperDetails(val projectId: Int, val bidAmount: Int)
票数 1
EN

Stack Overflow用户

发布于 2022-07-15 18:20:06

Kotlin溶液

代码语言:javascript
复制
Return -1 indicates no project assigned
代码语言:javascript
复制
fun getResult(numProjects: Int, arrProjectIds: Array<Int>, arrBids: Array<Int>): Long {
    return if (arrProjectIds.toSet().size != numProjects) {
        -1
    } else {

        // HashMap of ProjectID, Bid
        val hashMap = hashMapOf<Int, Int>()

        //Result
        var result: Long = 0L

        arrProjectIds.forEachIndexed { index, item ->
            val bid = arrBids[index]
            val bidAtProjectId = hashMap[item]
            if (bidAtProjectId == null) {
                hashMap[item] = bid
            } else if (bid < bidAtProjectId) {
                hashMap[item] = bid
            }
        }

        hashMap.values.forEach {
            result += it.toLong()
        }

        result
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72742378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档