Nk P
Nk P

Reputation: 41

Calculate project total min coast based on the bid amount given by developer for each project : Interview Question

Calculate project total min coast based on the bid amount given by the developer for each project If there is no kind of bid placed for any project then return -1, This means, there must be at least 1 bid for each project or return of -1.

    -: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
    

And If the input is like

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

Then Output is

No project assigned  

Understandings

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

Upvotes: 1

Views: 438

Answers (2)

Sachin Rupani
Sachin Rupani

Reputation: 1

Solution in Kotlin

Return -1 indicates no project assigned
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
    }
}

Upvotes: 0

Nk P
Nk P

Reputation: 41

Solution of code in Kotlin

    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)

Upvotes: 1

Related Questions