Skip to content

Instantly share code, notes, and snippets.

@cyantarek
Last active July 18, 2018 15:03
Show Gist options
  • Save cyantarek/bd9a36e36fb4c4e768f5feee624c776c to your computer and use it in GitHub Desktop.
Save cyantarek/bd9a36e36fb4c4e768f5feee624c776c to your computer and use it in GitHub Desktop.
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