Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Advanced DP

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Advanced DP

Travelling Salesman (Bitmask DP)

advancedlayer A1 · Advanced DP5 tests

You are given an n x n matrix cost where cost[i][j] is the price of travelling from city i to city j. Starting at city 0, visit every other city exactly once and come back to city 0. Return the cheapest total price of such a tour.

  • The tour always starts and ends at city 0
  • n is at most 12, so an exponential-in-n answer is expected — but n! permutations is not
  • cost[i][i] is 0 and the matrix need not be symmetric
  • A single city (n === 1) costs 0

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen