<?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>class Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/class/</link>
	<description></description>
	<lastBuildDate>Sat, 03 Oct 2020 22:35:38 +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>class Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/class/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1603. Design Parking System</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1603-design-parking-system/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1603-design-parking-system/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 03 Oct 2020 22:34:47 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7439</guid>

					<description><![CDATA[<p>Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1603-design-parking-system/">花花酱 LeetCode 1603. Design Parking System</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>Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.</p>



<p>Implement the&nbsp;<code>ParkingSystem</code>&nbsp;class:</p>



<ul><li><code>ParkingSystem(int big, int medium, int small)</code>&nbsp;Initializes object of the&nbsp;<code>ParkingSystem</code>&nbsp;class. The number of slots for each parking space are given as part of the constructor.</li><li><code>bool addCar(int carType)</code>&nbsp;Checks whether there is a parking space of&nbsp;<code>carType</code>&nbsp;for the car that wants to get into the parking lot.&nbsp;<code>carType</code>&nbsp;can be of three kinds: big, medium, or small, which are represented by&nbsp;<code>1</code>,&nbsp;<code>2</code>, and&nbsp;<code>3</code>&nbsp;respectively.&nbsp;<strong>A car can only park in a parking space of its&nbsp;</strong><code>carType</code>. If there is no space available, return&nbsp;<code>false</code>, else park the car in that size space and return&nbsp;<code>true</code>.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
<strong>Output</strong>
[null, true, true, false, false]
</pre>



<p><strong>Explanation</strong>
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
</p>



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



<ul><li><code>0 &lt;= big, medium, small &lt;= 1000</code></li><li><code>carType</code>&nbsp;is&nbsp;<code>1</code>,&nbsp;<code>2</code>, or&nbsp;<code>3</code></li><li>At most&nbsp;<code>1000</code>&nbsp;calls will be made to&nbsp;<code>addCar</code></li></ul>



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



<p>Time complexity: O(1) per addCar call<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 ParkingSystem {
public:
  ParkingSystem(int big, int medium, int small): 
    slots_{{big, medium, small}} {}

  bool addCar(int carType) {
    return slots_[carType - 1]-- &gt; 0;
  }
private:
  vector&lt;int&gt; slots_;
};</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua
class ParkingSystem:
  def __init__(self, big: int, medium: int, small: int):
    self.slots = {1: big, 2: medium, 3: small}

  def addCar(self, carType: int) -&gt; bool:
    if self.slots[carType] == 0: return False
    self.slots[carType] -= 1
    return True</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1603-design-parking-system/">花花酱 LeetCode 1603. Design Parking System</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-1603-design-parking-system/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1396. Design Underground System</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1396-design-underground-system/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1396-design-underground-system/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Mar 2020 04:35:17 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6543</guid>

					<description><![CDATA[<p>Implement the class&#160;UndergroundSystem&#160;that supports three methods: 1.&#160;checkIn(int id, string stationName, int t) A customer with id card equal to&#160;id, gets in the station&#160;stationName&#160;at time&#160;t. A&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1396-design-underground-system/">花花酱 LeetCode 1396. Design Underground System</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>Implement the class&nbsp;<code>UndergroundSystem</code>&nbsp;that supports three methods:</p>



<p>1.<code>&nbsp;checkIn(int id, string stationName, int t)</code></p>



<ul><li>A customer with id card equal to&nbsp;<code>id</code>, gets in the station&nbsp;<code>stationName</code>&nbsp;at time&nbsp;<code>t</code>.</li><li>A customer&nbsp;can only be checked into one place at a time.</li></ul>



<p>2.<code>&nbsp;checkOut(int id, string stationName, int t)</code></p>



<ul><li>A customer with id card equal to&nbsp;<code>id</code>, gets out from the station&nbsp;<code>stationName</code>&nbsp;at time&nbsp;<code>t</code>.</li></ul>



<p>3.&nbsp;<code>getAverageTime(string startStation, string endStation)</code>&nbsp;</p>



<ul><li>Returns the average time to travel between the&nbsp;<code>startStation</code>&nbsp;and the&nbsp;<code>endStation</code>.</li><li>The average time is computed from all the previous traveling from&nbsp;<code>startStation</code>&nbsp;to&nbsp;<code>endStation</code>&nbsp;that happened&nbsp;<strong>directly</strong>.</li><li>Call to&nbsp;<code>getAverageTime</code>&nbsp;is always valid.</li></ul>



<p>You can assume all calls to&nbsp;<code>checkIn</code>&nbsp;and&nbsp;<code>checkOut</code>&nbsp;methods are consistent. That is, if a customer gets in at time&nbsp;<strong>t<sub>1</sub></strong>&nbsp;at some station, then it gets out at time&nbsp;<strong>t<sub>2</sub></strong>&nbsp;with&nbsp;<strong>t<sub>2</sub>&nbsp;&gt; t<sub>1</sub></strong>.&nbsp;All events happen in chronological order.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]

<strong>Output</strong>
[null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0]

<strong>Explanation</strong>
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);
undergroundSystem.checkOut(27, "Waterloo", 20);
undergroundSystem.checkOut(32, "Cambridge", 22);
undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0
undergroundSystem.checkOut(10, "Waterloo", 38);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.0</p>
</pre>



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



<ul><li>There will be at most&nbsp;<code>20000</code>&nbsp;operations.</li><li><code>1 &lt;= id, t &lt;= 10^6</code></li><li>All strings consist of uppercase, lowercase English letters and digits.</li><li><code>1 &lt;=&nbsp;stationName.length &lt;= 10</code></li><li>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted as correct.</li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<p>For each user, store the checkin station and time.<br>For each trip (startStation + &#8220;_&#8221; + endStation), store the total time and counts.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class UndergroundSystem {
public:
  UndergroundSystem() {}

  void checkIn(int id, string stationName, int t) {
    m_[id] = {stationName, t};
  }

  void checkOut(int id, string stationName, int t) {
    const auto&amp; [s0, t0] = m_[id];
    string key = s0 + &quot;_&quot; + stationName;
    times_[key].first += (t - t0);
    ++times_[key].second;
  }

  double getAverageTime(string startStation, string endStation) {
    const auto&amp; [sum, count] = times_[startStation + &quot;_&quot; + endStation];
    return static_cast&lt;double&gt;(sum) / count;
  }
private:
  unordered_map&lt;int, pair&lt;string, int&gt;&gt; m_; // id -&gt; {station, t}
  unordered_map&lt;string, pair&lt;int, int&gt;&gt; times_; // trip -&gt; {sum, count}
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1396-design-underground-system/">花花酱 LeetCode 1396. Design Underground System</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-1396-design-underground-system/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 592. Fraction Addition and Subtraction</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 07 Aug 2018 05:11:43 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[fraction]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3461</guid>

					<description><![CDATA[<p>Problem Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/">花花酱 LeetCode 592. Fraction Addition and Subtraction</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description__3U1T">
<div>
<h1><strong>Problem</strong></h1>
<p>Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be <a href="https://en.wikipedia.org/wiki/Irreducible_fraction">irreducible fraction</a>. If your final result is an integer, say <code>2</code>, you need to change it to the format of fraction that has denominator <code>1</code>. So in this case, <code>2</code> should be converted to <code>2/1</code>.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b>"-1/2+1/2"
<b>Output:</b> "0/1"
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b>"-1/2+1/2+1/3"
<b>Output:</b> "1/3"
</pre>
<p><b>Example 3:</b></p>
<pre class="crayon:false"><b>Input:</b>"1/3-1/2"
<b>Output:</b> "-1/6"
</pre>
<p><b>Example 4:</b></p>
<pre class="crayon:false"><b>Input:</b>"5/3+1/3"
<b>Output:</b> "2/1"
</pre>
<p><b>Note:</b></p>
<ol>
<li>The input string only contains <code>'0'</code> to <code>'9'</code>, <code>'/'</code>, <code>'+'</code> and <code>'-'</code>. So does the output.</li>
<li>Each fraction (input and output) has format <code>±numerator/denominator</code>. If the first input fraction or the output is positive, then <code>'+'</code> will be omitted.</li>
<li>The input only contains valid <b>irreducible fractions</b>, where the <b>numerator</b> and <b>denominator</b> of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.</li>
<li>The number of given fractions will be in the range [1,10].</li>
<li>The numerator and denominator of the <b>final result</b> are guaranteed to be valid and in the range of 32-bit int.</li>
</ol>
</div>
</div>
<h1><strong>Solution: Math</strong></h1>
<p>a/b+c/d = (a*d + b * c) / (b * d)</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string fractionAddition(string expression) {
    int a = 0;
    int b = 1;    
    int c;
    int d;
    char slash;
    istringstream in(expression);
    while (in &gt;&gt; c &gt;&gt; slash &gt;&gt; d) {
      a = a * d + b * c;
      b *= d;
      int gcd = abs(__gcd(a, b));
      a /= gcd;
      b /= gcd;      
    }
    return to_string(a) + "/" + to_string(b);
  }
};</pre><p></div><h2 class="tabtitle">C++/class</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Fraction {
public:
  Fraction(int a, int b): a_(a), b_(b) {}
  Fraction&amp; operator+=(const Fraction&amp; f) {
    a_ = a_ * f.b_ + b_ * f.a_;
    b_ = b_ * f.b_;
    int d = abs(__gcd(a_, b_));
    a_ /= d;
    b_ /= d;
    return *this;
  }
  
  string toString() {
    return to_string(a_) + "/" + to_string(b_);
  }
private:
  int a_; // hold the sign, e.g. can be +-.
  int b_; // always &gt; 0.
};

class Solution {
public:
    string fractionAddition(string expression) {
      Fraction f(0, 1);
      int a;
      int b;
      char op;
      istringstream in(expression);
      while (in &gt;&gt; a &gt;&gt; op &gt;&gt; b)
        f += Fraction(a, b);  
      return f.toString();
    }
};</pre><p></div></div></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/">花花酱 LeetCode 592. Fraction Addition and Subtraction</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/math/leetcode-592-fraction-addition-and-subtraction/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
