LintCode Consistent Hashing II Consistent Hashing II [http://lintcode.com/en/problem/consistent-hashing-ii/] 在 Consistent Hashing I 中我们介绍了一个比较简单的一致性哈希算法，这个简单的版本有两个缺陷： 1. 增加一台机器之后，数据全部从其中一台机器过来，这一台机器的读负载过大，对正常的服务会造成影响。 2. 当增加到3台机器的时候，每台服务器的负载量不均衡，为1:1:2。 为了解决这个问题，引入了 micro-shards 的概念，一个更好的算法是这样： 1. 将 360° 的区间分得更细。从 0~359 变为一个 0 ~ n-1 的区间，将这个区间首尾相接，连成一个圆。 2. 当加入一台新的机器的时候，随机选择在圆周中撒 k 个点，代表这台机器的

LeetCode Intersection of Two Linked Lists 160. Intersection of Two Linked Lists [https://leetcode.com/problems/intersection-of-two-linked-lists/] LintCode-380.Intersection of Two Linked Lists [http://www.lintcode.com/en/problem/intersection-of-two-linked-lists/] Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1

LintCode Consistent Hashing Consistent Hashing [http://www.lintcode.com/en/problem/consistent-hashing/] 一般的数据库进行horizontal shard的方法是指，把 id 对 数据库服务器总数 n 取模，然后来得到他在哪台机器上。这种方法的缺点是，当数据继续增加，我们需要增加数据库服务器，将 n 变为 n+1 时，几乎所有的数据都要移动，这就造成了不 consistent。为了减少这种 naive 的 hash方法(%n) 带来的缺陷，出现了一种新的hash算法：一致性哈希的算法——Consistent Hashing。这种算法有很多种实现方式，这里我们来实现一种简单的 Consistent Hashing。 1. 将 id 对 360 取模，

LeetCode Combination Sum IV 377. Combination Sum IV [https://leetcode.com/problems/combination-sum-iv/] Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example: nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1)

LeetCode Combination Sum III 216. Combination Sum III [https://leetcode.com/problems/combination-sum-iii/] Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Input: k = 3, n

LeetCode Rearrange String k Distance Apart 358. Rearrange String k Distance Apart [https://leetcode.com/problems/rearrange-string-k-distance-apart/] Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to

LeetCode Split Array Largest Sum 410. Split Array Largest Sum [https://leetcode.com/problems/split-array-largest-sum/] Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length

LeetCode Graph Valid Tree 261. Graph Valid Tree [https://leetcode.com/problems/graph-valid-tree] Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. For example: Given n = 5

LeetCode Binary Tree Maximum Path Sum [124. Binary Tree Maximum Path Sum] Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and

LeetCode Sliding Window Maximum 239. Sliding Window Maximum [https://leetcode.com/problems/sliding-window-maximum/] Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window

LeetCode Text Justification 68. Text Justification [https://leetcode.com/problems/text-justification/] Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words

LeetCode Binary Tree Longest Consecutive Sequence Binary Tree Longest Consecutive Sequence [https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/] Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need

LeetCode Rotate Array 189. Rotate Array [https://leetcode.com/problems/rotate-array] Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to

LintCode Topological Sorting LintCode-127.Topological Sorting [http://www.lintcode.com/en/problem/topological-sorting/] Given an directed graph, a topological order of the graph nodes is defined as follow: For each directed edge A -> B in graph, A must before B in the order list. The first node in the order can be

LeetCode Lowest Common Ancestor of a Binary Tree 236. Lowest Common Ancestor of a Binary Tree [https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/] Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia [https://en.wikipedia.org/wiki/Lowest_common_ancestor]: “The lowest common ancestor

LeetCode H-Index II 275. H-Index II [https://leetcode.com/problems/h-index-ii/] Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? public class Solution { public int hIndex(int[] citations) { if (citations == null || citations.length == 0) { return 0; } int low = 0, high = citations.length -

Swift How to install Swift and Vapor on Ubuntu 20.04 This blog post just talk about how to install Swift and Vapor on Ubuntu 20.04. Let's install Swift first, 1. Install some dependencies including clang and Python components sudo apt install clang libpython2.7 libpython2.7-dev 2. Download Swift for Ubuntu 20.04 from [Download Swift]( https://www.swift.

SwiftUI How to create Tab Bar using TabView in SwiftUI In UIKit, we use the UITabBarController [https://developer.apple.com/documentation/uikit/uitabbarcontroller] to create the tab bar. While in SwiftUI we could use TabView instead. To create a user interface with tabs, we place views in a TabView and apply the tabItem(_:) modifier to the contents of each tab.

How to increase storage of Android Emulator I came across the INSTALL_FAILED_INSUFFICIENT_STORAGE error when I tried to install my app on the Android emulator. I found out that the storage of my Android emulator has run out. I have tried to update the Internal Storage of AVD directly but it did not work for

Upgrade blog from Ghost 0.11 to 4.x Previously this blog is using a pretty old version of Ghost 0.11 [https://github.com/TryGhost/Ghost/releases/tag/0.11.0], which was released back in 2016 and the tool Ghost-CLI [https://github.com/TryGhost/Ghost-CLI] was not released yet. This blog is hosting on the DigitalOcean [https:

Tesla 关于特斯拉4680电池 在2020年9月举办的特斯拉电池日上，特斯拉公布了自产新型4680电池的一些细节，与2170电池一样同样是圆柱形结构，但明显体积要大了不少，高度达到80MM, 直径达到46MM。 相比现在特斯拉使用的2170电芯，4680电芯容量上提升5倍，输出功率提高6倍，整车续航里程也将增加16%。新电池内部还采用了无极耳设计，有助于降低内阻，减少发热，从而为车辆带来更优秀的能耗表现以及充电性能。 使用4680电芯和新结构化的电池包(structural battery pack)之后，一台车上的电池外壳材料将会减少，使得制造成本进一步降低，每kWh的电池成本降低56%，每kWh所需的投资成本降低69%。 在2021第四季度财报会议上，特斯拉更新了一些关于4680电池的消息。 自从揭晓了4680电池后，特斯拉一直在加州的弗里蒙特(Fremont)工厂小量的试生产，然后计划在德州(Gigafactory Texas)和柏林(Gigafactory Berlin)超级工厂大规模量产4680电池。目前在Fremont的4680电池产能正在爬坡，预计年产能将达10GWh。德州超级工厂将会是

Swift How to implement Stack in Swift A stack is a last-in-first-out (LIFO) collection, which means the last thing we pushed is the first thing that gets popped off. If you used UINavigationController before, probably you used the method pushViewController(_:animated:) to push a view controller onto the navigation stack, and then used method popViewController(animated:) to

iOS How to use NSCache in Swift NSCache is a mutable dictionary that you could use to cache some data that are expensive to create, so it could improve your app performance. If memory is needed by other applications, it would remove some items from the cache, minimizing its memory footprint, then some values need to be

iOS How to implement recursiveDescription method in Swift You might find out that the private method of UIView recursiveDescription is useful, especially when you want to debug your view hierarchy. You could run the following command from the LLDB prompt in Xcode: po view.value(forKey: "recursiveDescription")! It would print the view hierarchy for you, all the views

iOS How to scroll UITableView to the top There is an instance method of UIScrollView called setContentOffset(_:animated:) [https://developer.apple.com/documentation/uikit/uiscrollview/1619400-setcontentoffset] . As UITableView is a subclass of UIScrollView, you could also use this method. If you need to scroll the table view to the top, you just call this method like below: tableView.