Press "Enter" to skip to content

Posts published in September 2018

花花酱 LeetCode 16. 3Sum Closest

Problem

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Solution: Sorting + Two Pointers

Similar to 花花酱 LeetCode 15. 3Sum

Time complexity: O(n^2)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 14. Longest Common Prefix

Problem

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

Solution: Brute Force

Time complexity: O(mk), where k the length of common prefix.

Space complexity: O(k)

C++

Java

Time complexity: (mk + k^2)

Python3

 

花花酱 LeetCode 13. Roman to Integer

Problem

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

accumulate the value of each letter.

If the value of current letter is greater than the previous one, deduct twice of the previous value.

e.g. IX, 1 + 10 – 2 * 1 = 9 instead of 1 + 10 = 11

Time complexity: O(n)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 223. Rectangle Area

Problem

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area

Example:

Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
Output: 45

Note:

Assume that the total area is never beyond the maximum possible value of int.

Solution:

area1 + area2 – overlapped area

Time complexity: O(1)

Space complexity: O(1)

C++

Java

Python3

花花酱 Amortized Analysis 均摊分析 SP7

Amortized Analysis

Amortized analysis can help us understand the actual cost of n operations on a data structure. Since some operations can be really fast e.g. O(1), but some operations can be really slow e.g. O(n). We could say the time complexity of that operation is O(n). However, it does not reflect the actual performance. And turns out, we can prove that certain operations have an amortized cost of O(1) while they can take O(n) in the worst case.

均摊分析可以帮助我们了解对一个数据结构进行n次操作的真实代价。对于相同的操作,有时候可以非常快,例如在O(1)时间内完成,而有时候则需要O(n)。我们当然可以说这个操作最坏情况下的时间复杂度是O(n),但是这并不能真实反映它的实际复杂度。通过均摊分析,我们可以证明,尽管有些操作在最坏情况下是O(n)的时间复杂度,但是均摊下来只需要O(1)。

 

Dynamic Array

dynamic array doubles its size when it’s full which could take O(i) time where i is the number of elements in the array. Otherwise just store the element which only cost O(1). We can use aggregate method to compute the amortized cost of dynamic array.

动态数组在容量满时将容量翻翻,这一步需要O(i)时间,i是当前数组中的元素个数。如果没有满,只需要将元素存储下来即可,只需要花费O(1)时间。我们可以使用聚合法来计算均摊成本。

((1 + 1) + (1 + 2) + (1) + (1 + 4) + (1) + (1) + (1) + (1+8) + … + (1+n)) / n, assuming n is 2^k.

= (1 * n + (1 + 2 + 4 + 8 + … + n)) / n = (n + 2n – 1) / n = 3. O(1)

C++

Output

 

Monotonic Stack

例子2: 单调栈

往单调栈push一个元素的时候,会删除上所有小于等于它的元素。这步操作在最优情况下是O(1)时间,如果它比栈顶元素要小。在最坏情况下是O(i)时间,栈上有i个元素,它比这i个元素都要大,所以一共要pop i次。

聚合法:

由于每个元素都会被push到栈上去1次,最多会被pop1次,所以总的操作数 <= 2n。 2n / n = 2 O(1)。

会计法:

每次push之前先存k块钱,k=2, 一块钱用于push自己,一块钱留着用于pop自己。

push的时候扣除1块钱,pop的时候再扣除1块钱。但不管怎样,我的账户上的钱永远>=0。这样我们可以说push的均摊成本是k=2。同样是O(1),尽管它的worst case是O(n)。

C++

Output

 

Related Problem