{"id":10110,"date":"2024-02-05T17:49:25","date_gmt":"2024-02-06T01:49:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10110"},"modified":"2024-02-05T17:49:58","modified_gmt":"2024-02-06T01:49:58","slug":"leetcode-3028-ant-on-the-boundary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-3028-ant-on-the-boundary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3028. Ant on the Boundary"},"content":{"rendered":"\n<p>An ant is on a boundary. It sometimes goes&nbsp;<strong>left<\/strong>&nbsp;and sometimes&nbsp;<strong>right<\/strong>.<\/p>\n\n\n\n<p>You are given an array of&nbsp;<strong>non-zero<\/strong>&nbsp;integers&nbsp;<code>nums<\/code>. The ant starts reading&nbsp;<code>nums<\/code>&nbsp;from the first element of it to its end. At each step, it moves according to the value of the current element:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If&nbsp;<code>nums[i] &lt; 0<\/code>, it moves&nbsp;<strong>left<\/strong>&nbsp;by&nbsp;<code>-nums[i]<\/code>&nbsp;units.<\/li><li>If&nbsp;<code>nums[i] &gt; 0<\/code>, it moves&nbsp;<strong>right<\/strong>&nbsp;by&nbsp;<code>nums[i]<\/code>&nbsp;units.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of times the ant&nbsp;<strong>returns<\/strong>&nbsp;to the boundary.<\/em><\/p>\n\n\n\n<p><strong>Notes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There is an infinite space on both sides of the boundary.<\/li><li>We check whether the ant is on the boundary only after it has moved&nbsp;<code>|nums[i]|<\/code>&nbsp;units. In other words, if the ant crosses the boundary during its movement, it does not count.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,-5]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> After the first step, the ant is 2 steps to the right of the boundary.\nAfter the second step, the ant is 5 steps to the right of the boundary.\nAfter the third step, the ant is on the boundary.\nSo the answer is 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,2,-3,-4]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> After the first step, the ant is 3 steps to the right of the boundary.\nAfter the second step, the ant is 5 steps to the right of the boundary.\nAfter the third step, the ant is 2 steps to the right of the boundary.\nAfter the fourth step, the ant is 2 steps to the left of the boundary.\nThe ant never returned to the boundary, so the answer is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>-10 &lt;= nums[i] &lt;= 10<\/code><\/li><li><code>nums[i] != 0<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation <\/strong><\/h2>\n\n\n\n<p>Simulate the position of the ant by summing up the numbers. If the position is zero (at boundary), increase the answer by 1.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int returnToBoundaryCount(vector<int>& nums) {\n    int ans = 0;\n    int pos = 0;\n    for (int x : nums)\n      ans += !(pos += x);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An ant is on a boundary. It sometimes goes&nbsp;left&nbsp;and sometimes&nbsp;right. You are given an array of&nbsp;non-zero&nbsp;integers&nbsp;nums. The ant starts reading&nbsp;nums&nbsp;from the first element of it&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,179],"class_list":["post-10110","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10110"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10110\/revisions"}],"predecessor-version":[{"id":10112,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10110\/revisions\/10112"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}