{"id":34436,"date":"2024-11-01T09:28:06","date_gmt":"2024-11-01T09:28:06","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34436"},"modified":"2024-11-01T11:41:16","modified_gmt":"2024-11-01T11:41:16","slug":"javascript-coding-test-course-fast-forward-with-time-machine","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34436\/","title":{"rendered":"JavaScript Coding Test Course, Fast Forward with Time Machine"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Coding tests are becoming mandatory for more and more companies, and JavaScript is one of the most popular languages in web development.<br \/>\n        In this course, we will solve commonly occurring algorithm problems found in coding tests using JavaScript.<br \/>\n        Today&#8217;s topic is the problem &#8216;Fast Travel with a Time Machine&#8217;.\n    <\/p>\n<h2>Problem Description<\/h2>\n<p>\n        You are a scientist who can operate a time machine. The time machine can move to a specific time, and<br \/>\n        given two integers a and b, you need to calculate the minimum time (number of moves) it takes to move from a to b.<br \/>\n        The time machine follows these rules:\n    <\/p>\n<ul>\n<li>You can add 1 or subtract 1 from your current position.<\/li>\n<li>You can double your current position.<\/li>\n<\/ul>\n<p>\n        Write a function that calculates the minimum number of moves required to travel from a to b for given a and b.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        &#8211; Two integers a (0 \u2264 a \u2264 10<sup>5<\/sup>), b (0 \u2264 b \u2264 10<sup>5<\/sup>) are given.\n    <\/p>\n<h2>Output Format<\/h2>\n<p>\n        &#8211; Output the minimum number of moves required to travel from a to b as an integer.\n    <\/p>\n<h2>Examples<\/h2>\n<pre>\n        Input: a = 2, b = 9\n        Output: 4\n    <\/pre>\n<pre>\n        Input: a = 5, b = 22\n        Output: 7\n    <\/pre>\n<h2>Approach to Solve the Problem<\/h2>\n<p>\n        To solve this problem, we will use BFS (Breadth-First Search). BFS is an algorithm suitable for finding the shortest path,<br \/>\n        exploring all possible states from the given state to find the shortest move sequence to reach the target state.<br \/>\n        In this case, each state represents a time the time machine could occupy.\n    <\/p>\n<h3>Step 1: Prepare to Use BFS Algorithm<\/h3>\n<p>\n        To implement BFS, we will use a queue. First, we add the starting position a to the queue.<br \/>\n        Then, we will repeat adding all possible moves to the queue until we reach the target position b.<br \/>\n        The possible moves from each position are as follows:\n    <\/p>\n<ul>\n<li>Add 1 to the current position<\/li>\n<li>Subtract 1 from the current position<\/li>\n<li>Double the current position<\/li>\n<\/ul>\n<p>\n        We will keep track of the number of moves taken for each move until we reach the target position, and output the count when we reach the target.\n    <\/p>\n<h3>Step 2: Implement the Code<\/h3>\n<pre><code>\nfunction minimumMoves(a, b) {\n    if (a >= b) return a - b; \/\/ If a is greater than or equal to b, determine moves by simply subtracting the difference\n    const queue = [[a, 0]]; \/\/ [current position, number of moves]\n    const visited = new Set(); \/\/ Record visited positions\n    visited.add(a); \/\/ Mark the starting position as visited\n\n    while (queue.length > 0) {\n        const [current, moves] = queue.shift(); \n\n        \/\/ Possible moves\n        const nextMoves = [current - 1, current + 1, current * 2]; \n\n        for (let next of nextMoves) {\n            if (next === b) return moves + 1; \/\/ Return move count when the target position is reached\n            if (next < 0 || next > 100000 || visited.has(next)) continue; \/\/ Only for valid range and unvisited positions\n            visited.add(next); \/\/ Mark the next position as visited\n            queue.push([next, moves + 1]); \/\/ Add the next position and move count to the queue\n        }\n    }\n}\n    <\/code><\/pre>\n<h2>Step 3: Analyze Algorithm Complexity<\/h2>\n<p>\n        The time complexity of the algorithm is O(n).<br \/>\n        In the worst case, we need to explore all possible positions, which corresponds to O(n),<br \/>\n        and the space complexity is also O(n). This complexity arises from the space needed for the queue and visited records.\n    <\/p>\n<h2>Step 4: Optimization Possibilities<\/h2>\n<p>\n        This algorithm is already based on BFS, which explores the shortest path<br \/>\n        and does not require additional optimization. However, depending on the situation,<br \/>\n        DFS (Depth-First Search) could also be applied, but BFS is more effective for this problem.\n    <\/p>\n<h2>Step 5: Conclusion<\/h2>\n<p>\n        Through the &#8216;Fast Travel with a Time Machine&#8217; problem, we have briefly understood the principle of BFS and learned how to solve a problem using JavaScript.<br \/>\n        In this way, various problems can be solved, and it is crucial to master basic algorithms to achieve good results in coding tests.\n    <\/p>\n<h2>Additional Resources<\/h2>\n<p>\n        &#8211; To further study the BFS algorithm and practice various problem-solving, we recommend the following resources.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/breadth-first-search-or-bfs-for-a-graph\/\">BFS Algorithm on GeeksforGeeks<\/a><\/li>\n<li><a href=\"https:\/\/leetcode.com\/problemset\/all\/\">LeetCode Problem Set<\/a><\/li>\n<li><a href=\"https:\/\/www.hackerrank.com\/domains\/tutorials\/10-days-of-java\">HackerRank &#8211; 10 Days of Java Tutorial<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coding tests are becoming mandatory for more and more companies, and JavaScript is one of the most popular languages in web development. In this course, we will solve commonly occurring algorithm problems found in coding tests using JavaScript. Today&#8217;s topic is the problem &#8216;Fast Travel with a Time Machine&#8217;. Problem Description You are a scientist &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34436\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Fast Forward with Time Machine&#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-34436","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, Fast Forward with Time Machine - \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\/34436\/\" \/>\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, Fast Forward with Time Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Coding tests are becoming mandatory for more and more companies, and JavaScript is one of the most popular languages in web development. In this course, we will solve commonly occurring algorithm problems found in coding tests using JavaScript. Today&#8217;s topic is the problem &#8216;Fast Travel with a Time Machine&#8217;. Problem Description You are a scientist &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Fast Forward with Time Machine&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34436\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:16+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\/34436\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34436\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Fast Forward with Time Machine\",\"datePublished\":\"2024-11-01T09:28:06+00:00\",\"dateModified\":\"2024-11-01T11:41:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34436\/\"},\"wordCount\":506,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34436\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34436\/\",\"name\":\"JavaScript Coding Test Course, Fast Forward with Time Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:06+00:00\",\"dateModified\":\"2024-11-01T11:41:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34436\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34436\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34436\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Fast Forward with Time Machine\"}]},{\"@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, Fast Forward with Time Machine - \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\/34436\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Fast Forward with Time Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Coding tests are becoming mandatory for more and more companies, and JavaScript is one of the most popular languages in web development. In this course, we will solve commonly occurring algorithm problems found in coding tests using JavaScript. Today&#8217;s topic is the problem &#8216;Fast Travel with a Time Machine&#8217;. Problem Description You are a scientist &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Fast Forward with Time Machine\"","og_url":"https:\/\/atmokpo.com\/w\/34436\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:06+00:00","article_modified_time":"2024-11-01T11:41:16+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\/34436\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34436\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Fast Forward with Time Machine","datePublished":"2024-11-01T09:28:06+00:00","dateModified":"2024-11-01T11:41:16+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34436\/"},"wordCount":506,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34436\/","url":"https:\/\/atmokpo.com\/w\/34436\/","name":"JavaScript Coding Test Course, Fast Forward with Time Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:06+00:00","dateModified":"2024-11-01T11:41:16+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34436\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34436\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34436\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Fast Forward with Time Machine"}]},{"@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\/34436","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=34436"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34436\/revisions"}],"predecessor-version":[{"id":34437,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34436\/revisions\/34437"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}