{"id":9499,"date":"2022-02-13T05:59:06","date_gmt":"2022-02-13T13:59:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9499"},"modified":"2022-02-13T06:03:22","modified_gmt":"2022-02-13T14:03:22","slug":"leetcode-2169-count-operations-to-obtain-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2169-count-operations-to-obtain-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2169. Count Operations to Obtain Zero"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>non-negative<\/strong>&nbsp;integers&nbsp;<code>num1<\/code>&nbsp;and&nbsp;<code>num2<\/code>.<\/p>\n\n\n\n<p>In one&nbsp;<strong>operation<\/strong>, if&nbsp;<code>num1 &gt;= num2<\/code>, you must subtract&nbsp;<code>num2<\/code>&nbsp;from&nbsp;<code>num1<\/code>, otherwise subtract&nbsp;<code>num1<\/code>&nbsp;from&nbsp;<code>num2<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>num1 = 5<\/code>&nbsp;and&nbsp;<code>num2 = 4<\/code>, subtract&nbsp;<code>num2<\/code>&nbsp;from&nbsp;<code>num1<\/code>, thus obtaining&nbsp;<code>num1 = 1<\/code>&nbsp;and&nbsp;<code>num2 = 4<\/code>. However, if&nbsp;<code>num1 = 4<\/code>&nbsp;and&nbsp;<code>num2 = 5<\/code>, after one operation,&nbsp;<code>num1 = 4<\/code>&nbsp;and&nbsp;<code>num2 = 1<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of operations<\/strong>&nbsp;required to make either<\/em>&nbsp;<code>num1 = 0<\/code>&nbsp;<em>or<\/em>&nbsp;<code>num2 = 0<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num1 = 2, num2 = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \n- Operation 1: num1 = 2, num2 = 3. Since num1 &lt; num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.\n- Operation 2: num1 = 2, num2 = 1. Since num1 &gt; num2, we subtract num2 from num1.\n- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.\nNow num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.\nSo the total number of operations required is 3.\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> num1 = 10, num2 = 10\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> \n- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.\nNow num1 = 0 and num2 = 10. Since num1 == 0, we are done.\nSo the total number of operations required is 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= num1, num2 &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(max(n,m) \/  min(n, m))<br>Space complexity: O(1)<\/p>\n\n\n\n<p>No code<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Simualtion + Math<\/strong><\/h2>\n\n\n\n<p>For the case of 100, 3<br>100 &#8211; 3 = 97<br>97 &#8211; 3 = 94<br>&#8230;<br>4 &#8211; 3 = 1<br>Swap<br>3 &#8211; 1 = 2<br>2 &#8211; 1 = 1<br>1 &#8211; 1 = 0<br>It takes 36 steps.<\/p>\n\n\n\n<p>We can do 100 \/ 3 to skip 33 steps <br>100 %= 3 = 1<br>3 \/ 1 = 3 to skip 3 steps<br>3 %= 1 = 0<br>total is 33 + 3 = 36.<\/p>\n\n\n\n<p>Time complexity: O(logn) ? <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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countOperations(int num1, int num2) {\n    int ans = 0;\n    while (num1 && num2) {\n      if (num1 < num2) swap(num1, num2);\n      ans += num1 \/ num2;\n      num1 %= num2;      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;non-negative&nbsp;integers&nbsp;num1&nbsp;and&nbsp;num2. In one&nbsp;operation, if&nbsp;num1 &gt;= num2, you must subtract&nbsp;num2&nbsp;from&nbsp;num1, otherwise subtract&nbsp;num1&nbsp;from&nbsp;num2. For example, if&nbsp;num1 = 5&nbsp;and&nbsp;num2 = 4, subtract&nbsp;num2&nbsp;from&nbsp;num1, thus obtaining&nbsp;num1 =&#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,31,179],"class_list":["post-9499","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-math","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9499","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=9499"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9499\/revisions"}],"predecessor-version":[{"id":9503,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9499\/revisions\/9503"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}