tag:blogger.com,1999:blog-14027264181069204782024-02-20T07:50:33.787-08:00Baozi Training Blog (包子IT面试培训博客)<a href="http://baozitraining.org">Baozi Training Website (包子IT面试培训网站) </a>Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.comBlogger98125tag:blogger.com,1999:blog-1402726418106920478.post-58741320544007060012022-09-18T11:39:00.010-07:002022-09-18T15:43:53.496-07:00Leetcode solution 2353. Design a Food Rating System<p> </p><h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Design a food rating system that can do the following:</p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><b style="box-sizing: border-box; font-weight: bolder;">Modify</b><span> </span>the rating of a food item listed in the system.</li><li style="box-sizing: border-box;">Return the highest-rated food item for a type of cuisine in the system.</li></ul><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Implement the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">FoodRatings</code><span> </span>class:</p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code><span> </span>Initializes the system. The food items are described by<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">foods</code>,<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">cuisines</code><span> </span>and<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">ratings</code>, all of which have a length of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n</code>.<ul style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">foods[i]</code><span> </span>is the name of the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</sup></code><span> </span>food,</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">cuisines[i]</code><span> </span>is the type of cuisine of the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</sup></code><span> </span>food, and</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">ratings[i]</code><span> </span>is the initial rating of the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</sup></code><span> </span>food.</li></ul></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">void changeRating(String food, int newRating)</code><span> </span>Changes the rating of the food item with the name<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">food</code>.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">String highestRated(String cuisine)</code><span> </span>Returns the name of the food item that has the highest rating for the given type of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">cuisine</code>. If there is a tie, return the item with the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">lexicographically smaller</b><span> </span>name.</li></ul><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Note that a string<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x</code><span> </span>is lexicographically smaller than string<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">y</code><span> </span>if<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x</code><span> </span>comes before<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">y</code><span> </span>in dictionary order, that is, either<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x</code><span> </span>is a prefix of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">y</code>, or if<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i</code><span> </span>is the first position such that<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x[i] != y[i]</code>, then<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x[i]</code><span> </span>comes before<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">y[i]</code><span> </span>in alphabetic order.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input</b>
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
<b style="box-sizing: border-box; font-weight: bolder;">Output</b>
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
<b style="box-sizing: border-box; font-weight: bolder;">Explanation</b>
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
foodRatings.highestRated("korean"); // return "kimchi"
// "kimchi" is the highest rated korean food with a rating of 9.
foodRatings.highestRated("japanese"); // return "ramen"
// "ramen" is the highest rated japanese food with a rating of 14.
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "sushi"
// "sushi" is the highest rated japanese food with a rating of 16.
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "ramen"
// Both "sushi" and "ramen" have a rating of 16.
// However, "ramen" is lexicographically smaller than "sushi".
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= n <= 2 * 10<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">4</sup></code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n == foods.length == cuisines.length == ratings.length</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= foods[i].length, cuisines[i].length <= 10</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">foods[i]</code>,<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">cuisines[i]</code><span> </span>consist of lowercase English letters.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= ratings[i] <= 10<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">8</sup></code></li><li style="box-sizing: border-box;">All the strings in<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">foods</code><span> </span>are<span> </span><b style="box-sizing: border-box; font-weight: bolder;">distinct</b>.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">food</code><span> </span>will be the name of a food item in the system across all calls to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">changeRating</code>.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">cuisine</code><span> </span>will be a type of cuisine of<span> </span><b style="box-sizing: border-box; font-weight: bolder;">at least one</b><span> </span>food item in the system across all calls to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">highestRated</code>.</li><li style="box-sizing: border-box;">At most<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">2 * 10<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">4</sup></code><span> </span>calls<span> </span><b style="box-sizing: border-box; font-weight: bolder;">in total</b><span> </span>will be made to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">changeRating</code><span> </span>and<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">highestRated</code>.</li></ul><p><a href="https://leetcode.com/problems/design-a-food-rating-system/" target="_blank">Problem link</a></p><p> <br />
</p><h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/TFTGvVnnq1M" target="_blank">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV1Ve4y1k7Fk/" target="_blank">B站</a> <br /></li></ul>
<h3> </h3><h3>Thought Process</h3><p>If we want to optimize highestRated API call (which we should), a max heap would help achieve it with O(1). Some details about implementation. </p><ul style="text-align: left;"><li>Make each food into a class would make code clean, also maintain the order when insert/remove from the max heap through Comparable interface. </li><li>Make sure we have O(1) access to each Food object by maintaining two HashMaps. </li><ul><li>foodIndex, Map<String, Food> -> Key: food name, Value: Food object </li><li>CuisineIndex, Map<String, Queue<Food>>, Key: cuisine name, Value: Max heap on rating </li></ul><li>Follow up thought: if this is a system design question, how would you actually do it? Some read on how <a href="https://blog.baozitraining.org/2020/03/system-design-interview-how-to-design-comments.html" target="_blank">Youtube calculates total views</a>. <br /></li></ul><h3> </h3><h3>Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3><div><div><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/cfb128c356e6b0654f1578ac84823b00.js"></script>Time Complexity: highestRated: O(1), changeRating: O(N) because we have to remove the object, and in order to find the object in a max heap, it is O(N) scan (even though re-insert is O(lgN)). <br /></div><div>Space Complexity: O(N) because we used extra maps linear to food names (which is larger than cuisine names) </div><div><br /></div></div>
<h3>
References</h3>
<ul><li><a href="https://blog.baozitraining.org/2018/02/leetcode-solution-621-task-scheduler.html" target="_blank">Task Scheduler using heap</a></li><li><a href="https://blog.baozitraining.org/2020/02/leetcode-solution-146-lru-cache.html" target="_blank">LRU Cache using heap </a><br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-83509000329072653472022-07-09T14:50:00.003-07:002022-07-10T17:09:06.032-07:00Leetcode solution 2320. Count Number of Ways to Place Houses<p> <br /></p><h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">There is a street with<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n * 2</code><span> </span><b style="box-sizing: border-box; font-weight: bolder;">plots</b>, where there are<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n</code><span> </span>plots on each side of the street. The plots on each side are numbered from<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1</code><span> </span>to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n</code>. On each plot, a house can be placed.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Return<span> </span><i style="box-sizing: border-box;">the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street</i>. Since the answer may be very large, return it<span> </span><b style="box-sizing: border-box; font-weight: bolder;">modulo</b><span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">10<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">9</sup><span> </span>+ 7</code>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Note that if a house is placed on the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</sup></code><span> </span>plot on one side of the street, a house can also be placed on the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</sup></code><span> </span>plot on the other side of the street.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> n = 1
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 4
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b>
Possible arrangements:
1. All plots are empty.
2. A house is placed on one side of the street.
3. A house is placed on the other side of the street.
4. Two houses are placed, one on each side of the street.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="-webkit-text-stroke-width: 0px; background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; height: 500px; letter-spacing: normal; max-width: 100%; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; vertical-align: middle; white-space: normal; widows: 2; width: 500px; word-spacing: 0px;" /><span face="-apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="-webkit-text-stroke-width: 0px; background-color: white; color: #263238; display: inline; float: none; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></span><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> n = 2
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 9
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> The 9 possible arrangements are shown in the diagram above.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= n <= 10<sup style="box-sizing: border-box; font-size: 9.75px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">4</sup></code></li></ul><a href="https://leetcode.com/problems/count-number-of-ways-to-place-houses/" target="_blank"> Problem link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://www.youtube.com/shorts/Is39ise3mcA " target="_blank">Youtube</a><br /></li><li><a href="https://b23.tv/QezacZv" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>A classic DP problem (Dynamic Programming)
because it's either ask you a boolean yes or no questions, Or ask for
extreme values, e.g., min, max, or just a number of solutions etc. <br /></div><div><ul style="text-align: left;"><li>Two sides are independent, each side is similar to Climb Stairs, <a href="https://blog.baozitraining.org/2020/06/leetcode-solution-198-house-robber.html">House Robber</a> <a href="https://blog.baozitraining.org/search?q=house+robber">House Robber II</a> </li><li>Given each side is independent, the combo would be multiplied together. <br /></li><li>Implementation wise</li><ul><li>could use two variables to replace the array</li><li>be careful about overflow (use long to cast back to int)<br /></li></ul></ul></div><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3><div><div><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/26b682979054bf5fed8cf2b6f2a52337.js"></script>Time Complexity: O(N) since going through each number till n<br /></div><div>Space Complexity: O(1) if we use two variables to track, the above implementation is O(N) since an extra array is used. <br /></div></div>
<h3>
References</h3>
<ul><li><a href="https://blog.baozitraining.org/2020/06/leetcode-solution-198-house-robber.html">House Robber</a> </li><li><a href="https://blog.baozitraining.org/search?q=house+robber">House Robber II</a> <br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-60088601876522062472022-06-23T13:45:00.005-07:002022-06-24T10:18:03.309-07:00Leetcode solution 2304. Minimum Path Cost in a Grid<br /><h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">You are given a<span> </span><b style="box-sizing: border-box; font-weight: bolder;">0-indexed</b><span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">m x n</code><span> </span>integer matrix<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">grid</code><span> </span>consisting of<span> </span><b style="box-sizing: border-box; font-weight: bolder;">distinct</b><span> </span>integers from<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0</code><span> </span>to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">m * n - 1</code>. You can move in this matrix from a cell to any other cell in the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">next</b><span> </span>row. That is, if you are in cell<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(x, y)</code><span> </span>such that<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x < m - 1</code>, you can move to any of the cells<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(x + 1, 0)</code>,<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(x + 1, 1)</code>, ...,<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(x + 1, n - 1)</code>.<span> </span><b style="box-sizing: border-box; font-weight: bolder;">Note</b><span> </span>that it is not possible to move from cells in the last row.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Each possible move has a cost given by a<span> </span><b style="box-sizing: border-box; font-weight: bolder;">0-indexed</b><span> </span>2D array<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">moveCost</code><span> </span>of size<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(m * n) x n</code>, where<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">moveCost[i][j]</code><span> </span>is the cost of moving from a cell with value<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i</code><span> </span>to a cell in column<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">j</code><span> </span>of the next row. The cost of moving from cells in the last row of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">grid</code><span> </span>can be ignored.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">The cost of a path in<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">grid</code><span> </span>is the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">sum</b><span> </span>of all values of cells visited plus the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">sum</b><span> </span>of costs of all the moves made. Return<span> </span><i style="box-sizing: border-box;">the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">minimum</b><span> </span>cost of a path that starts from any cell in the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">first</b><span> </span>row and ends at any cell in the<span> </span><b style="box-sizing: border-box; font-weight: bolder;">last</b><span> </span>row.</i></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/28/griddrawio-2.png" style="-webkit-text-stroke-width: 0px; background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; height: 281px; letter-spacing: normal; max-width: 100%; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; vertical-align: middle; white-space: normal; widows: 2; width: 301px; word-spacing: 0px;" /><span face="-apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="-webkit-text-stroke-width: 0px; background-color: white; color: #263238; display: inline; float: none; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></span><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 17
<b style="box-sizing: border-box; font-weight: bolder;">Explanation: </b>The path with the minimum possible cost is the path 5 -> 0 -> 1.
- The sum of the values of cells visited is 5 + 0 + 1 = 6.
- The cost of moving from 5 to 0 is 3.
- The cost of moving from 0 to 1 is 8.
So the total cost of the path is 6 + 3 + 8 = 17.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 6
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> The path with the minimum possible cost is the path 2 -> 3.
- The sum of the values of cells visited is 2 + 3 = 5.
- The cost of moving from 2 to 3 is 1.
So the total cost of this path is 5 + 1 = 6.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">m == grid.length</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">n == grid[i].length</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">2 <= m, n <= 50</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">grid</code><span> </span>consists of distinct integers from<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0</code><span> </span>to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">m * n - 1</code>.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">moveCost.length == m * n</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">moveCost[i].length == n</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= moveCost[i][j] <= 100</code></li></ul><a href="https://leetcode.com/problems/minimum-path-cost-in-a-grid/"> Problem link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtube.com/shorts/eV0kLfZuYZE?feature=share" target="_blank">Youtube</a><br /></li><li><a href="https://b23.tv/WdYkirD" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>A classic DP problem (Dynamic Programming) because it's either ask you a boolean yes or no questions, Or ask for extreme values, e.g., min, max<br /></div><div><ul style="text-align: left;"><li>Build up a minCost 2 day array, each array element denotes by far the min cost to reach to that element</li><li>Make sure to initialize the values. </li></ul><p> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVr97j6gx7RQ4Of5AQBXbW9mqOS0q3DvcV-kJgDiHgLNIUyhDcs4ZdUe4WNX7674LY0p0BIvijU8Zj2QnMm6I7olmMqV8j5U4SIE41jT-x7DL9i3PvobkTmB7IA2Cod6FORY9zyz6uTxl9f9TyhGwB5clK7rk8_m3i5m0KwL4W365euJbqvBPAIBsoUw/s925/leetcode-2304.png" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="925" data-original-width="573" height="290" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVr97j6gx7RQ4Of5AQBXbW9mqOS0q3DvcV-kJgDiHgLNIUyhDcs4ZdUe4WNX7674LY0p0BIvijU8Zj2QnMm6I7olmMqV8j5U4SIE41jT-x7DL9i3PvobkTmB7IA2Cod6FORY9zyz6uTxl9f9TyhGwB5clK7rk8_m3i5m0KwL4W365euJbqvBPAIBsoUw/w230-h290/leetcode-2304.png" width="230" /></a></div><p></p><img alt="" height="281" src="https://assets.leetcode.com/uploads/2022/04/28/griddrawio-2.png" style="-webkit-text-stroke-width: 0px; background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, "system-ui", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; height: 281px; letter-spacing: normal; max-width: 100%; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; vertical-align: middle; white-space: normal; widows: 2; width: 301px; word-spacing: 0px;" width="244" /></div><br /><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3><div><div><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/d7b9387179f54ec3f8020bfe44efd5f9.js"></script>Time Complexity: O(N^3) since going through the 2-d array and another nested loop<br /></div><div>Space Complexity: O(N^2) used the 2-d minCost array <br /></div></div>
<h3>
References</h3>
<ul><li>None<br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-72869462340903018822021-02-15T19:34:00.004-08:002021-02-15T20:14:59.680-08:00Leetcode solution 1752. Check if Array Is Sorted and Rotated<p> </p><h3>
Problem Statement </h3><div class="content__u3I1 question-content__JfgR" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 1em 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><div style="box-sizing: border-box;"><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;">Given an array<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">nums</code>, return<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">true</code><i style="box-sizing: border-box;"><span> </span>if the array was originally sorted in non-decreasing order, then rotated<span> </span><b style="box-sizing: border-box; font-weight: bolder;">some</b><span> </span>number of positions (including zero)</i>. Otherwise, return<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">false</code>.</p><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;">There may be<span> </span><b style="box-sizing: border-box; font-weight: bolder;">duplicates</b><span> </span>in the original array.</p><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Note:</b><span> </span>An array<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">A</code><span> </span>rotated by<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">x</code><span> </span>positions results in an array<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">B</code><span> </span>of the same length such that<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">A[i] == B[(i+x) % A.length]</code>, where<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">%</code><span> </span>is the modulo operation.</p><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"> </p><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [3,4,5,1,2]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> true
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> [1,2,3,4,5] is the original sorted array.
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
</pre><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [2,1,3,4]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> false
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> There is no sorted array once rotated that can make nums.
</pre><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 3:</b></p><pre style="background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,2,3]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> true
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> [1,2,3] is the original sorted array.
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
</pre><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 4:</b></p><pre style="background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,1,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> true
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> [1,1,1] is the original sorted array.
You can rotate any number of positions to make nums.
</pre><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 5:</b></p><pre style="background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [2,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> true
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> [1,2] is the original sorted array.
You can rotate the array by x = 5 positions to begin on the element of value 2: [2,1].
</pre><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"> </p><p style="box-sizing: border-box; font-size: inherit; font-weight: 400; margin-bottom: 1em; margin-top: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= nums.length <= 100</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= nums[i] <= 100</code></li></ul></div></div><div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.65); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; position: relative; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><div class="css-q9155n" style="-webkit-box-align: center; align-items: center; box-sizing: border-box; display: flex; padding: 10px 0px;"><div class="css-oqu510" style="-webkit-box-align: center; align-items: center; box-sizing: border-box; display: flex; font-size: 13px; line-height: 20px; padding: 0px 20px 0px 0px; position: relative;"><br class="Apple-interchange-newline" /><br /></div></div></div><a href="https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/"> Problem link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/pXzkBoznIdA">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV15b4y1R7dr/">B站 </a></li></ul>
<h3>
Thought Process</h3><div>Simple problem, just need to find a pattern<br /></div><div><ul style="text-align: left;"><li>If always non-decreasing, true <br /></li><li>If it has one decreasing pivot, the last element needs to be less or equal than the first element <br /></li><li>If it has more than one decreasing pivot, false<br /></li></ul></div><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3><div><div><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/696aa9a0fb9a5da14a4a651822b1c8a9.js"></script>
Time Complexity: O(N) since going through the array once<br /></div><div>Space Complexity: O(1) no extra space is used <br /></div></div>
<h3>
References</h3>
<ul><li>None<br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-66204519079101529952020-08-02T21:53:00.001-07:002020-08-02T21:53:04.592-07:00包子聊天系列:中年大叔程序员疫情之后往哪里跳?<p style="clear: both; margin: 0px; padding: 0px;">直接上视频,建议1.5倍速收听😄<br style="margin: 0px; padding: 0px;" /></p><p style="clear: both; margin: 0px; padding: 0px;"><br style="margin: 0px; padding: 0px;" /></p><ul style="text-align: left;"><li>油管:<span> </span>https://youtu.be/D2JPb76-l78</li><li>B站: <a _href="https://www.bilibili.com/video/BV1Hv411v7UU/" href="https://www.bilibili.com/video/BV1Hv411v7UU/" style="color: #576b95; margin: 0px; padding: 0px; text-decoration: none;">https://www.bilibili.com/video/BV1Hv411v7UU/</a></li></ul><p style="clear: both; margin: 0px; padding: 0px;"><br style="margin: 0px; padding: 0px;" /></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi45Q4a3wRDrEp9Injivc6K4Lg0muKX2-6AFhU4PnXdNEwGM9ieP47KNnQ4f3DdCg1cwdOjvQZZdwJxpWZepL_MaWKfyGovXiXZ7acqwSmdYxhFnQNEf2dcyV6rWuFSx__QYJGnjJm05REC/s640/61440018p4os10344227.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="426" data-original-width="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi45Q4a3wRDrEp9Injivc6K4Lg0muKX2-6AFhU4PnXdNEwGM9ieP47KNnQ4f3DdCg1cwdOjvQZZdwJxpWZepL_MaWKfyGovXiXZ7acqwSmdYxhFnQNEf2dcyV6rWuFSx__QYJGnjJm05REC/s0/61440018p4os10344227.jpg" /></a></div><p style="clear: both; margin: 0px; padding: 0px; text-align: center;"><br /></p><p style="clear: both; margin: 0px; padding: 0px; text-align: center;">【有没有觉得乔杉和习大大有那么一丢丢撞脸呀🤣】<br style="margin: 0px; padding: 0px;" /></p><p style="clear: both; margin: 0px; padding: 0px;"><br style="-webkit-text-stroke-width: 0px; color: #333333; font-family: mp-quote, -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px; orphans: 2; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /><br /></p>Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-22018309811066860422020-07-25T06:00:00.002-07:002020-07-25T06:00:02.180-07:00Leetcode solution 1344. Angle Between Hands of a Clock<h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Given two numbers,<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">hour</code><span> </span>and<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">minutes</code>. Return the smaller angle (in degrees) formed between the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">hour</code><span> </span>and the<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">minute</code><span> </span>hand.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><img alt="" src="https://assets.leetcode.com/uploads/2019/12/26/sample_1_1673.png" style="border-style: none; box-sizing: border-box; height: 225px; max-width: 100%; vertical-align: middle; width: 230px;" /></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> hour = 12, minutes = 30
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 165
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><img alt="" src="https://assets.leetcode.com/uploads/2019/12/26/sample_2_1673.png" style="border-style: none; box-sizing: border-box; height: 225px; max-width: 100%; vertical-align: middle; width: 230px;" /></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> hour = 3, minutes = 30
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 75
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 3:</b></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;"><img alt="" src="https://assets.leetcode.com/uploads/2019/12/26/sample_3_1673.png" style="border-style: none; box-sizing: border-box; height: 225px; max-width: 100%; vertical-align: middle; width: 230px;" /></b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> hour = 3, minutes = 15
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 7.5
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 4:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> hour = 4, minutes = 50
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 155
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 5:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> hour = 12, minutes = 0
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 0
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= hour <= 12</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0 <= minutes <= 59</code></li><li style="box-sizing: border-box;">Answers within <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">10^-5</code> of the actual value will be accepted as correct.</li></ul><a href="https://leetcode.com/problems/angle-between-hands-of-a-clock/"> Problem link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/QTqBWGBC70w">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV1bt4y1X7rJ/">B站 </a></li></ul>
<h3>
Thought Process</h3><div>Purely a math problem. Calculate the clock's hour hand and minute hand separately. <br /></div><div><ul style="text-align: left;"><li>There are 60 minutes in 360 angle, so each minute is 6 degree in angle. </li><li>Angle should be the absolute value of (minute angel - hour angle)</li><li>Final angle should be min(angle, 360 - angle)<br /></li></ul></div><div><br /></div><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3><div><div><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/2e0f17d82bffa7874c4b54bad18f45f7.js"></script>
Time Complexity: O(1) since it's a math problem<br />
Space Complexity: O(1) no extra space is used <br /></div></div>
<h3>
References</h3>
<ul><li><a href="https://drive.google.com/file/d/1nMnZ37-FNtrX-QMVIAUleCVu8LUFUMfO/view?usp=sharing">Leetcode official solution </a><br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-88540398789210865642020-07-17T22:48:00.002-07:002020-07-18T15:21:52.998-07:00Leetcode solution 1512. Number of Good Pairs<h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Given an array of integers <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">nums</code>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">A pair <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">(i,j)</code> is called<span> </span><i style="box-sizing: border-box;">good</i><span> </span>if <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">nums[i]</code><span> </span>==<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">nums[j]</code><span> </span>and<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">i</code><span> </span><<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">j</code>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Return the number of<span> </span><i style="box-sizing: border-box;">good</i><span> </span>pairs.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,2,3,1,1,3]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 4
<b style="box-sizing: border-box; font-weight: bolder;">Explanation: </b>There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,1,1,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 6
<b style="box-sizing: border-box; font-weight: bolder;">Explanation: </b>Each pair in the array are <i style="box-sizing: border-box;">good</i>.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 3:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,2,3]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 0
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= nums.length <= 100</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= nums[i] <= 100</code></li></ul> Problem <a href="https://leetcode.com/problems/number-of-good-pairs/" target="_blank">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/dvnjvOLh88k" target="_blank">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV1hK4y1x7Py/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>Array is the most basic data structures and common solutions inclue</div><div><ul style="text-align: left;"><li>Nested for loops O(N^2)</li><li>Sort O(NlgN)</li><li>Use extra space O(N)</li></ul><div>This applies to this problem perfectly. The O(N^2) brute force solution is naive. We can also use a Map data structure (key is the number, value is the occurrence count) thus O(N). We can also sort the array and use this simple formula (also leetcode's hint) to calculate the good number pair. <br /></div><blockquote><div>Good pairs = N * (N-1) / 2 where N is how many duplicate numbers, this is from combination C(n^2), from n elements pick two<br /></div></blockquote></div><div><br /></div>
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use Map<br /></h4><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/4b315aa0a58f9b2d648605f0a129b60c.js"></script>
Time Complexity: O(N), N is the array size<br />
Space Complexity: O(N) since we use extra Map<br /><div><div><br /></div><div><h4>Sort<br /></h4><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/64be9d75606ea1e8e9ba8c5070523e34.js"></script>
Time Complexity: O(NlgN), N is the array size since we sort<br />
Space Complexity: O(1) no extra space is used <br /></div></div>
<h3>
References</h3>
<ul><li>None<br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-57253211122063721272020-07-04T01:00:00.001-07:002020-07-06T21:10:28.322-07:00System design interview: how to design a simple twitter search system<h2><br /></h2>
<h2 style="text-align: left;">
Methodology: READ MF!</h2>
<span style="font-weight: normal;">[Originally from the Post: <a href="https://blog.baozitraining.org/2020/02/system-design-interview-how-to-design.html">System design interview: how to design a chat system (e.g., Facebook Messenger, WeChat or WhatsApp)</a>]</span><br /><div><div>
<br />
Remind ourselves with the "READ MF!" methodology.<br />
</div><div><div style="text-align: left;"><h2>Requirements <br /></h2><div><p style="text-align: left;">Simple text based search with "AND" or "OR" support. E.g., give me all the tweets that have words "Black" AND "Life" in it sorted by published time in descending order. <br /></p><p style="text-align: left;"><b id="docs-internal-guid-9c0992ae-7fff-e606-2f4e-cca8657d6eef" style="font-weight: normal;"></b></p><p dir="ltr" style="background-color: #1e1e1e; line-height: 1.55455; margin-bottom: 0pt; margin-top: 0pt;"><b id="docs-internal-guid-9c0992ae-7fff-e606-2f4e-cca8657d6eef" style="font-weight: normal;"><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">search</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">(</span><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">api_dev_key</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">,</span><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> search_terms</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">,</span><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> maximum_results_to_return</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">,</span><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> sort</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">,</span><span style="background-color: transparent; color: #d4d4d4; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> page_token</span><span style="background-color: transparent; color: gainsboro; font-family: "consolas", sans-serif; font-size: 10.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">)</span></b></p><br class="Apple-interchange-newline" /><p></p><p style="text-align: left;">A follow up question would be sort those results in other orders, E.g., give me all the tweets that have words "Black" AND "Life" in it sorted by the most commented tweets in descending order.</p></div><h2>Estimation </h2></div><ul style="text-align: left;"><li>Storage: assuming twitter has 1B users, 50% of them post a tweet per day, each tweet is 140 char = 140 Byte. Each day, 1B * 0.5 * 280(roughly 300)Byte = 150GB data</li><li>Write QPS = 1B user * 0.5 post a tweet per day / 86400 = 5787 QPS = 6000 QPS<br /></li><li>Query QPS = assuming 3x than write QPS = 18000 QPS <br /></li></ul><div>Write QPS in general we don't worry too much since it could be processed asynchronously. 18000 QPS for read is easy to handle as long as we have enough number of boxes and most of query would be returned via memory (not hitting disk). <br /></div><div style="text-align: left;"><br /></div>
<h2 style="text-align: left;">
Key designs and terms <br /></h2><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHiCnMeJ9R2GYByW2pl0D04FWNmqugw-MuaQ6GGGcJsLSyQl9jmRQULrAe6h7oasjBWxwfRXEHABhu9BUNdFfYiLh0Jfq0FwnvvoFiRGwhusuecoAOZWoH4GqIx_uRp4zq-RzzZl0tzRiX/s1737/twitter+search.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1525" data-original-width="1737" height="488" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHiCnMeJ9R2GYByW2pl0D04FWNmqugw-MuaQ6GGGcJsLSyQl9jmRQULrAe6h7oasjBWxwfRXEHABhu9BUNdFfYiLh0Jfq0FwnvvoFiRGwhusuecoAOZWoH4GqIx_uRp4zq-RzzZl0tzRiX/w625-h549/twitter+search.png" width="554" /></a></div><div style="text-align: left;"><br /></div></div>You can also divided the system as usual into three major layers, presentation, service and data layer.<br /></div><div><div>
<br />
A few key terminologies. <br /><ul style="text-align: left;"><li><a href="https://lucene.apache.org/core/8_5_2/index.html" target="_blank">Lucene</a> <br /></li><li>
<a href="https://www.geeksforgeeks.org/inverted-index/#:~:text=An%20inverted%20index%20is%20an,document%20or%20a%20web%20page." target="_blank">Inverted Index</a></li><li><a href="https://www.slideshare.net/SmartCat-io/elasticsearch-under-the-hood?from_action=save" target="_blank">Elastic Search Under the Hood,</a> <a href="https://www.elastic.co/blog/found-elasticsearch-from-the-bottom-up" target="_blank">Elastic Search 101</a><br /></li><li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" target="_blank">Elastic Search API</a></li><li><a href="https://www.elastic.co/webinars/introduction-elk-stack" target="_blank">ELK</a> <br /></li></ul><p style="text-align: left;">If the follow up question, we just need to build another index given the sort key or we could fetch the results on flight and sort (local optimal). Follow "Index -> Search -> Rank" steps. <br /></p><h3>
Baozi Youtube Video</h3>
<ul><li><a href="https://youtu.be/c8e66QFjik8" target="_blank">Youtube Video Link</a></li><li><a href="https://www.bilibili.com/video/BV1TC4y1Y7hn/" target="_blank">B站 </a></li></ul>
<br />
<h3>
References (Credits to original authors)</h3>
<ul><li><a href="https://drive.google.com/file/d/1CHekcwmwCv1LVLznPwa5HUxvD-aqle0o/view?usp=sharing" target="_blank">Grokking the system design interviews</a></li><li><a href="https://drive.google.com/file/d/1Fzy0AKmR2zHpzwgYqsTmN-sxeLjywnfZ/view?usp=sharing" target="_blank">Elastic Search Under the Hood GDrive</a></li><li><a href="https://www.slideshare.net/SmartCat-io/elasticsearch-under-the-hood?from_action=save" target="_blank">Elastic Search Under the Hood source</a> <br /></li></ul>
</div></div>Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com1tag:blogger.com,1999:blog-1402726418106920478.post-45571066206390178632020-06-27T06:00:00.010-07:002020-06-27T06:00:00.541-07:00Leetcode solution 213. House Robber II<h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are<span> </span><b style="box-sizing: border-box; font-weight: bolder;">arranged in a circle.</b><span> </span>That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and <b style="box-sizing: border-box; font-weight: bolder;">it will automatically contact the police if two adjacent houses were broken into on the same night</b>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight<span> </span><b style="box-sizing: border-box; font-weight: bolder;">without alerting the police</b>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> [2,3,2]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 3
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> [1,2,3,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 4
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.</pre> Problem <a href="https://leetcode.com/problems/house-robber-ii/" target="_blank">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/FZq3YHzvUa4" target="_blank">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV1k54y1B7mD/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>This is very similar to <a href="http://blog.baozitraining.org/2020/06/leetcode-solution-198-house-robber.html" target="_blank">House Robber I problem</a> where the only difference is the houses now form a circle (French fancy way calls it cul-de-sac). It's same DP algorithm except now we need to consider two cases: whether we rob the first house or not. If we rob the first house, we should not rob the last house. If we do not rob the first house, we can rob the last house. We can even reuse the rob() function in <a href="http://blog.baozitraining.org/2020/06/leetcode-solution-198-house-robber.html" target="_blank">House Robber I problem</a></div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiICWyKLsdm7FL3EYMwZkP53dwUEE6YGv7qrG4FT4QGEwiitpr1CdmM29ti5gK8QYI30yn-9GX57LVhVZNyp8L9QLWQVtvFrOlf035OKeAy4n1u9WeS0K3o8Q15KdJMH3n3kIc_UCMRkbK/s1280/cul-de-sac.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="960" data-original-width="1280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiICWyKLsdm7FL3EYMwZkP53dwUEE6YGv7qrG4FT4QGEwiitpr1CdmM29ti5gK8QYI30yn-9GX57LVhVZNyp8L9QLWQVtvFrOlf035OKeAy4n1u9WeS0K3o8Q15KdJMH3n3kIc_UCMRkbK/s320/cul-de-sac.jpg" width="320" /></a></div><div><br /></div><div><br /></div>
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use DP</h4><div><br /></div>
<script src="https://gist.github.com/shixiaoyu/c664335d2725f679ca89077908dcec48.js"></script>
Time Complexity: O(N), N is the array size<br />
Space Complexity: O(N) since we use extra arrays
<div><div><br /><br /></div></div>
<h3>
References</h3>
<ul><li><a href="http://blog.baozitraining.org/2020/06/leetcode-solution-198-house-robber.html" target="_blank">House Robber I problem solution <br /></a></li><li><a href="https://leetcode.com/problems/house-robber-ii/discuss/59934/Simple-AC-solution-in-Java-in-O(n)-with-explanation" target="_blank">Leetcode discussion solution</a></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-78203017619126532512020-06-20T06:00:00.013-07:002020-06-20T06:00:01.886-07:00Leetcode solution 198. House Robber<h3>Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and<span> </span><b style="box-sizing: border-box; font-weight: bolder;">it will automatically contact the police if two adjacent houses were broken into on the same night</b>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight<span> </span><b style="box-sizing: border-box; font-weight: bolder;">without alerting the police</b>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [1,2,3,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 4
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> nums = [2,7,9,3,1]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 12
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0 <= nums.length <= 100</code></li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0 <= nums[i] <= 400</code></li></ul> Problem <a href="https://leetcode.com/problems/house-robber/" target="_blank">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/QKuUYdbn9hM" target="_blank">Youtube</a><br /></li><li><a href="https://www.bilibili.com/video/BV1ek4y1z7pT/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>Easy problem and Dynamic Programming(DP) should jump into mind given it's only asking for max values (Just think about different combo we have to do without using DP, a little less than 2^N)</div><div><br /></div><div>The mathematical induction formula is below, for any current max money at index i, you either choose to use the i-1 or i-2 + current house's money to not trigger police. <br /></div><div><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "menlo"; font-size: 9pt;"><span style="color: grey;">max[i] = max(max[i - 2] + a[i], max[i-1])</span></pre></div>
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>DP<br /></h4>
<script src="https://gist.github.com/shixiaoyu/72ded54dd04f50e12b7359d1183ef7c8.js"></script>
Time Complexity: O(N) N is the array size<br />
Space Complexity: O(N) since we used an extra array<br />
<br />
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h3>
References</h3>
<ul><li><a href="https://drive.google.com/file/d/1oBaOOV9pEdnom7--A1JU9QZ8EH8PBKhM/view?usp=sharing" target="_blank">Leetcode official solution</a></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-69743918887845985832020-06-13T06:00:00.027-07:002020-06-13T06:00:01.226-07:00Leetcode solution 200. Number of Islands (Using BFS, DFS & Disjoint Set)<h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given a 2d grid map of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">'1'</code>s (land) and<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">'0'</code>s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b>
11110
11010
11000
00000
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 1
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b>
11000
11000
00100
00011
<b style="box-sizing: border-box; font-weight: bolder;">Output: </b>3</pre> Problem <a href="https://leetcode.com/problems/number-of-islands/" target="_blank">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/dA1FYFlvckY" target="_blank">Youtube BFS & DFS solution in 2015</a><br /></li><li><a href="https://youtu.be/E5_ivrrLVFQ" target="_blank">Youtube Disjoint Solution </a><br /></li><li><a href="https://www.bilibili.com/video/BV1oz4y1X71Y/" target="_blank">B站 </a></li></ul>
<h3>
Thought Process</h3><div>I have recorded a video back in 2015 (5 years ago, Geez time flies) using BFS and DFS. The essential idea is every time we see a land (i.e., "1"), we would trigger a BFS or DFS search to mark all the connected grids and increase the island counts to 1. <br /></div><div><br /></div><div>Another solution is to use Disjoint Set (aka Union Find). There is a generic solution where we could implement a DisjointSet data structure and perform "union" steps first to assign each grid a "parent". In the 2nd iteration we can perform "find" step to count how many distinct "parents", which is the number of islands. <br /></div><div><br /></div><div>A quick summary of some classic use cases</div><div><ul style="text-align: left;"><li>Disjoint Set / Union Find (Disjoint set is the data structure while union find is the algorithm, people normally use it interchangeably) can often be used to solve if a graph is connected or not, such as phone contact problem (who knows who, how many isolated groups are there) <br /></li><li>To detect if a directed graph has cycle, <a href="https://www.geeksforgeeks.org/detect-cycle-in-a-directed-graph-using-bfs/?ref=rp" target="_blank">use Kahn's algorithm for topological sort</a></li><li>To detect if a un-directed graph has cycle</li><ul><li>We could use normal <a href="https://www.geeksforgeeks.org/detect-cycle-in-an-undirected-graph-using-bfs/?ref=rp" target="_blank">BFS </a>or <a href="https://www.geeksforgeeks.org/detect-cycle-undirected-graph/?ref=rp" target="_blank">DFS</a> <br /></li><li>We should use Union Find with edge list graph representation <br /></li></ul></ul></div><div><br /></div>
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use BFS<br /></h4>
<script src="https://gist.github.com/shixiaoyu/fe8cda2173428a6df63121f7c9b30464.js"></script>
Time Complexity: O(M*N), where M and N is row and col of grid matrix. Each grid is visited once<br />
Space Complexity: O(M*N) since we have to track the visited grids<br />
<br />
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use DFS<br /></h4>
<script src="https://gist.github.com/shixiaoyu/1882de7f135cc86ce1677137efb57c31.js"></script>
Time Complexity: O(M*N), where M and N is row and col of grid matrix. Each grid is visited once<br />
Space Complexity: O(M*N) since we have to track the visited grids<div><br /></div><div><h4>Use Disjoint Set / Union Find<br /></h4>
<script src="https://gist.github.com/shixiaoyu/834d2fb48160eb8e8e80adfdddfc1ec4.js"></script>
Time Complexity: O(M*N), where M and N is row and col of grid matrix. Each grid is visited once<br />
Space Complexity: O(M*N) since we have to track the visited grids using a map<br /><div><br /><br /></div></div>
<h3>
References</h3>
<ul><li><a href="https://drive.google.com/open?id=1OpI9Ru14gFRd0xaM0f30H6w1ogUSQ657" target="_blank">Leetcode official solution</a></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-17141045547313958582020-06-06T06:00:00.007-07:002020-06-06T06:00:08.990-07:00Leetcode solution 207. Course Schedule<h3>
Problem Statement </h3><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">There are a total of<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">numCourses</code><span> </span>courses you have to take, labeled from<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">0</code><span> </span>to<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">numCourses-1</code>.</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:<span> </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">[0,1]</code></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given the total number of courses and a list of prerequisite<span> </span><b style="box-sizing: border-box; font-weight: bolder;">pairs</b>, is it possible for you to finish all courses?</p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> numCourses = 2, prerequisites = [[1,0]]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> true
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></p><pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250) none repeat scroll 0% 0%; border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> numCourses = 2, prerequisites = [[1,0],[0,1]]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> false
<b style="box-sizing: border-box; font-weight: bolder;">Explanation:</b> There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
</pre><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Constraints:</b></p><ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><li style="box-sizing: border-box;">The input prerequisites is a graph represented by<span> </span><b style="box-sizing: border-box; font-weight: bolder;">a list of edges</b>, not adjacency matrices. Read more about<span> </span><a href="https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: currentcolor none medium; padding-bottom: 1px; pointer-events: auto; text-decoration: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;" target="_blank">how a graph is represented</a>.</li><li style="box-sizing: border-box;">You may assume that there are no duplicate edges in the input prerequisites.</li><li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">1 <= numCourses <= 10^5</code></li></ul> Problem <a href="https://leetcode.com/problems/course-schedule/" target="_blank">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/4vaaXaqNvek" target="_blank">Youtube</a></li><li><a href="https://youtu.be/pCvUGQ4l1-8" target="_blank">Youtube BFS in 2015</a>, <a href="https://youtu.be/Vuuy4KEFc2c" target="_blank">Youtube DFS in 2015</a><br /></li><li><a href="https://www.bilibili.com/video/BV1hp4y1Q7cz/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3>It is a classic dependency graph problem. We can translate this problem to direct if there is a cycle in a directed graph or not. A text book solution is <a href="https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/" target="_blank">Kahn's algorithm for topological sorting</a>. We can have a simple way to represent the graph or use a more proper adjacency lists (a little bit overkill for this problem though)<br />
<br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use adjacency lists BFS<br /></h4>
<script src="https://gist.github.com/shixiaoyu/63a2e71566126a3dd43d49b2021f4347.js"></script>
Time Complexity: O(V), since each vertex is visited only once during BFS<br />
Space Complexity: O(V+E) since we use adjacency lists to represent a directed graph<br />
<br />
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Use simple hashmap BFS<br /></h4>
<script src="https://gist.github.com/shixiaoyu/8b1e577b34a6de49325ba77b56708738.js"></script>
Time Complexity: O(V), since each vertex is visited only once during BFS<br /><div>
Space Complexity: O(V) since we are using a hashmap</div><div><br /></div><div><h4>Use recursion DFS<br /></h4>
<script src="https://gist.github.com/shixiaoyu/03d0778daf4ad7f21deaffcad3ecb694.js"></script>
Time Complexity: O(V), since each vertex is visited only once during BFS<br /><div>
Space Complexity: O(V) since we used a lookup hashmap for memorization purpose (not considering function stack space)<br /></div><div><br /><br /></div></div>
<h3>
References</h3>
<ul><li><a href="https://drive.google.com/file/d/10ZwaF62BQJxw63ERoaeRZ2R2NfOnDM_4/view?usp=sharing" target="_blank">Leetcode official solution</a></li><li><a href="https://www.geeksforgeeks.org/detect-cycle-in-a-directed-graph-using-bfs/" target="_blank">GeeksforGeeks Detect Cycle in a directly graph using topological sort</a><br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-31886776185990123662020-05-30T06:00:00.025-07:002020-05-30T06:00:00.324-07:00Google Code Jam 2020 Qualification Round: Parenting Partnering Returns Solution<h3>
Problem Statement <br /></h3><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Problem</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Cameron and Jamie's kid is almost 3 years old! However, even though the child is more independent now, scheduling kid activities and domestic necessities is still a challenge for the couple.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Cameron and Jamie have a list of<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>activities to take care of during the day. Each activity happens during a specified interval during the day. They need to assign each activity to one of them, so that neither of them is responsible for two activities that overlap. An activity that ends at time t is not considered to overlap with another activity that starts at time t.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For example, suppose that Jamie and Cameron need to cover 3 activities: one running from 18:00 to 20:00, another from 19:00 to 21:00 and another from 22:00 to 23:00. One possibility would be for Jamie to cover the activity running from 19:00 to 21:00, with Cameron covering the other two. Another valid schedule would be for Cameron to cover the activity from 18:00 to 20:00 and Jamie to cover the other two. Notice that the first two activities overlap in the time between 19:00 and 20:00, so it is impossible to assign both of those activities to the same partner.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given the starting and ending times of each activity, find any schedule that does not require the same person to cover overlapping activities, or say that it is impossible.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Input</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">The first line of the input gives the number of test cases,<span> </span><b style="outline: currentcolor none 0px;">T</b>.<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>test cases follow. Each test case starts with a line containing a single integer<span> </span><b style="outline: currentcolor none 0px;">N</b>, the number of activities to assign. Then,<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>more lines follow. The i-th of these lines (counting starting from 1) contains two integers<span> </span><b style="outline: currentcolor none 0px;">S<sub style="outline: currentcolor none 0px;">i</sub></b><span> </span>and<span> </span><b style="outline: currentcolor none 0px;">E<sub style="outline: currentcolor none 0px;">i</sub></b>. The i-th activity starts exactly<span> </span><b style="outline: currentcolor none 0px;">S<sub style="outline: currentcolor none 0px;">i</sub></b><span> </span>minutes after midnight and ends exactly<span> </span><b style="outline: currentcolor none 0px;">E<sub style="outline: currentcolor none 0px;">i</sub></b><span> </span>minutes after midnight.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Output</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For each test case, output one line containing<span> </span><code style="outline: currentcolor none 0px;">Case #x: y</code>, where<span> </span><code style="outline: currentcolor none 0px;">x</code><span> </span>is the test case number (starting from 1) and<span> </span><code style="outline: currentcolor none 0px;">y</code><span> </span>is<span> </span><code style="outline: currentcolor none 0px;">IMPOSSIBLE</code><span> </span>if there is no valid schedule according to the above rules, or a string of exactly<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>characters otherwise. The i-th character in<span> </span><code style="outline: currentcolor none 0px;">y</code><span> </span>must be<span> </span><code style="outline: currentcolor none 0px;">C</code><span> </span>if the i-th activity is assigned to Cameron in your proposed schedule, and<span> </span><code style="outline: currentcolor none 0px;">J</code><span> </span>if it is assigned to Jamie.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">If there are multiple solutions, you may output any one of them. (See "What if a test case has multiple correct solutions?" in the Competing section of the<span> </span><a href="https://codingcompetitions.withgoogle.com/codejam/faq#competing" style="color: #1967d2; outline: currentcolor none 0px; text-decoration: none; transition: color 0.15s ease 0s;">FAQ</a>. This information about multiple solutions will not be explicitly stated in the remainder of the 2020 contest.)</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Limits</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Time limit: 20 seconds per test set.<br style="outline: currentcolor none 0px;" />Memory limit: 1GB.<br style="outline: currentcolor none 0px;" />1 ≤<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>≤ 100.<br style="outline: currentcolor none 0px;" />0 ≤<span> </span><b style="outline: currentcolor none 0px;">S<sub style="outline: currentcolor none 0px;">i</sub></b><span> </span><<span> </span><b style="outline: currentcolor none 0px;">E<sub style="outline: currentcolor none 0px;">i</sub></b><span> </span>≤ 24 × 60.<br style="outline: currentcolor none 0px;" /></p><h4 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Test set 1 (Visible Verdict)</h4><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">2 ≤<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>≤ 10.<br style="outline: currentcolor none 0px;" /></p><h4 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Test set 2 (Visible Verdict)</h4><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">2 ≤<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>≤ 1000.<br style="outline: currentcolor none 0px;" /></p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Sample</h3><div class="problem-io-wrapper" style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; outline: currentcolor none 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><table style="border-spacing: 2rem 1rem; outline: currentcolor none 0px;"><tbody><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Input</span><br style="outline: currentcolor none 0px;" /> </td><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Output</span><br style="outline: currentcolor none 0px;" /> </td></tr><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">4
3
360 480
420 540
600 660
3
0 1440
1 3
2 4
5
99 150
1 100
100 301
2 5
150 250
2
0 720
720 1440
</pre></td><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: JCCJJ
Case #4: CC
</pre></td></tr></tbody></table></div><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Sample Case #1 is the one described in the problem statement. As mentioned above, there are other valid solutions, like<span> </span><code style="outline: currentcolor none 0px;">JCJ</code><span> </span>and<span> </span><code style="outline: currentcolor none 0px;">JCC</code>.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #2, all three activities overlap with each other. Assigning them all would mean someone would end up with at least two overlapping activities, so there is no valid schedule.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #3, notice that Cameron ends an activity and starts another one at minute 100.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #4, any schedule would be valid. Specifically, it is OK for one partner to do all activities.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></p><div>
Problem <a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020bdf9" target="_blank">link</a> <br /></div><div>
</div><h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/ZdshYXDldO8" target="_blank">Youtube</a></li><li><a href="https://www.bilibili.com/video/BV1aT4y1u7BU/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>If you remember the leetcode "merge interval" question, this is quite similar. <a href="https://youtu.be/OIqAVmD1RGk" target="_blank">Baozi Training "Merge Interval"</a></div><div>Model the time range in internal, sort the intervals in ascending order based on the start time, then schedule either "C" or "J" as long as they are available. Update the end time with the assigned task. <br /></div><div>The only caveat is we have to remember the original task index for output purpose because we sort the interval using the start time. <br /></div><div><br /></div><div>An alternate solution is a "bipartite graph coloring" problem. In essence, model the interval into an <a href="https://en.wikipedia.org/wiki/Interval_graph" target="_blank">interval graph</a>. Each interval is a vertex in the graph, and if it overlaps with another interval, there would be an edge. Now the problem becomes starting with one node, if we could color the nodes in two colors without any conflicts. Note the graph doesn't need to be fully connected, a simple BFS with a visited hash map should do the trick. <a href="https://www.geeksforgeeks.org/bipartite-graph/">https://www.geeksforgeeks.org/bipartite-graph/</a></div><div>The time complexity of "bipartite graph coloring" is O(V+E)</div><div><br /></div><div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Interval_graph.svg/300px-Interval_graph.svg.png" /> <br /></div><h3><br /></h3><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Greedy solution </h4>
<script src="https://gist.github.com/shixiaoyu/a4cfbabae9044e686cf41317fdd7e48c.js"></script>
Time Complexity: O(lgN) since we sort<br />
Space Complexity: O(N) since we used extra list for the intervals <br />
<br />
<h3>
References</h3>
<ul><li><a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020bdf9" target="_blank">Google Code Jam official solution</a></li><li><a href="https://www.geeksforgeeks.org/bipartite-graph/" target="_blank">GeeksForGeeks check if a given graph is Bipartite or not</a><br /></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-30191264649564181462020-05-23T06:00:00.015-07:002020-05-23T06:00:01.993-07:00Google Code Jam 2020 Qualification Round: Nesting Depth Solution<h3>
Problem Statement <br /></h3><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Problem</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">tl;dr: Given a string of digits<span> </span><b style="outline: currentcolor none 0px;">S</b>, insert a minimum number of opening and closing parentheses into it such that the resulting string is balanced and each digit d is inside exactly d pairs of matching parentheses.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Let the<span> </span><i style="outline: currentcolor none 0px;">nesting</i><span> </span>of two parentheses within a string be the substring that occurs strictly between them. An opening parenthesis and a closing parenthesis that is further to its right are said to<span> </span><i style="outline: currentcolor none 0px;">match</i><span> </span>if their nesting is empty, or if every parenthesis in their nesting matches with another parenthesis in their nesting. The<span> </span><i style="outline: currentcolor none 0px;">nesting depth</i><span> </span>of a position p is the number of pairs of matching parentheses m such that p is included in the nesting of m.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For example, in the following strings, all digits match their nesting depth:<span> </span><code style="outline: currentcolor none 0px;">0((2)1)</code>,<span> </span><code style="outline: currentcolor none 0px;">(((3))1(2))</code>,<span> </span><code style="outline: currentcolor none 0px;">((((4))))</code>,<span> </span><code style="outline: currentcolor none 0px;">((2))((2))(1)</code>. The first three strings have minimum length among those that have the same digits in the same order, but the last one does not since<span> </span><code style="outline: currentcolor none 0px;">((22)1)</code><span> </span>also has the digits<span> </span><code style="outline: currentcolor none 0px;">221</code><span> </span>and is shorter.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given a string of digits<span> </span><b style="outline: currentcolor none 0px;">S</b>, find another string S', comprised of parentheses and digits, such that:</p><ul style="list-style-position: outside; list-style-type: disc; margin: 0px; outline: currentcolor none 0px; padding: 1rem 0px 0px 1rem;"><li style="outline: currentcolor none 0px;">all parentheses in S' match some other parenthesis,</li><li style="outline: currentcolor none 0px;">removing any and all parentheses from S' results in<span> </span><b style="outline: currentcolor none 0px;">S</b>,</li><li style="outline: currentcolor none 0px;">each digit in S' is equal to its nesting depth, and</li><li style="outline: currentcolor none 0px;">S' is of minimum length.</li></ul><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Input</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">The first line of the input gives the number of test cases,<span> </span><b style="outline: currentcolor none 0px;">T</b>.<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>lines follow. Each line represents a test case and contains only the string<span> </span><b style="outline: currentcolor none 0px;">S</b>.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Output</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For each test case, output one line containing<span> </span><code style="outline: currentcolor none 0px;">Case #x: y</code>, where<span> </span><code style="outline: currentcolor none 0px;">x</code><span> </span>is the test case number (starting from 1) and<span> </span><code style="outline: currentcolor none 0px;">y</code><span> </span>is the string S' defined above.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Limits</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Time limit: 20 seconds per test set.<br style="outline: currentcolor none 0px;" />Memory limit: 1GB.<br style="outline: currentcolor none 0px;" />1 ≤<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>≤ 100.<br style="outline: currentcolor none 0px;" />1 ≤ length of<span> </span><b style="outline: currentcolor none 0px;">S</b><span> </span>≤ 100.<br style="outline: currentcolor none 0px;" /></p><h4 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Test set 1 (Visible Verdict)</h4><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Each character in<span> </span><b style="outline: currentcolor none 0px;">S</b><span> </span>is either<span> </span><code style="outline: currentcolor none 0px;">0</code><span> </span>or<span> </span><code style="outline: currentcolor none 0px;">1</code>.<br style="outline: currentcolor none 0px;" /></p><h4 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Test set 2 (Visible Verdict)</h4><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Each character in<span> </span><b style="outline: currentcolor none 0px;">S</b><span> </span>is a decimal digit between<span> </span><code style="outline: currentcolor none 0px;">0</code><span> </span>and<span> </span><code style="outline: currentcolor none 0px;">9</code>, inclusive.<br style="outline: currentcolor none 0px;" /></p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Sample</h3><div class="problem-io-wrapper" style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; outline: currentcolor none 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><table style="border-spacing: 2rem 1rem; outline: currentcolor none 0px;"><tbody><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Input</span><br style="outline: currentcolor none 0px;" /> </td><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Output</span><br style="outline: currentcolor none 0px;" /> </td></tr><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">4
0000
101
111000
1
</pre></td><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">Case #1: 0000
Case #2: (1)0(1)
Case #3: (111)000
Case #4: (1)
</pre></td></tr></tbody></table></div><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">The strings<span> </span><code style="outline: currentcolor none 0px;">()0000()</code>,<span> </span><code style="outline: currentcolor none 0px;">(1)0(((()))1)</code><span> </span>and<span> </span><code style="outline: currentcolor none 0px;">(1)(11)000</code><span> </span>are not valid solutions to Sample Cases #1, #2 and #3, respectively, only because they are not of minimum length. In addition,<span> </span><code style="outline: currentcolor none 0px;">1)(</code><span> </span>and<span> </span><code style="outline: currentcolor none 0px;">)(1</code><span> </span>are not valid solutions to Sample Case #4 because they contain unmatched parentheses and the nesting depth is 0 at the position where there is a 1.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">You can create sample inputs that are valid only for Test Set 2 by removing the parentheses from the example strings mentioned in the problem statement.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></p><div>
Problem <a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/0000000000209a9f" target="_blank">link</a> <br /></div><div>
</div><h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul><li><a href="https://youtu.be/JqNCQlnvVRQ" target="_blank">Youtube</a></li><li><a href="https://www.bilibili.com/video/BV1uz411B7eH/" target="_blank">B站</a> </li></ul>
<h3>
Thought Process</h3><div>Simple simulation. Key points <br /></div><div><ul style="text-align: left;"><li>An integer could be used for depth to record how many parentheses we need to append. (I used a stack in the code during the competition) <br /></li><li>We could use a dummy node as the depth is 0 instead of starting from index 1. <br /></li></ul></div>
<h3><br /></h3><h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Simulation solution </h4>
<script src="https://gist.github.com/shixiaoyu/ff37ec57588c2c7b496019730d8e2e34.js"></script>
Time Complexity: O(N) <br />
Space Complexity: O(N) since we used extra extra space to store the output <br />
<br />
<h3>
References</h3>
<ul><li><a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/0000000000209a9f" target="_blank">Google Code Jam official solution</a></li></ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-11470856306479890662020-05-16T06:00:00.015-07:002020-05-16T06:00:00.575-07:00Google Code Jam 2020 Qualification Round: Vestigium Solution<h3>
Problem Statement <br /></h3><font size="4">Problem</font><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">The<span> </span><i style="outline: currentcolor none 0px;">trace</i><span> </span>of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">An<span> </span><b style="outline: currentcolor none 0px;">N</b>-by-<b style="outline: currentcolor none 0px;">N</b><span> </span>square matrix is a<span> </span><i style="outline: currentcolor none 0px;">Latin square</i><span> </span>if each cell contains one of<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>values are the integers between 1 and<span> </span><b style="outline: currentcolor none 0px;">N</b>.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Given a matrix that contains only integers between 1 and<span> </span><b style="outline: currentcolor none 0px;">N</b>, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Input</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">The first line of the input gives the number of test cases,<span> </span><b style="outline: currentcolor none 0px;">T</b>.<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>test cases follow. Each starts with a line containing a single integer<span> </span><b style="outline: currentcolor none 0px;">N</b>: the size of the matrix to explore. Then,<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>lines follow. The i-th of these lines contains<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>integers<span> </span><b style="outline: currentcolor none 0px;">M<sub style="outline: currentcolor none 0px;">i,1</sub></b>,<span> </span><b style="outline: currentcolor none 0px;">M<sub style="outline: currentcolor none 0px;">i,2</sub></b><span> </span>...,<span> </span><b style="outline: currentcolor none 0px;">M<sub style="outline: currentcolor none 0px;">i,N</sub></b>.<span> </span><b style="outline: currentcolor none 0px;">M<sub style="outline: currentcolor none 0px;">i,j</sub></b><span> </span>is the integer in the i-th row and j-th column of the matrix.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Output</h3><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For each test case, output one line containing<span> </span><code style="outline: currentcolor none 0px;">Case #x: k r c</code>, where<span> </span><code style="outline: currentcolor none 0px;">x</code><span> </span>is the test case number (starting from 1),<span> </span><code style="outline: currentcolor none 0px;">k</code><span> </span>is the trace of the matrix,<span> </span><code style="outline: currentcolor none 0px;">r</code><span> </span>is the number of rows of the matrix that contain repeated elements, and<span> </span><code style="outline: currentcolor none 0px;">c</code><span> </span>is the number of columns of the matrix that contain repeated elements.</p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Limits</h3><h4 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Test set 1 (Visible Verdict)</h4><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Time limit: 20 seconds per test set.<br style="outline: currentcolor none 0px;" />Memory limit: 1GB.<br style="outline: currentcolor none 0px;" />1 ≤<span> </span><b style="outline: currentcolor none 0px;">T</b><span> </span>≤ 100.<br style="outline: currentcolor none 0px;" />2 ≤<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>≤ 100.<br style="outline: currentcolor none 0px;" />1 ≤<span> </span><b style="outline: currentcolor none 0px;">M<sub style="outline: currentcolor none 0px;">i,j</sub></b><span> </span>≤<span> </span><b style="outline: currentcolor none 0px;">N</b>, for all i, j.<br style="outline: currentcolor none 0px;" /></p><h3 style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "google sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Sample</h3><div class="problem-io-wrapper" style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; outline: currentcolor none 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><table style="border-spacing: 2rem 1rem; outline: currentcolor none 0px;"><tbody><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Input</span><br style="outline: currentcolor none 0px;" /> </td><td style="outline: currentcolor none 0px;"><br style="outline: currentcolor none 0px;" /><span class="io-table-header" style="outline: currentcolor none 0px;">Output</span><br style="outline: currentcolor none 0px;" /> </td></tr><tr style="outline: currentcolor none 0px;"><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3
</pre></td><td style="outline: currentcolor none 0px;"><pre class="io-content" style="outline: currentcolor none 0px;">Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2
</pre></td></tr></tbody></table></div><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through<span> </span><b style="outline: currentcolor none 0px;">N</b><span> </span>may be absent from the input.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">In Sample Case #3, the leftmost and rightmost columns have repeated elements.</p><p style="-webkit-text-stroke-width: 0px; color: #202124; font-family: "roboto"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; outline: currentcolor none 0px; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></p><div>
Problem <a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020993c" target="_blank">link</a> <br /></div><div>
</div><h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul>
<li><a href="https://youtu.be/oipVOvqkFVc" target="_blank">Youtube</a></li>
<li><a href="https://www.bilibili.com/video/BV16z4y1R7k6/" target="_blank">B站</a> </li>
</ul>
<h3>
</h3>
<h3>
Thought Process</h3><div>The first problem in those Competitive Programming (CP) is generally easy. Just a simple simulation to count the sum of diagonal, and keep two sets to track duplication for row and col. This could also be a good 10 mins interview warm up questions just to see if candidate knows how to write code or not.<br /></div><br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>Simulation solution </h4>
<script src="https://gist.github.com/shixiaoyu/d607a8cdb9094ea470d98b5f6ef33b61.js"></script>
Time Complexity: O(N^2) <br />
Space Complexity: O(N) since we used extra sets<br />
<br />
<h3>
References</h3>
<ul>
<li><a href="https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020993c" target="_blank">Google Code Jam official solution</a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0San Francisco, CA, USA37.7749295 -122.41941559.4646956638211535 -157.5756655 66.085163336178852 -87.2631655tag:blogger.com,1999:blog-1402726418106920478.post-86934358242907032402020-05-02T06:00:00.000-07:002020-05-02T06:00:10.369-07:00Google Kickstart 2019 Round A: Training Solution<h3>
Problem Statement </h3>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Problem</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
As the football coach at your local school, you have been tasked with picking a team of exactly <b style="outline: 0px !important;">P</b> students to represent your school. There are <b style="outline: 0px !important;">N</b> students for you to pick from. The i-th student has a <i style="outline: 0px !important;">skill rating</i> <b style="outline: 0px !important;">S<sub style="outline: 0px !important;">i</sub></b>, which is a positive integer indicating how skilled they are.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
You have decided that a team is <i style="outline: 0px !important;">fair</i> if it has exactly <b style="outline: 0px !important;">P</b> students on it and they all have the same skill rating. That way, everyone plays as a team. Initially, it might not be possible to pick a fair team, so you will give some of the students one-on-one coaching. It takes one hour of coaching to increase the skill rating of any student by 1.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
The competition season is starting very soon (in fact, the first match has already started!), so you'd like to find the minimum number of hours of coaching you need to give before you are able to pick a fair team.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Input</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
The first line of the input gives the number of test cases, <b style="outline: 0px !important;">T</b>. <b style="outline: 0px !important;">T</b> test cases follow. Each test case starts with a line containing the two integers <b style="outline: 0px !important;">N</b> and <b style="outline: 0px !important;">P</b>, the number of students and the number of students you need to pick, respectively. Then, another line follows containing <b style="outline: 0px !important;">N</b> integers <b style="outline: 0px !important;">S<sub style="outline: 0px !important;">i</sub></b>; the i-th of these is the skill of the i-th student.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Output</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
For each test case, output one line containing <code style="outline: 0px !important;">Case #x: y</code>, where <code style="outline: 0px !important;">x</code> is the test case number (starting from 1) and <code style="outline: 0px !important;">y</code> is the minimum number of hours of coaching needed, before you can pick a fair team of <b style="outline: 0px !important;">P</b> students.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Limits</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Time limit: 15 seconds per test set.<br />
Memory limit: 1 GB.<br />
1 ≤ <b style="outline: 0px !important;">T</b> ≤ 100.<br />
1 ≤ <b style="outline: 0px !important;">S<sub style="outline: 0px !important;">i</sub></b> ≤ 10000, for all i.<br />
2 ≤ <b style="outline: 0px !important;">P</b> ≤ <b style="outline: 0px !important;">N</b>.</div>
<h4 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Test set 1 (Visible)</h4>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
2 ≤ <b style="outline: 0px !important;">N</b> ≤ 1000.</div>
<h4 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Test set 2 (Hidden)</h4>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
2 ≤ <b style="outline: 0px !important;">N</b> ≤ 10<sup style="outline: 0px !important;">5</sup>.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Sample</h3>
<div class="problem-io-wrapper" style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<table style="border-spacing: 2rem 1rem; outline: 0px !important;"><tbody>
<tr style="outline: 0px !important;"><td style="outline: 0px !important;"><br style="outline: 0px !important;" />
<span class="io-table-header" style="outline: 0px !important;">Input</span><br />
</td><td style="outline: 0px !important;"><br style="outline: 0px !important;" />
<span class="io-table-header" style="outline: 0px !important;">Output</span><br />
</td></tr>
<tr style="outline: 0px !important;"><td style="outline: 0px !important;"><pre class="io-content" style="outline: 0px !important;">3
4 3
3 1 9 100
6 2
5 5 1 2 3 4
5 5
7 7 1 7 7
</pre>
</td><td style="outline: 0px !important;"><pre class="io-content" style="outline: 0px !important;">Case #1: 14
Case #2: 0
Case #3: 6
</pre>
</td></tr>
</tbody></table>
</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
In Sample Case #1, you can spend a total of 6 hours training the first student and 8 hours training the second one. This gives the first, second and third students a skill level of 9. This is the minimum time you can spend, so the answer is 14.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
In Sample Case #2, you can already pick a fair team (the first and second student) without having to do any coaching, so the answer is 0.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
In Sample Case #3, <b style="outline: 0px !important;">P</b> = <b style="outline: 0px !important;">N</b>, so every student will be on your team. You have to spend 6 hours training the third student, so that they have a skill of 7, like everyone else. This is the minimum time you can spend, so the answer is 6.</div>
<br />
Problem <a href="https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050e01/00000000000698d6?show=progress">link</a> <br />
<h3>
</h3>
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul>
<li><a href="https://youtu.be/Oem6C19f5ME">Youtube</a></li>
<li><a href="https://www.bilibili.com/video/BV1P7411S7HE/">B站</a> </li>
</ul>
<h3>
</h3>
<h3>
Thought Process</h3>
Start with brute force approach, get all different combos of picking P students over N. O(Cn^p), which is factorial.<br />
<br />
Sort definitely comes into mind. After sort,we have to go through the sorted array once, assuming if the highest score element is picked, the rest elements should be close to it. We can keep a rolling sum to avoid computing the sum over and over again. <br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>
Sort and rolling sum solution </h4>
<script src="https://gist.github.com/shixiaoyu/4e8d2960029cbc450870934b866a722c.js"></script>
Time Complexity: O(N) <br />
Space Complexity: O(N)<br />
<br />
<h3>
References</h3>
<ul>
<li><a href="https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050e01/00000000000698d6?show=progress">Google kickstart official solution</a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-24562919551992081472020-04-25T06:00:00.000-07:002020-04-25T06:00:09.303-07:00Google Kickstart 2020 Round A: Plates Solution<h3>
Problem Statement </h3>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Problem</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Dr. Patel has <b style="outline: 0px !important;">N</b> stacks of plates. Each stack contains <b style="outline: 0px !important;">K</b> plates. Each plate has a positive <i style="outline: 0px !important;">beauty value</i>, describing how beautiful it looks.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Dr. Patel would like to take exactly <b style="outline: 0px !important;">P</b> plates to use for dinner tonight. If he would like to take a plate in a stack, he must also take all of the plates above it in that stack as well.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Help Dr. Patel pick the <b style="outline: 0px !important;">P</b> plates that would maximize the total sum of beauty values.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Input</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
The first line of the input gives the number of test cases, <b style="outline: 0px !important;">T</b>. <b style="outline: 0px !important;">T</b> test cases follow. Each test case begins with a line containing the three integers <b style="outline: 0px !important;">N</b>, <b style="outline: 0px !important;">K</b> and <b style="outline: 0px !important;">P</b>. Then, <b style="outline: 0px !important;">N</b> lines follow. The i-th line contains <b style="outline: 0px !important;">K</b> integers, describing the beauty values of each stack of plates from top to bottom.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Output</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
For each test case, output one line containing <code style="outline: 0px !important;">Case #x: y</code>, where <code style="outline: 0px !important;">x</code> is the test case number (starting from 1) and <code style="outline: 0px !important;">y</code> is the maximum total sum of beauty values that Dr. Patel could pick.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Limits</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Time limit: 20 seconds per test set.<br />
Memory limit: 1GB.<br />
1 ≤ <b style="outline: 0px !important;">T</b> ≤ 100.<br />
1 ≤ <b style="outline: 0px !important;">K</b> ≤ 30.<br />
1 ≤ <b style="outline: 0px !important;">P</b> ≤ <b style="outline: 0px !important;">N</b> * <b style="outline: 0px !important;">K</b>.<br />
The beauty values are between 1 and 100, inclusive.</div>
<h4 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Test set 1</h4>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
1 ≤ <b style="outline: 0px !important;">N</b> ≤ 3.</div>
<h4 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Test set 2</h4>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
1 ≤ <b style="outline: 0px !important;">N</b> ≤ 50.</div>
<h3 style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: "Google Sans"; font-size: 1.25rem; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 300; letter-spacing: 0.1px; line-height: 1.75rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Sample</h3>
<div class="problem-io-wrapper" style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<table style="border-spacing: 2rem 1rem; outline: 0px !important;"><tbody>
<tr style="outline: 0px !important;"><td style="outline: 0px !important;"><br style="outline: 0px !important;" />
<span class="io-table-header" style="outline: 0px !important;">Input</span></td><td style="outline: 0px !important;"><br style="outline: 0px !important;" />
<span class="io-table-header" style="outline: 0px !important;">Output</span></td></tr>
<tr style="outline: 0px !important;"><td style="outline: 0px !important;"><pre class="io-content" style="outline: 0px !important;">2
2 4 5
10 10 100 30
80 50 10 50
3 2 3
80 80
15 50
20 10
</pre>
</td><td style="outline: 0px !important;"><pre class="io-content" style="outline: 0px !important;">Case #1: 250
Case #2: 180
</pre>
</td></tr>
</tbody></table>
</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
In Sample Case #1, Dr. Patel needs to pick <b style="outline: 0px !important;">P</b> = 5 plates:</div>
<ul style="list-style-position: outside; list-style-type: disc; margin: 0px; outline: 0px !important; padding: 1rem 0px 0px 1rem;">
<li style="outline: 0px !important;">He can pick the top 3 plates from the first stack (10 + 10 + 100 = 120).</li>
<li style="outline: 0px !important;">He can pick the top 2 plates from the second stack (80 + 50 = 130) .</li>
</ul>
In total, the sum of beauty values is 250.<br />
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
In Sample Case #2, Dr. Patel needs to pick <b style="outline: 0px !important;">P</b> = 3 plates:</div>
<ul style="list-style-position: outside; list-style-type: disc; margin: 0px; outline: 0px !important; padding: 1rem 0px 0px 1rem;">
<li style="outline: 0px !important;">He can pick the top 2 plates from the first stack (80 + 80 = 160).</li>
<li style="outline: 0px !important;">He can pick no plates from the second stack.</li>
<li style="outline: 0px !important;">He can pick the top plate from the third stack (20).</li>
</ul>
In total, the sum of beauty values is 180.<br />
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="outline: 0px !important;">Note:</b> Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.</div>
<br />
Problem <a href="https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d40bb">link</a> <br />
<h3>
</h3>
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul>
<li><a href="https://youtu.be/vJjJPWqVrWY">Youtube</a></li>
<li><a href="https://www.bilibili.com/video/BV1A7411S7uZ/">B站</a> </li>
</ul>
<h3>
</h3>
<h3>
Thought Process</h3>
First thought is similar to merging multiple lists, this won't work since the lists are not sorted and we are not allowed to sort due to the order constraint.<br />
<br />
Second thought is keep a max heap, and always merge the largest value from all the top elements in the lists. This is wrong since greedy won't work here. E.g., below if we want to pick 3 plates, using max heap will pick 2, 2, 2, instead of 1, 1, 100<br />
2, 2, 2, 2, 2<br />
1, 1, 100, 100, 100<br />
<br />
Third thought seems we have to brute force, for all the combos of P, we check what's the maximum value. Quote from the analysis, it's exponential time complexity <br />
<blockquote class="tr_bq">
<span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For example, if </span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">N</b><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> = 3 and for any given values of </span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">K</b><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> and </span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">P</b><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">, generate all possible triples (S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">1</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">, S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">2</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">, S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">3</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">) such that S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">1</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">+S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">2</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">+S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">3</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> = </span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">P</b><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> and 0 ≤ S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">i</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> ≤ </span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">K</b><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">. Note: S</span><sub style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">i</sub><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> is the number of plates picked from the i-th stack.</span><br />
<span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">This can be done via recursion and the total time complexity is O(</span><b style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">K</b><sup style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><b style="outline: 0px !important;">N</b></sup><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">) which abides by the time limits.</span></blockquote>
Forth and final solution to optimize this is using dynamic programming. It's very common in those coding competitions.<br />
Quote from the analysis<br />
<br />
<blockquote class="tr_bq">
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">First, let's consider an intermediate state </span><i style="-webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; outline: 0px !important; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">dp[i][j] which denotes the maximum sum that can be obtained using the first i stacks when we need to pick j plates in total</i><span style="color: #202124; display: inline; float: none; font-family: "roboto"; font-size: 16px; font-style: normal; font-weight: 400; letter-spacing: 0.1px; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">.</span> </div>
</blockquote>
<blockquote class="tr_bq">
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Next, we iterate over the stacks and try to answer the question: <i style="outline: 0px !important;">What is the maximum sum if we had to pick j plates in total using the i stacks we've seen so far? </i>This would give us dp[i][j]. However, we need to also decide, <i style="outline: 0px !important;">among those j plates, how many come from the i-th stack?</i> i.e., Let's say we pick x plates from the i-th stack, then <i style="outline: 0px !important;">dp[i][j] = max(dp[i][j], sum[i][x]+dp[i-1][j-x])</i>. Therefore, in order to pick j plates in total from i stacks, we can pick anywhere between [0, 1, ..., j] plates from the i-th stack and [j, j-1, ..., 0] plates from the previous i-1 stacks respectively. Also, we need to do this for all values of 1 ≤ <i style="outline: 0px !important;">j</i> ≤ <b style="outline: 0px !important;">P</b>.</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
The flow would look like:<br />
for i [1, <b style="outline: 0px !important;">N</b>]:<br />
for j [0, <b style="outline: 0px !important;">P</b>]:<br />
dp[i][j] := 0<br />
for x [0, min(j, <b style="outline: 0px !important;">K</b>)]:<br />
dp[i][j] = max(dp[i][j], sum[i][x]+dp[i-1][j-x])</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0px; color: #202124; font-family: Roboto; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.1px; line-height: 1.5rem; margin: 1rem 0px 0px; orphans: 2; outline: 0px !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
If we observe closely, this is similar to the <a href="https://en.wikipedia.org/wiki/Knapsack_problem#0-1_knapsack_problem" style="color: #1967d2; outline: 0px !important; text-decoration: none; transition: color 0.15s ease 0s;" target="_blank">0-1 Knapsack Problem</a> with some added complexity. To conclude, the overall time complexity would be O(<b style="outline: 0px !important;">N</b>*<b style="outline: 0px !important;">P</b>*<b style="outline: 0px !important;">K</b>).</div>
</blockquote>
<br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>
DP solution </h4>
<script src="https://gist.github.com/shixiaoyu/b20ed3700795c83b2811d15288ccd965.js"></script>
Time Complexity: O(N*P*K) <br />
Space Complexity: O(N*max(P, K))<br />
<br />
<h3>
References</h3>
<ul>
<li><a href="https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d40bb">Google kickstart official solution</a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-26369377189343433972020-04-18T06:00:00.000-07:002020-04-18T06:00:02.902-07:00Leetcode solution 75. Sort Colors <h3>
Problem Statement </h3>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Given an array with <i style="box-sizing: border-box;">n</i> objects colored red, white or blue, sort them <b style="box-sizing: border-box; font-weight: bolder;"><a href="https://en.wikipedia.org/wiki/In-place_algorithm" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;" target="_blank">in-place</a> </b>so that objects of the same color are adjacent, with the colors in the order red, white and blue.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Note:</b> You are not suppose to use the library's sort function for this problem.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Example:</b></div>
<pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> [2,0,2,1,1,0]
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> [0,0,1,1,2,2]</pre>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Follow up:</b></div>
<ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box;">A rather straight forward solution is a two-pass algorithm using counting sort.<br style="box-sizing: border-box;" />First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.</li>
<li style="box-sizing: border-box;">Could you come up with a one-pass algorithm using only constant space?</li>
</ul>
Problem <a href="https://leetcode.com/problems/sort-colors/">link</a> <br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul>
<li><a href="https://youtu.be/bzuYME2kFb0">Youtube</a></li>
<li><a href="https://www.bilibili.com/video/av96654652/">B站</a> </li>
</ul>
<h3>
Thought Process</h3>
This is the classic <a href="https://en.wikipedia.org/wiki/Dutch_national_flag_problem">Dutch National Flag Problem. </a><br />
<br />
Two pass with constant space is easy with count sort. (Reminds me of <a href="https://en.wikipedia.org/wiki/Radix_sort">Radix Sort</a> somehow) An extension would be what happens if there are K elements (K >=3)<br />
<br />
If do it in one pass, then the idea must involve pointers and swap. Start from left to right, if 1, pass; if 0, move to the left; if 2, move to the right <br />
<br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>
Two pass</h4>
<script src="https://gist.github.com/shixiaoyu/780b4942f7783c97cc0f04de1a4339b9.js"></script>
Time Complexity: O(N), where N is the array size, N + 3*N = 4N = O(N)<br />
Space Complexity: O(1) since only need 3 elements in this case, still considered constant <br />
<br />
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>
One Pass</h4>
<script src="https://gist.github.com/shixiaoyu/3d934b5ff175d2164503dcbc77b94523.js"></script>
Time Complexity: O(N), where N is the array size<br />
Space Complexity: O(1)<br />
<h3>
References</h3>
<ul>
<li><a href="https://drive.google.com/open?id=1imHUi_35LUGWqPnt0hsBBoKugn_7Iy-9">Leetcode official solution</a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-32882340162667225332020-04-04T06:00:00.000-07:002020-04-04T06:00:01.219-07:00System design interview: how to design a video platform (e.g, Youtube, Netflix)<h2>
System design interview: how to design a video platform (e.g., Youtube, Netflix)</h2>
<h3>
Methodology: READ MF!</h3>
<span style="font-weight: normal;">[Originally from the Post: <a href="https://blog.baozitraining.org/2020/02/system-design-interview-how-to-design.html">System design interview: how to design a chat system (e.g., Facebook Messenger, WeChat or WhatsApp)</a>]</span><br />
<br />
Remind ourselves with the "READ MF!" methodology.<br />
<br />
<h3>
Key designs and terms </h3>
Let's first admit this is a hard problem. Youtube has hundreds of smart engineers keep improving it and it's almost impossible to design a good one in 60 mins. This solution is a pretty decent one from <a href="https://drive.google.com/open?id=1dPUUEzT3SI-YZcYP-44-N6IoagLt2eSA">Grokking the system design interviews</a> where it covers the backbone about the system.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2qnDaM-_upOH2wzinHUTuWnKrrNmqrdKs7gjyUcPbsgkXqcorJuDgwcSUgrasil40vkpWjbNGpe60As4BnATP3Zemd8kMEvIE9K-Yv8h4S4DctBmM-7FOOiAxgFBozzvtAklGbCyQi_-J/s1600/youtube.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="514" data-original-width="974" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2qnDaM-_upOH2wzinHUTuWnKrrNmqrdKs7gjyUcPbsgkXqcorJuDgwcSUgrasil40vkpWjbNGpe60As4BnATP3Zemd8kMEvIE9K-Yv8h4S4DctBmM-7FOOiAxgFBozzvtAklGbCyQi_-J/s640/youtube.png" width="640" /> </a></div>
You can also divided the system as usual into three major layers, presentation, service and data layer.<br />
<br />
A few key terminologies. If you are not familiar, brush up a bit<br />
<ul>
<li>CDN: a distributed cache for videos</li>
<li>Codec: encoding and decoding, a video would be encoded to different resolutions and different formats, e.g, 16:9 mp4, 4: 3 avi</li>
<li>Video processing is slow, normally done asynchronously via queue and workflows </li>
<li>Video de-duplication, hash content (SHA, MD5), phase correlation, block matching. If you forget about Fourier Transformation, watch <a href="https://www.youtube.com/watch?v=spUNpyF58BY">3Blue1Brown</a></li>
<li>Metadata sharding, pros vs con. If shard videos by user uuid (hot
partition for popular users). If shard videos by video uuid (hot
partition for popular videos) </li>
<li>To solve hot hot partition / hot key: <a href="https://blog.baozitraining.org/2014/10/1-consistent-hashing.html">Consistent Hashing</a> (original paper 1997: <a href="https://www.akamai.com/es/es/multimedia/documents/technical-publication/consistent-hashing-and-random-trees-distributed-caching-protocols-for-relieving-hot-spots-on-the-world-wide-web-technical-publication.pdf">Consistent Hashing and Random Trees:Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web</a>) + Load balancing + Replicate data (Need to automatically discover the hotkey first, redis has built-in support). <a href="https://medium.com/@Alibaba_Cloud/redis-hotspot-key-discovery-and-common-solutions-95474d27e0f8">Alibaba Cloud Read </a></li>
<li>Comment/Reply design, Likes and View counts (all sorts of counters) would be explained in a separate post. </li>
</ul>
<br />
<h3>
Baozi Youtube Video</h3>
<ul>
<li><a href="https://youtu.be/Ji5uKgSrW9k">Youtube Video Link</a></li>
<li><a href="https://www.bilibili.com/video/av82276149/">B站</a> </li>
</ul>
<br />
<h3>
References (Credits to original authors)</h3>
<ul>
<li><a href="https://drive.google.com/open?id=1dPUUEzT3SI-YZcYP-44-N6IoagLt2eSA">Grokking the system design interviews</a></li>
<li><a href="https://medium.com/@Alibaba_Cloud/redis-hotspot-key-discovery-and-common-solutions-95474d27e0f8">Alibaba Cloud Read </a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-26191479699167227562020-03-28T06:00:00.000-07:002020-03-28T06:00:09.603-07:00Leetcode solution 190: Reverse Bits <h3>
Problem Statement </h3>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Reverse bits of a given 32 bits unsigned integer.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Example 1:</b></div>
<pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> 00000010100101000001111010011100
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 00111001011110000010100101000000
<b style="box-sizing: border-box; font-weight: bolder;">Explanation: </b>The input binary string <b style="box-sizing: border-box; font-weight: bolder;">00000010100101000001111010011100</b> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <b style="box-sizing: border-box; font-weight: bolder;">00111001011110000010100101000000</b>.
</pre>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Example 2:</b></div>
<pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px;"><b style="box-sizing: border-box; font-weight: bolder;">Input:</b> 11111111111111111111111111111101
<b style="box-sizing: border-box; font-weight: bolder;">Output:</b> 10111111111111111111111111111111
<b style="box-sizing: border-box; font-weight: bolder;">Explanation: </b>The input binary string <b style="box-sizing: border-box; font-weight: bolder;">11111111111111111111111111111101</b> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <b style="box-sizing: border-box; font-weight: bolder;">10111111111111111111111111111111</b>.</pre>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Note:</b></div>
<ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box;">Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.</li>
<li style="box-sizing: border-box;">In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;" target="_blank">2's complement notation</a>. Therefore, in <b style="box-sizing: border-box; font-weight: bolder;">Example 2</b> above the input represents the signed integer <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">-3</code> and the output represents the signed integer <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding: 2px 4px;">-1073741825</code>.</li>
</ul>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b style="box-sizing: border-box; font-weight: bolder;">Follow up</b>:</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
If this function is called many times, how would you optimize it?</div>
Problem <a href="https://leetcode.com/problems/reverse-bits/">link </a><br />
<h3>
Video Tutorial</h3>
You can find the detailed video tutorial here<br />
<ul>
<li><a href="https://youtu.be/PYS5s26t26U">Youtube</a></li>
<li><a href="https://www.bilibili.com/video/av85055139/">B站 </a></li>
</ul>
<h3>
Thought Process</h3>
Use this problem to bring awareness of bit manipulation in interviews, it's rare but sometimes do get asked by some interviewers.<br />
<br />
We can directly use Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#reverse-int-">Integer.reverse()</a> method, not suitable for interview purpose.<br />
<br />
Simple simulation, reading bits from the right most and adding them up while shifting left.<br />
<br />
Regarding the follow up, caching is an obvious solution. There are two ways to cache<br />
<ul>
<li>Cache all the integers, 2^32 = 4294967296, total memory needed is 2^32*4B = 16GB roughly, easily handled by one server </li>
<li>Cache each byte, divide the 32 bits into 4 bytes. Memory footprint is only 2^8*1B = 256B, significantly smaller than 16GB</li>
</ul>
If you are curious on how Java SDK implemented this method, it uses the algorithm in <a href="https://doc.lagout.org/security/Hackers%20Delight.pdf">Hackers Delight book</a> section 7-1 <br />
<blockquote class="tr_bq">
<pre style="-webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;"><a href="https://www.blogger.com/null" name="1191"> 1195 /**
1196 * Returns the value obtained by reversing the order of the bits in the
1197 * two's complement binary representation of the specified {@code int}
1198 * value.
1199 *
1200 * @return the value obtained by reversing order of the bits in the
</a><a href="https://www.blogger.com/null" name="1201"> 1201 * specified {@code int} value.
1202 * @since 1.5
1203 */
1204 public static int reverse(int i) {
1205 // HD, Figure 7-1
1206 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
1207 i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
1208 i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
1209 i = (i << 24) | ((i & 0xff00) << 8) |
1210 ((i >>> 8) & 0xff00) | (i >>> 24);
</a><a href="https://www.blogger.com/null" name="1211"> 1211 return i;
1212 }</a></pre>
</blockquote>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJegauAdp1OxD52U2F1YmLuJSdOQaxvBGqQerxnQGUnwfj68TFGJoZ8MgygMI3Soy2cc_rbRJCqAMXAdDWF3OxMk5RMfQyn0HzGV8fNS_iSLwHQXweqJTQo5cmGuo__UV3LBwO4oKacN5v/s1600/Screen+Shot+2020-01-25+at+1.39.32+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="974" data-original-width="1600" height="388" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJegauAdp1OxD52U2F1YmLuJSdOQaxvBGqQerxnQGUnwfj68TFGJoZ8MgygMI3Soy2cc_rbRJCqAMXAdDWF3OxMk5RMfQyn0HzGV8fNS_iSLwHQXweqJTQo5cmGuo__UV3LBwO4oKacN5v/s640/Screen+Shot+2020-01-25+at+1.39.32+PM.png" width="640" /></a></div>
<br />
<h3>
Solutions</h3>
<h3 class="separator" style="clear: both; text-align: left;">
</h3>
<h4>
Simple simulation with follow up question </h4>
<h4>
</h4>
<script src="https://gist.github.com/shixiaoyu/24fc4c1cc89ecea90b0644ac99ea2bdb.js"></script>
Time Complexity: O(N), where N is number of bits in the input integer<br />
Space Complexity: O(1), no extra space needed <br />
<br />
<h3>
References</h3>
<ul>
<li><a href="https://leetcode.com/problems/reverse-bits/discuss/54741/O(1)-bit-operation-C%2B%2B-solution-(8ms)">Leetcode discussion solution</a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0San Francisco, CA, USA37.7749295 -122.4194155000000136.9717915 -123.71030900000001 38.578067499999996 -121.12852200000002tag:blogger.com,1999:blog-1402726418106920478.post-73857939057891407382020-03-21T06:00:00.000-07:002020-03-21T06:00:04.569-07:00System design interview: how to design comments and reply, likes button and total views on Youtube<h2>
System design interview: how to design comments and reply, likes button and total views on Youtube</h2>
<h3>
Methodology: READ MF!</h3>
<span style="font-weight: normal;">[Originally from the Post: <a href="https://blog.baozitraining.org/2020/02/system-design-interview-how-to-design.html">System design interview: how to design a chat system (e.g., Facebook Messenger, WeChat or WhatsApp)</a>]</span><br />
Remind ourselves with the "READ MF!" methodology.<br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-size: small;">This is a follow up on the previous post: <a href="https://blog.baozitraining.org/2020/01/system-design-interview-how-to-design-youtube.html"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">System design interview: how to design a video platform (e.g, Youtube, Netflix)</span></a></span></span><br />
<br />
<h3>
Requirements </h3>
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"></span></span></span><span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"></span></span></span><span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </span><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"></span><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"></span></span></span><span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">First, let's quickly about requirements. Likes and views are relatively straightforward, users can torllerate a bit delay and inaccuracy. For the majority case, as long as user clicks the button, it pluses one, then it's fine. Sometimes it's not the case if videos have too many likes, e.g., if a Youtube video already has 10K likes, you plus 1, it still shows 10K, it's just the UI tricks you that it's toggled. </span></span></span><br />
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></span></span></span>
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">For comments reply, there are a few different styles. The major ones such as Youtube, Instagram and TikTok uses following style. It displays comments (directly reply to video) based on order of likes and timestamp (descending) and any reply to comments are only 1 on 1, meaning A@B How are you, then B@A I am fine thank you and you? There is no more indentation needed. </span></span></span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVjLg1-aCfO5cUVvrqeNFzIRm_5dKVmohG027FMO4WLws67ojfA2jgSfaRJwoUsqpJaHkywtmPK5jXBZBBiRfMqvOTL50gfdr8E0z4w2PfrviHzPRS-Q2BWc9n2GFuNb_OVpFlJuuE4U_H/s1600/tiktok.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1600" data-original-width="900" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVjLg1-aCfO5cUVvrqeNFzIRm_5dKVmohG027FMO4WLws67ojfA2jgSfaRJwoUsqpJaHkywtmPK5jXBZBBiRfMqvOTL50gfdr8E0z4w2PfrviHzPRS-Q2BWc9n2GFuNb_OVpFlJuuE4U_H/s640/tiktok.PNG" width="360" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgucLFRHt8SPLah317kxlodY6zNi32oYC2TbRqFvQq6TmVLcWYj7H8ExNwafs9RAkIvC3L8QgB0nEpDqmTaW3MInCwV70HyG8HaFTychu2GdkxAi1LWhsev87QMUyZBLEvUIzZGXdk0PWyq/s1600/ins.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1289" data-original-width="1600" height="513" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgucLFRHt8SPLah317kxlodY6zNi32oYC2TbRqFvQq6TmVLcWYj7H8ExNwafs9RAkIvC3L8QgB0nEpDqmTaW3MInCwV70HyG8HaFTychu2GdkxAi1LWhsev87QMUyZBLEvUIzZGXdk0PWyq/s640/ins.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5QvwW2Uyc5jS6TmVqR-sIKbE5Or1Gu1WFzVhe-Dh2g7qqvtPZBIG4SOZWlRIxasyF9ZUk6b7gW-sEXsH1WrotUjFn6ltySn5djDO2x2VDudKvLQ9ZHufyn3Efbnbv_DPf22pjO_4gHdRI/s1600/youtube.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1078" data-original-width="1350" height="510" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5QvwW2Uyc5jS6TmVqR-sIKbE5Or1Gu1WFzVhe-Dh2g7qqvtPZBIG4SOZWlRIxasyF9ZUk6b7gW-sEXsH1WrotUjFn6ltySn5djDO2x2VDudKvLQ9ZHufyn3Efbnbv_DPf22pjO_4gHdRI/s640/youtube.png" width="640" /></a></div>
<br />
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></span></span></span>
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Reddit uses the "block building" reply-to style (中文俗称"盖楼"), where it shows which reply replies to which reply, and it needs to show the indentations about those replies. </span></span></span><br />
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></span></span></span>
<br />
<span style="font-family: inherit;"><span style="font-size: small;"><span style="color: #333333; display: inline; float: none; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-LJQJ7TeHbrtgosM-nkTYgd-kwshQxvRxzLkPhTMRdshbkdtWzAGTYrLL1FllNRl0OUjis6zZov4uWwyZzIAcO6dwuxQKEk52cT6uer92N44XTiKGV-QFwfCQxTTRvbsJpkEBWKX2lz8P/s1600/reddit.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1600" data-original-width="1406" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-LJQJ7TeHbrtgosM-nkTYgd-kwshQxvRxzLkPhTMRdshbkdtWzAGTYrLL1FllNRl0OUjis6zZov4uWwyZzIAcO6dwuxQKEk52cT6uer92N44XTiKGV-QFwfCQxTTRvbsJpkEBWKX2lz8P/s640/reddit.png" width="562" /></a></span></span></span><br />
<br />
<br />
<h3>
Estimation</h3>
For viral videos, say normally it has around 10M views<br />
For likes, assuming 20% people liked a video, 10M * 20% = 2M likes<br />
For comments, 1% people would leave a comment (we are lazy) 10M * 10% = 100K comments<br />
<br />
For the majority normal videos, it would probably has 1000 views and 100 likes top and maybe 10 comments, a relationship DB could solve it pretty well. <br />
<h3>
</h3>
<h3>
Key designs and terms </h3>
<h4>
Comments design </h4>
If you start building your product, just bootstrap it with a relational DB<br />
Introduce a comments table shard by video UUID, add a reply_to_uuid to know which comment the reply is targeted to and leave it null for root comment. Build an index on the reply_to_uuid<br />
<br />
<blockquote class="tr_bq">
Select * from comments where reply_to_uuid is null order by comment likes desc, timestamp desc</blockquote>
<br />
<br />
If you need to see the replies to those comments, just<br />
<br />
<blockquote class="tr_bq">
Select * from comments where reply_to_uuid is the_target_comment_uuid order by comment likes desc, timestamp desc</blockquote>
<br />
Even if your product becomes Youtube scale, the comments would be around 100K for viral videos, the above solution would still works fine. Simply add more capacity to better shard your comments using consistent hashing, cache the comments would do the trick.<br />
<br />
If you need to build the Reddit tree structure, just sort it in memory. If the problem can fit into memory, it becomes much easier. <br />
<br />
The extreme case is your comments section becoming a chat, then we can do something like an append only in memory DB or redis cache keep appending the values to the queue with async backup to DB. <br />
<br />
<h4>
Views and Likes count design</h4>
Similarly, when you bootstrap the project, keep a counter in DB or in memory cache solves your problem when traffic is low. If within one machine, you don't even need locks just use compare and swap (CAS), atomic operations for counting, thread safe. <br />
<br />
If your product starts to become popular, add more capacity using
consistent hashing. Add in memory cache like Redis to count the values
(memory access time 100us vs disk access time 10ms. 100Kx improvement).
Could be further optimized using distributed counter, aggregating the
results together when read. <br />
<br />
If you product becomes YouTube scale, then
use offline counting. Build a pipeline to promote the videos from cold
to hot/viral once the view counts hit a certain threshold (say 1M). Use
async messaging like Kafka to ingest from those logs and pump it to data
warehouse, query it and update the values on a cron schedule. Of course on the UI side, you need to toggle the like button, plus 1 if needed (Sometimes you would see a 100K likes video, even if clicked the like, the count would not be increased)<br />
<br />
<h3>
Baozi Youtube Video</h3>
<ul>
<li><a href="https://youtu.be/HSDfhFUNWxs">Youtube Video Link (In Chinese and it's a funny one)</a></li>
<li><a href="https://www.bilibili.com/video/av82920867/">B站</a> </li>
</ul>
<h3>
References (Credits to original authors)</h3>
<ul>
<li><a href="https://drive.google.com/open?id=1dPUUEzT3SI-YZcYP-44-N6IoagLt2eSA">Grokking the system design interviews</a></li>
<li><a href="https://medium.com/@Alibaba_Cloud/redis-hotspot-key-discovery-and-common-solutions-95474d27e0f8">Alibaba Cloud Read </a></li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com3tag:blogger.com,1999:blog-1402726418106920478.post-53014476936825190652020-03-14T06:00:00.000-07:002020-03-14T06:00:01.805-07:00包子聊天系列 - Perf Review到底怎么回事儿?如何拿到好的Rating?<h2>
包子聊天系列 - Perf Review到底怎么回事儿?如何拿到好的Rating?</h2>
大家没事儿的时候当做podcast听吧,对于new grad新兵快速往上升职还是有帮助的。我们不是大佬,说的也不一定都对,但是我们确实是老呀,So show some respect son!😃😃😃😃<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiam99wDtFfzmxLFEgSW_hJboIR_yzSEqHZExrmQQu3QAylQLGyW0SvMHBlNZnbqTxFrBy7SLxtsirX09-MWxB9_-onzJ3ULo1lenV8L8Gh5MPo-oIh8NDW8AZxof-6BqdW38eBs6iaTVBx/s1600/respect-my-authority.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="440" data-original-width="625" height="280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiam99wDtFfzmxLFEgSW_hJboIR_yzSEqHZExrmQQu3QAylQLGyW0SvMHBlNZnbqTxFrBy7SLxtsirX09-MWxB9_-onzJ3ULo1lenV8L8Gh5MPo-oIh8NDW8AZxof-6BqdW38eBs6iaTVBx/s400/respect-my-authority.jpg" width="400" /></a></div>
<br />
<ul>
<li><a href="https://youtu.be/2AMM6k4LRcw">Youtube 视频</a></li>
<li><a href="https://www.bilibili.com/video/av94821663/">B站视频</a></li>
<li><a href="https://www.youtube.com/playlist?list=PLAE-zml3hxQuKHSu0O345AtBk5ja7JQre">Youtube包子聊天系列playlist</a></li>
</ul>
<h2>
<br /></h2>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-76134537618709138302020-03-10T20:58:00.000-07:002020-03-10T20:58:12.428-07:00美国菜鸟买枪看家护院指南<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;"><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">【作者】 包子小旋风老师 【包子铺里聊IT <a href="https://mp.weixin.qq.com/s?__biz=MzA4MTAzNTIyMA==&mid=2652374651&idx=1&sn=68592423a455af086d84ef0e2e6641da&chksm=84778cc0b30005d6ad8f5e0d524caaa4878713f910768869d4f5decefa380fd06821d2c09386&token=1250388703&lang=zh_CN#rd">微信公众号</a>】</span></span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;"><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"> </span> </span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">新冠肺炎</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">美国各州新增案例,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">不少小伙伴动了买</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">枪的念头。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">听</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">闻</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">一个朋友</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">今天</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">买</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">了把</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">散弹</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">枪,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;"></span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">问他什么</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">g</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">auge,他说ga</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">ug</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">e是什么,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">问他枪</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">管多长,他说没仔细看</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">...</span><img _width="20px" class="" crossorigin="anonymous" data-fail="0" data-ratio="1" data-src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Yellowdog.png" data-w="20" src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Yellowdog.png?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="box-sizing: border-box !important; display: inline-block; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; height: auto !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify; vertical-align: text-bottom; visibility: visible !important; width: 20px !important;" /><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">虽然</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">在</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">美</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">国买枪</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">虽然和买白菜差不多</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">,但枪的</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">参数因为用途,差别很大,这篇文章写</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">给</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">第一次买枪想用</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">来家防</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">的</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">菜鸟</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; text-align: justify;">。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">买枪需要什么资格?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
如果你<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">没有绿卡</strong>,可以买,但比较麻烦,你必须要有打猎证才可以买枪。获取打猎证需要经过hunter education,可以选择线下上课2,3天,或者线上课程和线下半天课,然后通过多选题的考试。大概是100题要对90题以上。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
如果你<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">有绿卡</strong>,你只需要获取Firearm Safety Certificate,你可以上网搜搜题库,然后打电话去你附近的枪店,说你想要考这个证,有时可以随时答题,有时要预约,去店里做多选题,大概是30题主要对25以上就通过了,题目是基本安全须知。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">什么枪适合家防Home Defense?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
枪一般分为手枪Pistol,散弹枪Shotgun和来复枪Rifle。其中来复枪大多用来长距离精确射击,用于家防的枪,目标很近,所以基本不用来复枪。合适的手枪和散弹枪都可以用来家防。但绝不是所有手枪或散弹枪都适合家防。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">我们来比较下</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">手</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">枪和散弹枪。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">手枪的优势</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">小巧</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">灵活</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">,在被近身或者空间狭小的情况下</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">散弹</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">枪</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">难以发挥。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">散弹</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">枪的优势是</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">瞄准容易射击稳定,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">因为散弹枪和</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">人</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">有三个接触点</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">(肩膀和两个手)比只有一个接触点(两个手放在一起)的手枪</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">稳定很多,后坐力更容易控制。散弹枪</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">的子弹是</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">很多的小钢珠,命中率也会比手枪的单一弹头高很多。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">小旋风</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">用</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">散弹枪打20米外的运动的飞</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">盘的命中率,远远大于用手枪设计1</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">0米外</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">固定的靶子<img _width="20px" class="" crossorigin="anonymous" data-fail="0" data-ratio="1" data-src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Sweat.png" data-w="20" src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Sweat.png?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="box-sizing: border-box !important; display: inline-block; height: auto !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; vertical-align: text-bottom; visibility: visible !important; width: 20px !important;" /></span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"></span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
到底选哪种枪,非常建议找玩枪的朋友带你去靶场试试看,很大可能你会喜欢一种远大于另一种,买适合自己的枪,不光图个顺手,更重要的是你会更愿意去练习射击,而只有非常熟悉的枪和丰富的射击经验,关键时刻才真正能起到保护自己的目的。<img _width="20px" class="" crossorigin="anonymous" data-fail="0" data-ratio="1" data-src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Addoil.png" data-w="20" src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Addoil.png?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="box-sizing: border-box !important; display: inline-block; height: auto !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; vertical-align: text-bottom; visibility: visible !important; width: 20px !important;" /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">具体选哪一款枪呢?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
对于第一把枪,大多数菜鸟会选择一把便宜的,400-500之间。推荐买最popular的大品牌的枪,大品牌的枪可靠性更高。枪的品牌非常多,较熟悉的散弹枪品牌有Remington, Winchester, Benelli, Beretta, Browning, Mossberg, Ruger, Marlin等等,这些牌子的入门级shotgun都可以入手。包子君自用的是史上最Popular的Remington 870。在很多电影里都出现它,新手必备。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: center; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<img src="https://mmbiz.qpic.cn/mmbiz_jpg/edSnflBH73ibdLTNjJOcgFd3vTVddQwQ3WzuWTShqUQf35VFHDlUUDNhicc7NBpZaUdmwN9PcXyUstRzkbe5d1SQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
手枪的话,常见的品牌除了上面这些散弹枪品牌,还有Glock, Sig, Smirth&Wesson, CZ USA等等。其中最Popular的是Glock 19,口径9mm的。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">枪有什么重要的参数要留意?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
选定了一款散弹枪后,需要注意三个参数:第一个是口径也叫gauge或者caliber,代表枪管的直径,常见的是12gauge和20guage,12的子弹比20的粗,火药更多后坐力更大,钢珠更<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">多,枪身也更重</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">建议175以上的</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">男生用1</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">2gauge,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">身材小一点的男生和女生,都适合20gauge。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">如果</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">你让你的女票或老婆去打12gauge,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">那么</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">你</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">从今以后去靶场,她肯定不会</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">跟你一起了...</span><img _width="20px" class="" crossorigin="anonymous" data-fail="0" data-ratio="1" data-src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Sweat.png" data-w="20" src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/Sweat.png?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="box-sizing: border-box !important; display: inline-block; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; height: auto !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; vertical-align: text-bottom; visibility: visible !important; width: 20px !important;" /><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">第二个</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">是</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">枪管的长度barrel length。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">默认的散弹枪的长度大多28</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">寸,家防的应用场景,需要枪管更短才</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">更</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">灵活</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">,</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">一般用</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">1</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">8寸</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">左右的。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">如果你想回头拿去打猎,那你需要换个28-30的枪管来增加有效距离</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">。</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">第三个参数是子弹的长度和</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">钢珠的大小,最简单的方法是</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">告诉</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">店</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">员</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">你是用</span><span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">来家防的,让他推荐子弹,别自己去选,多半会选错。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
手枪的参数较少,主要看Caliber,9mm是最流行的,它后坐力包子君觉得蛮大的...</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">考好了证,选好了枪,怎么去买?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
去你附近的枪店(连锁的Sportsman Warehouse, BassPro, Big 5, Dicks或者gun range),带上你的real ID的驾照或者护照,绿卡或Visa,和上面说的证件<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">,</strong>就可以去挑选枪支了,选好后,要做背景调查(费用37.5),填很多的表格之后,过10天之后去店里取。和任何购物一样,枪也时常会有打折,你可以在这些店的网站上刷deal和评论。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<strong style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">有了枪,你就安全了吗?</strong></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
绝对不是,你需要练习,练习,练习!千万不要觉得自己有了枪,遇到坏人拿出来就能把他吓走。你一定要去靶场射击练习至少10次。试想,坏人闯入你家,你迅速的拿到了枪,情况紧急,你如果不能2秒内架好枪,完成瞄准,随时可以射击来形成威慑,坏人很有可能已经射击或者冲到你的面前夺枪。<span style="box-sizing: border-box !important; font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;">射击和所有运动一样,多练习就会提高,如果你没有个1000发射击经验,遇到紧急情况,肯定无法发挥作用,还不如没有枪,可以去专心考虑逃跑或者别的方法脱困。湾区附近打飞盘和手枪的地方包子君推荐Sunnyvale Rod & Gun Club,这里的工作人员都是Volunteer,对新手非常友好。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
其实啦,枪是蛮好玩的,当你打碎飞在空中的飞盘,或者击中靶纸的10环,都会觉得很爽<img _width="20px" class="" crossorigin="anonymous" data-fail="0" data-ratio="1" data-src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/2_11.png" data-w="20" src="https://res.wx.qq.com/mpres/htmledition/images/icon/common/emotion_panel/emoji_wx/2_11.png?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="box-sizing: border-box !important; display: inline-block; height: auto !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px; vertical-align: text-bottom; visibility: visible !important; width: 20px !important;" />枪既是武器,也是很好玩的玩具。有了枪,你会去关注枪的制作工艺,制作精良射击优美的枪,也是件简约而美丽的艺术品。而打猎,更是一种特别的了解自然的方式,付出辛苦徒步找寻猎物,再把他们做成佳肴,都是很棒的体验。当你入手了第一把枪,说不定你就会发现一项新的爱好。</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;" /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
包子粉丝们如果有什么问题,请在下方留言</div>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-67290283865041776822020-03-10T20:53:00.000-07:002020-03-10T20:56:13.322-07:00新冠肺炎来袭, 视频onsite指南<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">【作者】 包子霹雳火老师 【包子铺里聊IT <a href="https://mp.weixin.qq.com/s?__biz=MzA4MTAzNTIyMA==&mid=2652374657&idx=1&sn=edeb050fcceebb8dcdc52790a8a068c5&chksm=84778c3ab300052ca0f6c20d7943836df0829984e99fe0b2ba8f47b19ad17f530883ba38c6c2&token=1250388703&lang=zh_CN#rd">微信公众号</a>】</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这两天在北美最火的话题应该就是出门要不要戴口罩,以及今天要不要去公司上班了,小编连续在家</span>work from home<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">了两天就感觉得一个人上班是多么的孤单寂寞冷,无比想念大家一起在公司上班有说有笑,谈天谈地的日子。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">随着新冠肺炎目前在北美有愈演愈烈之势,再加上蜜汁自信的美国政府跟毫无能力的基础医疗,很多大公司已经开始鼓励自己的员工</span>work from home<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">了</span>. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">同时也有一些公司开始取消</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">面试来减少病毒传播的风险。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">同一时间</span>, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">很多</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">面试也被各个公司用</span>OA<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">或者视频面试来取代。那么对于正在准备面试的小伙伴来说,这次疫情带来的影响是很大的,除了面试被取消的风险之外,还有全新的视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的经历,这些都会给前期的面试准备带来一些不确定的因素。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">在此,包子君根据以往长期的视频模拟面试经验,总结一些个人觉得有用的信息,希望给在准备的面试和即将要做视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的小伙伴一些帮助。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<ul class="list-paddingleft-2" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; list-style-type: disc; margin: 0px; max-width: 100%; orphans: 2; overflow-wrap: break-word !important; padding: 0px 0px 0px 2.2em; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "symbol"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"><span style="box-sizing: border-box !important; font-size: 9px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"> </span></span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">如何更好地做自我介绍</span></div>
</li>
<ul class="list-paddingleft-2" style="box-sizing: border-box !important; list-style-type: square; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px 0px 0px 30px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">很多小伙伴可能把自己的简历准备得很是熟练,尤其是介绍</span>past experience<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的时候很多时候在</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">中都可以通过画图的方式来体现自己的</span>project<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的</span>impact<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">跟</span>complexity. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">但是当</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">转到视频之后</span>, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这些准备好的图可能一下就无法展现出来了,这时候小伙伴就应该换一种思维去想面试官想要从你的自我介绍中得到什么</span>?</div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">自我介绍一般都是面试官给面试者一个熟悉环境,放松的时间,所以从面试官本身来讲更多是期待面试者能够自然地总结自己过去的经历。</span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这个时候就需要面试者能够有以下的一些能力</span></div>
</li>
<ul class="list-paddingleft-2" style="box-sizing: border-box !important; list-style-type: circle; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px 0px 0px 30px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
High-level <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">讲述自己组或者自己负责</span>service<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的</span>responsibility<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">跟</span>business impact</div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">指出在整个</span>system<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">中或者整个</span>project<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">中自我的</span>contribution. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这里的</span>contribution<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">不仅仅是写的</span>code, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">还可以是很多自己</span>drive<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的</span>process</div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
Detail-oriented to key components, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">最后面试者要能够详细的讲出自己</span>contribution<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">里面的</span>technical details<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">。这一步一般在视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的时候会被省略掉,但是如果面试官问起来,面试者还需要能够用最简单明了的语言去描述。</span></div>
<div style="box-sizing: border-box !important; clear: both; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px;">
<span style="box-sizing: border-box; color: black; font-family: "dengxian"; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"></span></div>
<div style="box-sizing: border-box !important; clear: both; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px;">
<span style="box-sizing: border-box; color: black; font-family: "dengxian"; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"></span></div>
</li>
</ul>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px;">
<span style="box-sizing: border-box; color: black; font-family: "dengxian"; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">总上所述,面试者在准备自我介绍的过程中,可以试着把自我介绍介绍给非</span><span style="box-sizing: border-box; color: black; font-family: "calibri" , sans-serif; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">CS</span><span style="box-sizing: border-box; color: black; font-family: "dengxian"; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的小伙伴,看看别人是否能大概知道你在公共做或者学校</span><span style="box-sizing: border-box; color: black; font-family: "calibri" , sans-serif; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">project</span><span style="box-sizing: border-box; color: black; font-family: "dengxian"; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">里有什么样的作用</span><span style="box-sizing: border-box; color: black; font-family: "calibri" , sans-serif; font-size: 16px; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">.</span></div>
</li>
</ul>
</ul>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<ul class="list-paddingleft-2" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; list-style-type: disc; margin: 0px; max-width: 100%; orphans: 2; overflow-wrap: break-word !important; padding: 0px 0px 0px 2.2em; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">如何更好地做</span>online coding</div>
</li>
<ul class="list-paddingleft-2" style="box-sizing: border-box !important; list-style-type: square; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px 0px 0px 30px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">在视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">过程中,对于面试这最熟悉的应该就是</span>coding<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的部分了</span>, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">但是跟普通</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">不同,在视频过程中你的表情,说话语气,包括神态都会放大般的展现在面试官的面前。所以在此,小编要给大家提醒一下几点</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">不要自言自语,尤其是说中文。因为在小编过往模拟面试的过程中,有些面试者还时不时得自言自语两句中文,这在视频面试中是一个很大的</span>red flag.</div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">跟面试官沟通,沟通,沟通!!!重要的事情说三次。</span>Onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">面试的时候有些面试者喜欢那道题之后自己思考,但是在视频面试中,面试者一定要学会</span>think loud and keep talking. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">要让面试官知道你的想法是什么,你的思路是否正确,或者你</span>stuck<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">在哪里。对于面试官来讲,面试的过程是一个选择自己潜在同事的过程,而不是一个考试的过程,所以面试官注重的一点就是能否跟面试者有有效的沟通。</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
Take feedback. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">在一般</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">面试过程中,如果面试者的</span>solution<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">有问题,面试官都会给一些</span>hint<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">或者一些</span>feedback<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">通过各式各样的交流。但是在视频面试过程中,面试官可能就会通过举反例或者提问的方式向面试者提供一些</span>feedback,<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这时候面试者就应该去</span>jump out<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">然后去想这些</span>feedback<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">是否对你的</span>solution<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">有用</span>.</div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">最后,还是要鼓励大家熟悉一些自己</span>coding language. Online coding<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">有些公司是需要跑</span>code<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的,所以对自己的</span>language<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">一定要熟悉,小编见过很多人喜欢用</span>python<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">只是因为简单好写,但是到了</span>online coding<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的部分,如果你选了你不熟悉的语言,不知道怎么</span>debug<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">,那么从面试官的角度来讲就是你</span>technical skills<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">不过关了。</span></div>
</li>
</ul>
</ul>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"></span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<ul class="list-paddingleft-2" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; list-style-type: disc; margin: 0px; max-width: 100%; orphans: 2; overflow-wrap: break-word !important; padding: 0px 0px 0px 2.2em; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">如何更好地做</span>system design</div>
</li>
<ul class="list-paddingleft-2" style="box-sizing: border-box !important; list-style-type: square; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px 0px 0px 30px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">对于</span>System design<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的问题</span>, <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">确实会有很大的挑战。首先面试者没有自己擅长的白板,这样很多架构图就无法展现出来。这时候对于面试者的挑战就变成了如何清晰地展现自己处理问题的思路和如何利用好</span>bullet point<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">来表达自己的观点了。</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">通常来讲,面试者先要把</span>system design<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的</span>requirement<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">清晰地记录下来,不明确的要去跟面试官沟通,把需求确定好。</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">其次就是要把整个</span>system<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">分成不同的部分,这时候就要用</span>bullet point<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">把自己的思维一步步得展现给面试官。就当自己在写</span>ppt<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的概要</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">做完上述两步之后,面试者就可以跟面试官讨论然后着重挑其中的一个</span>component<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">做</span>detail design. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">比如</span>db schema, API signature <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">然后</span>customer use scenario , error handling. <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">这些都可以用简单的</span>statement<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">写好</span></div>
</li>
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">最后面试者需要做出一些</span>summary<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">的总结,列举出</span>pros and cons.<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">向面试官展现出一个完整的设计思路</span></div>
</li>
</ul>
</ul>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"></span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<ul class="list-paddingleft-2" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; list-style-type: disc; margin: 0px; max-width: 100%; orphans: 2; overflow-wrap: break-word !important; padding: 0px 0px 0px 2.2em; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">如何更好地做</span>behavior question</div>
</li>
<ul class="list-paddingleft-2" style="box-sizing: border-box !important; list-style-type: square; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px 0px 0px 30px;">
<li style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;"><div style="box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; margin: 0px; max-width: 100%; min-height: 1em; overflow-wrap: break-word !important; padding: 0px; text-align: start; white-space: normal;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">对于</span>behavior <span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">来说,应该是对于视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">来说最直观的了。重要的一点就是管理好自己的表情,表达方式,</span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">毕竟</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">面试官看到的是一个整体的人,而在视频</span>onsite<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">中看到的只是面试者的脸。所以任何小的问题都会被放大。</span></div>
</li>
</ul>
</ul>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;"></span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">最后小编希望新冠肺炎早日结束,至少希望迷之操作的美国政府能想我们大中国一样拿</span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">出点有力地防护措施。</span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">也希望</span><span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">大家早日找到心仪的工作。</span></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: #333333; font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br style="box-sizing: border-box !important; margin: 0px; max-width: 100%; overflow-wrap: break-word !important; padding: 0px;" /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box !important; clear: both; color: black; font-family: Calibri, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: 0.544px; margin: 0px; max-width: 100%; min-height: 1em; orphans: 2; overflow-wrap: break-word !important; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="box-sizing: border-box; font-family: "dengxian"; margin: 0px; max-width: 100%; overflow-wrap: break-word; padding: 0px;">包子粉丝们如果有什么问题,请在下方留言</span></div>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0tag:blogger.com,1999:blog-1402726418106920478.post-4719515421182549252020-03-08T20:42:00.002-07:002020-03-10T21:36:53.333-07:00How to effectively work from home as a software engineer (during COVID-19)<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h2>
How to effectively work from home as a software engineer (during COVID-19😷) </h2>
<ul>
<li><a href="https://youtu.be/IbziWiQKfII">Youtube </a></li>
<li><a href="https://www.bilibili.com/video/av95391407/">B站</a></li>
</ul>
Most bay area and Seattle tech companies have already allowed employees to Work From Home (WFH). It is a very common topic on how to effectively work from home. Here I quickly summarized a few quick tips for particularly software engineers. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg57Mvj4Ssj3fZBPg5hs9bINZFL0nkxTnuVbfg3lLHGkPDm5889ahVlFQe_kTvKaqHJdsvczS3I0_tTSNUZrO7vKuhM8xLbp7yAOgM6D_7EGc-AWU6CJ4uxgkGFV_Q0-vOyvNpy2supDq5G/s1600/wfh.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="225" data-original-width="225" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg57Mvj4Ssj3fZBPg5hs9bINZFL0nkxTnuVbfg3lLHGkPDm5889ahVlFQe_kTvKaqHJdsvczS3I0_tTSNUZrO7vKuhM8xLbp7yAOgM6D_7EGc-AWU6CJ4uxgkGFV_Q0-vOyvNpy2supDq5G/s320/wfh.jpeg" width="320" /></a></div>
<br />
<h3>
Tip 1: Keep in mind that WFH is a privilege</h3>
Not everyone in the US or around the world can wfh. It's a privilege and we should not take it for granted. Assuming people in those roles are professionals that have a clear goal and career aspiration, with solid self-discipline and would not abuse it. Don't slack off, too much. Ultimately you only work for yourself.<br />
<br />
<h3>
Tip 2: Get dressed & Keep a normal schedule</h3>
Do that normal thing you do when you go to office. You can wear your PJs with Xmas socks, but keep your upper body dressed normally for online meetings. Moreover, keep your normal working schedules and take breaks as well (especially if you are coding and super focused, now nobody bugs you and you could sit for a long time, take breaks). You might actually be more productive since you now are able to potentially save lots of commute time.<br />
<h3>
Tip 3: Stay available & Make online meeting fun</h3>
Presumably we still need to join meetings or communicate with each via chats. Stay available, especially now everyone is remote.<br />
<br />
Online meetings for a while could be dry, to make things fun, this is how you can set up zoom or any online video conferencing with face filters and lenses<br />
<br />
<ol>
<li>Download snap camera: <a href="https://snapcamera.snapchat.com/">https://snapcamera.snapchat.com/</a></li>
<li>Open your video conferencing software, and choose video camera: snap camera <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZRuuJEaq3GNMsvVpkt1GMTixfk31uiyE9MmMpTC1Ibpmf1S5AH2EPoITOLfLG9o4kZyDfOKkyNswENnFtS_RkbxTpM6GtLgWtWrgrjTzzqLrcAz5wIU2qzTbbmnQDzeHaE3cgfGMdlurf/s1600/Screen+Shot+2020-03-08+at+8.30.12+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="452" data-original-width="994" height="181" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZRuuJEaq3GNMsvVpkt1GMTixfk31uiyE9MmMpTC1Ibpmf1S5AH2EPoITOLfLG9o4kZyDfOKkyNswENnFtS_RkbxTpM6GtLgWtWrgrjTzzqLrcAz5wIU2qzTbbmnQDzeHaE3cgfGMdlurf/s400/Screen+Shot+2020-03-08+at+8.30.12+PM.png" width="400" /></a></div>
</li>
<li>Open snap camera and choose lenses <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVzaL6tu-A5KP2_iceQzfUXSPtu4TbjWqqC2ZXrmGtAmygRzBsqhAm1Tdmi7SzZ3m3euNS2D6QqT-jMREB3tMXnDkfuBEg9eEnXf3UYqXuUc_mbbAYsPFfR-brmFXqQIV8mHW7qF1XkGZh/s1600/Screen+Shot+2020-03-08+at+8.31.40+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1114" data-original-width="1600" height="277" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVzaL6tu-A5KP2_iceQzfUXSPtu4TbjWqqC2ZXrmGtAmygRzBsqhAm1Tdmi7SzZ3m3euNS2D6QqT-jMREB3tMXnDkfuBEg9eEnXf3UYqXuUc_mbbAYsPFfR-brmFXqQIV8mHW7qF1XkGZh/s400/Screen+Shot+2020-03-08+at+8.31.40+PM.png" width="400" /></a></div>
</li>
<li> Happy zooming! <table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9nwXqnv45dnxxgOsckwqqf-N5w2quc7QTkUMlJOICfsSGGCDStQT2B7uhs8MQZ8m3LPiZkdOXXz_4IHAoKE9iHfMBxinqJRSxWPecPaV0AwpthyphenhyphenBnSG0u94lGRnjLOIjkKt3vkKWG_sQu/s1600/Screen+Shot+2020-03-08+at+8.33.16+PM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="838" data-original-width="1558" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9nwXqnv45dnxxgOsckwqqf-N5w2quc7QTkUMlJOICfsSGGCDStQT2B7uhs8MQZ8m3LPiZkdOXXz_4IHAoKE9iHfMBxinqJRSxWPecPaV0AwpthyphenhyphenBnSG0u94lGRnjLOIjkKt3vkKWG_sQu/s400/Screen+Shot+2020-03-08+at+8.33.16+PM.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Me in real meetings with folks, when stakes are high, VR lenses can help :) </td></tr>
</tbody></table>
</li>
</ol>
<h3>
Tip 4: Maintain a work life harmony </h3>
You can choose to balance work and life, but I would rather maintain a work life harmony, it's a circle, said by a wise and definitely wealthy guy. (It's Jeff B if you are still wondering) <br />
<br />
<h3>
References </h3>
<ul>
<li>https://www.cnbc.com/2018/08/09/what-jeff-bezos-does-instead-of-work-life-balance.html</li>
<li>https://hbr.org/2014/10/5-ways-to-work-from-home-more-effectively</li>
</ul>
Baozi Bloggerhttp://www.blogger.com/profile/07289751810673778475noreply@blogger.com0