<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>base10 Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/base10/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/base10/</link>
	<description></description>
	<lastBuildDate>Mon, 28 Feb 2022 14:58:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>base10 Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/base10/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2180. Count Integers With Even Digit Sum</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2180-count-integers-with-even-digit-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2180-count-integers-with-even-digit-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Feb 2022 13:51:26 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[base10]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[num]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9519</guid>

					<description><![CDATA[<p>Given a positive integer&#160;num, return&#160;the number of positive integers&#160;less than or equal to&#160;num&#160;whose digit sums are&#160;even. The&#160;digit sum&#160;of a positive integer is the sum of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2180-count-integers-with-even-digit-sum/">花花酱 LeetCode 2180. Count Integers With Even Digit Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a positive integer&nbsp;<code>num</code>, return&nbsp;<em>the number of positive integers&nbsp;<strong>less than or equal to</strong></em>&nbsp;<code>num</code>&nbsp;<em>whose digit sums are&nbsp;<strong>even</strong></em>.</p>



<p>The&nbsp;<strong>digit sum</strong>&nbsp;of a positive integer is the sum of all its digits.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The only integers less than or equal to 4 whose digit sums are even are 2 and 4.    
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 30
<strong>Output:</strong> 14
<strong>Explanation:</strong>
The 14 integers less than or equal to 30 whose digit sums are even are
2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= num &lt;= 1000</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Use std::to_string to convert an integer to string.</p>



<p>Time complexity: O(nlgn)<br>Space complexity: O(lgn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countEven(int num) {
    int ans = 0;
    for (int i = 1; i &lt;= num; ++i) {
      int sum = 0;
      for (char c : to_string(i))
        sum += (c - '0');
      if (sum % 2 == 0) ++ans;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2180-count-integers-with-even-digit-sum/">花花酱 LeetCode 2180. Count Integers With Even Digit Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-2180-count-integers-with-even-digit-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1945. Sum of Digits of String After Convert</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1945-sum-of-digits-of-string-after-convert/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1945-sum-of-digits-of-string-after-convert/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 20:23:55 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[base10]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9311</guid>

					<description><![CDATA[<p>You are given a string&#160;s&#160;consisting of lowercase English letters, and an integer&#160;k. First,&#160;convert&#160;s&#160;into an integer by replacing each letter with its position in the alphabet&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1945-sum-of-digits-of-string-after-convert/">花花酱 LeetCode 1945. Sum of Digits of String After Convert</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a string&nbsp;<code>s</code>&nbsp;consisting of lowercase English letters, and an integer&nbsp;<code>k</code>.</p>



<p>First,&nbsp;<strong>convert</strong>&nbsp;<code>s</code>&nbsp;into an integer by replacing each letter with its position in the alphabet (i.e., replace&nbsp;<code>'a'</code>&nbsp;with&nbsp;<code>1</code>,&nbsp;<code>'b'</code>&nbsp;with&nbsp;<code>2</code>, &#8230;,&nbsp;<code>'z'</code>&nbsp;with&nbsp;<code>26</code>). Then,&nbsp;<strong>transform</strong>&nbsp;the integer by replacing it with the&nbsp;<strong>sum of its digits</strong>. Repeat the&nbsp;<strong>transform</strong>&nbsp;operation&nbsp;<code>k</code><strong>&nbsp;times</strong>&nbsp;in total.</p>



<p>For example, if&nbsp;<code>s = "zbax"</code>&nbsp;and&nbsp;<code>k = 2</code>, then the resulting integer would be&nbsp;<code>8</code>&nbsp;by the following operations:</p>



<ul><li><strong>Convert</strong>:&nbsp;<code>"zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124</code></li><li><strong>Transform #1</strong>:&nbsp;<code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li><li><strong>Transform #2</strong>:&nbsp;<code>17 ➝ 1 + 7 ➝ 8</code></li></ul>



<p>Return&nbsp;<em>the resulting integer after performing the operations described above</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "iiii", k = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The operations are as follows:
- Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999
- Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
Thus the resulting integer is 36.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcode", k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong> The operations are as follows:
- Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545
- Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
- Transform #2: 33 ➝ 3 + 3 ➝ 6
Thus the resulting integer is 6.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "zbax", k = 2
<strong>Output:</strong> 8
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 100</code></li><li><code>1 &lt;= k &lt;= 10</code></li><li><code>s</code>&nbsp;consists of lowercase English letters.</li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(klogn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int getLucky(string s, int k) {
    int ans = 0;
    for (char c : s) {
      int n = c - 'a' + 1;
      ans += n % 10;
      ans += n / 10;
    }
    while (--k) {
      int n = ans;
      ans = 0;
      while (n) {
        ans += n % 10;
        n /= 10;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1945-sum-of-digits-of-string-after-convert/">花花酱 LeetCode 1945. Sum of Digits of String After Convert</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-1945-sum-of-digits-of-string-after-convert/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1742. Maximum Number of Balls in a Box</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1742-maximum-number-of-balls-in-a-box/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1742-maximum-number-of-balls-in-a-box/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 Jan 2021 04:31:10 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[base10]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8045</guid>

					<description><![CDATA[<p>You are working in a ball factory where you have&#160;n&#160;balls numbered from&#160;lowLimit&#160;up to&#160;highLimit&#160;inclusive&#160;(i.e.,&#160;n == highLimit - lowLimit + 1), and an infinite number of boxes&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1742-maximum-number-of-balls-in-a-box/">花花酱 LeetCode 1742. Maximum Number of Balls in a Box</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are working in a ball factory where you have&nbsp;<code>n</code>&nbsp;balls numbered from&nbsp;<code>lowLimit</code>&nbsp;up to&nbsp;<code>highLimit</code>&nbsp;<strong>inclusive</strong>&nbsp;(i.e.,&nbsp;<code>n == highLimit - lowLimit + 1</code>), and an infinite number of boxes numbered from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>infinity</code>.</p>



<p>Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball&#8217;s number. For example, the ball number&nbsp;<code>321</code>&nbsp;will be put in the box number&nbsp;<code>3 + 2 + 1 = 6</code>&nbsp;and the ball number&nbsp;<code>10</code>&nbsp;will be put in the box number&nbsp;<code>1 + 0 = 1</code>.</p>



<p>Given two integers&nbsp;<code>lowLimit</code>&nbsp;and&nbsp;<code>highLimit</code>, return<em>&nbsp;the number of balls in the box with the most balls.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lowLimit = 1, highLimit = 10
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Box Number:  1 2 3 4 5 6 7 8 9 10 11 ...
Ball Count:  2 1 1 1 1 1 1 1 1 0  0  ...
Box 1 has the most number of balls with 2 balls.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lowLimit = 5, highLimit = 15
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Box Number:  1 2 3 4 5 6 7 8 9 10 11 ...
Ball Count:  1 1 1 1 2 2 1 1 1 0  0  ...
Boxes 5 and 6 have the most number of balls with 2 balls in each.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lowLimit = 19, highLimit = 28
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Box Number:  1 2 3 4 5 6 7 8 9 10 11 12 ...
Ball Count:  0 1 1 1 1 1 1 1 1 2  0  0  ...
Box 10 has the most number of balls with 2 balls.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= lowLimit &lt;= highLimit &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Hashtable and base-10</strong></h2>



<p>Max sum will be 9+9+9+9+9 = 45</p>



<p>Time complexity: O((hi-lo) * log(hi))<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countBalls(int lowLimit, int highLimit) {
    vector&lt;int&gt; balls(46);
    int ans = 0;
    for (int i = lowLimit; i &lt;= highLimit; ++i) {
      int n = i;
      int box = 0;
      while (n) { box += n % 10; n /= 10; }
      ans = max(ans, ++balls[box]);
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def countBalls(self, lowLimit: int, highLimit: int) -&gt; int:
    balls = defaultdict(int)
    ans = 0
    for x in range(lowLimit, highLimit + 1):
      s = sum(int(d) for d in str(x))
      balls[s] += 1
      ans = max(ans, balls[s])
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1742-maximum-number-of-balls-in-a-box/">花花酱 LeetCode 1742. Maximum Number of Balls in a Box</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-1742-maximum-number-of-balls-in-a-box/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
