Last active
July 18, 2018 15:03
-
-
Save cyantarek/bd9a36e36fb4c4e768f5feee624c776c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
const maxWeight = 5 | |
var currentWeight int | |
var currentValue int | |
var totalItemsTaken int | |
func main() { | |
//items := map[int]int{1: 100, 3: 500, 4: 50, 5: 200} | |
sortedItems := map[int]int{3: 500, 5: 200, 1: 100, 4: 50} //price sorted items | |
for k, v := range sortedItems { | |
if currentWeight += k; currentWeight > maxWeight { | |
currentWeight -= k | |
continue | |
} else { | |
currentValue += v | |
totalItemsTaken += 1 | |
} | |
} | |
fmt.Println("Weight taken:", currentWeight, "kgs") | |
fmt.Println("Value taken:", currentValue, "Tk") | |
fmt.Println("Items taken:", totalItemsTaken, "Pcs") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment