{"id":34462,"date":"2024-11-01T09:28:18","date_gmt":"2024-11-01T09:28:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34462"},"modified":"2024-11-01T11:41:10","modified_gmt":"2024-11-01T11:41:10","slug":"javascript-coding-test-course-salesmans-dilemma","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34462\/","title":{"rendered":"javascript coding test course, salesman&#8217;s dilemma"},"content":{"rendered":"<article>\n<header>\n<p>Published on: <time datetime=\"2023-10-01\">October 1, 2023<\/time><\/p>\n<p>Author: Coding Expert<\/p>\n<\/header>\n<section>\n<h2>Problem Description<\/h2>\n<p>A salesman is visiting various cities to sell products. The salesman knows the prices of products that can be sold in each city, as well as the travel costs between cities. The salesman needs to set a goal and find the most efficient route to achieve that goal. In other words, the salesman must visit each city only once and find a route that allows him to return home while maximizing his profit.<\/p>\n<h3>Input<\/h3>\n<p>The input consists of the number of cities <code>n<\/code>, an array of sale prices <code>prices<\/code>, and a 2D array of travel costs <code>costs<\/code>. The prices are represented by <code>prices[i]<\/code> for the price of the product in the i-th city, and the travel costs are represented by <code>costs[i][j]<\/code> for the cost of traveling from city i to city j.<\/p>\n<h3>Output<\/h3>\n<p>The function should return the maximum profit that the salesman can achieve by returning home slowly.<\/p>\n<h3>Constraints<\/h3>\n<ul>\n<li>2 \u2264 n \u2264 10<\/li>\n<li>0 \u2264 prices[i] \u2264 1000<\/li>\n<li>0 \u2264 costs[i][j] \u2264 1000<\/li>\n<\/ul>\n<h2>Problem-Solving Approach<\/h2>\n<p>This problem is similar to the &#8216;Traveling Salesman Problem&#8217; and can be solved using backtracking or dynamic programming. Essentially, the salesman needs to try all possible combinations to optimize the route that visits all cities and returns home.<\/p>\n<h3>Step 1: Understanding the Problem<\/h3>\n<p>Since the salesman must visit all cities, he needs to explore all paths between cities while considering the item sale profits and travel costs for each path. The goal is to calculate profits and costs to select the optimal route.<\/p>\n<h3>Step 2: Designing the Algorithm<\/h3>\n<p>To solve this problem, follow these steps:<\/p>\n<ul>\n<li>Assume each city as the starting city and visit each city while exploring all possible paths.<\/li>\n<li>Calculate the sale profits and travel costs for each path to update the optimal profit.<\/li>\n<li>After visiting all cities, calculate the cost to return to the original city as well.<\/li>\n<\/ul>\n<h3>Step 3: Implementation<\/h3>\n<p>Now let&#8217;s move on to the implementation phase. We will write a function in JavaScript to find the maximum profit of the salesman.<\/p>\n<pre><code>\nfunction maxProfit(n, prices, costs) {\n    let maxProfit = -Infinity;\n\n    function backtrack(currentCity, visited, currentProfit) {\n        \/\/ If all cities are visited, return home.\n        if (visited.length === n) {\n            const returnCost = costs[currentCity][0]; \/\/ Cost to return to the starting city\n            const totalProfit = currentProfit - returnCost; \/\/ Calculate total profit\n            maxProfit = Math.max(maxProfit, totalProfit);\n            return;\n        }\n\n        \/\/ Visit each city.\n        for (let nextCity = 0; nextCity < n; nextCity++) {\n            if (!visited.includes(nextCity)) {\n                visited.push(nextCity); \/\/ Record city visit\n                const nextProfit = currentProfit + prices[nextCity]; \/\/ Calculate profit for next city\n                const travelCost = costs[currentCity][nextCity]; \/\/ Travel cost\n                backtrack(nextCity, visited, nextProfit - travelCost); \/\/ Recursive call\n                visited.pop(); \/\/ Remove visit record (backtracking)\n            }\n        }\n    }\n\n    \/\/ Start from the 0th city\n    backtrack(0, [0], prices[0]);\n\n    return maxProfit;\n}\n\n\/\/ Example usage\nconst n = 4;\nconst prices = [100, 70, 90, 40];\nconst costs = [\n    [0, 10, 15, 20],\n    [10, 0, 35, 25],\n    [15, 35, 0, 30],\n    [20, 25, 30, 0]\n];\n\nconsole.log(maxProfit(n, prices, costs));\n        <\/code><\/pre>\n<h3>Step 4: Code Explanation<\/h3>\n<p>The <code>maxProfit<\/code> function defined above performs the following tasks:<\/p>\n<ul>\n<li><code>currentCity<\/code>: Tracks the current city.<\/li>\n<li><code>visited<\/code>: Tracks the cities visited so far.<\/li>\n<li><code>currentProfit<\/code>: Tracks the cumulative profit so far.<\/li>\n<\/ul>\n<p>We recursively explore each city. After visiting all cities, we calculate the cost to return home to update the total profit.<\/p>\n<h2>Example Test<\/h2>\n<p>When running the code, the <code>maxProfit<\/code> function will return the maximum profit. It is advisable to experiment with various input values to observe the performance of the algorithm.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this lesson, we explored the Traveling Salesman Problem. It is important to understand the theoretical background and implementation methods that frequently appear in coding tests. By exploring various routes and quantitatively calculating the optimal profit, we learned how to utilize the powerful features of JavaScript.<\/p>\n<p>In the next session, we will cover another algorithm problem. If you have any questions or feedback, please leave a comment!<\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Published on: October 1, 2023 Author: Coding Expert Problem Description A salesman is visiting various cities to sell products. The salesman knows the prices of products that can be sold in each city, as well as the travel costs between cities. The salesman needs to set a goal and find the most efficient route to &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34462\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;javascript coding test course, salesman&#8217;s dilemma&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[141],"tags":[],"class_list":["post-34462","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>javascript coding test course, salesman&#039;s dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34462\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"javascript coding test course, salesman&#039;s dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Published on: October 1, 2023 Author: Coding Expert Problem Description A salesman is visiting various cities to sell products. The salesman knows the prices of products that can be sold in each city, as well as the travel costs between cities. The salesman needs to set a goal and find the most efficient route to &hellip; \ub354 \ubcf4\uae30 &quot;javascript coding test course, salesman&#8217;s dilemma&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34462\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:10+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"javascript coding test course, salesman&#8217;s dilemma\",\"datePublished\":\"2024-11-01T09:28:18+00:00\",\"dateModified\":\"2024-11-01T11:41:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/\"},\"wordCount\":478,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34462\/\",\"name\":\"javascript coding test course, salesman's dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:18+00:00\",\"dateModified\":\"2024-11-01T11:41:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34462\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34462\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"javascript coding test course, salesman&#8217;s dilemma\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"javascript coding test course, salesman's dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34462\/","og_locale":"ko_KR","og_type":"article","og_title":"javascript coding test course, salesman's dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Published on: October 1, 2023 Author: Coding Expert Problem Description A salesman is visiting various cities to sell products. The salesman knows the prices of products that can be sold in each city, as well as the travel costs between cities. The salesman needs to set a goal and find the most efficient route to &hellip; \ub354 \ubcf4\uae30 \"javascript coding test course, salesman&#8217;s dilemma\"","og_url":"https:\/\/atmokpo.com\/w\/34462\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:18+00:00","article_modified_time":"2024-11-01T11:41:10+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34462\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34462\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"javascript coding test course, salesman&#8217;s dilemma","datePublished":"2024-11-01T09:28:18+00:00","dateModified":"2024-11-01T11:41:10+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34462\/"},"wordCount":478,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34462\/","url":"https:\/\/atmokpo.com\/w\/34462\/","name":"javascript coding test course, salesman's dilemma - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:18+00:00","dateModified":"2024-11-01T11:41:10+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34462\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34462\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34462\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"javascript coding test course, salesman&#8217;s dilemma"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34462"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34462\/revisions"}],"predecessor-version":[{"id":34463,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34462\/revisions\/34463"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}