Chapter 60 Bloomberg 2016

60.3 645. Set Mismatch

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

Example 1: Input: nums = [1,2,2,4], Output: [2,3]

https://leetcode.com/contest/leetcode-weekly-contest-42/problems/set-mismatch/

60.3.2 Algo 2: unordered_multiset

其实这题用unordered_multiset做也行. 但是注意删除元素的时候只能一个个删,所以不能用erase(int),必须用erase(iterator).

60.4 Missing Number in Unsorted Arithmetic Series

https://en.wikipedia.org/wiki/Arithmetic_progression

Ideas:

    1. Sort - \(O(NLogN)\)
    1. Gauss Sum - \(O(N)\) (overflow)
    1. XOR Trick - \(O(N)\)
    1. Geometric Sequence Sum -\(O(NK)\) (overflow)

60.4.2 Missing 2 number

In the following two sequences, A has two missing numbers: 3 and 9.

\[A = 1, 2, p, 4, 5, 6, 7, 8, p, 10\>\\B = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]

where placeholder \(p\) is any value except 3 and 9. \(p\) doesn’t matter as two same numbers have no impact in xor trick, and it can be nothing.

Then \[A \wedge B = 3\wedge 9 = 10=0b10\underline{1}0\]

According to the second from the last bit, we can split:

\(A\) into \(A_1=1,4,5,8\) and \(A_2=2,6,7,10\)

\(B\) into \(B_1=1,4,5,8,9\) and \(B_2=2,3,6,7,10\)

Then we can get missing numbers 3 and 9 using missingNumber(\[A_1,B_1\]) and missingNumber(\[A_2,B_2\]).

60.4.3 Missing k number

60.4.3.1 Case 1:

In the following two sequences, A has two missing numbers: x = 3 and y = 9. \[A = 1, 2, 4, 5, 6, 7, 8, 10\>\\B = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]

We have:

(1). \(A \wedge B =x \wedge y=10=R\)

(2). \(\sum A[i] - \sum B[i] =x+y=12=S1\)

(3). \(\sum A[i]^2 - \sum B[i]^2=x^2+y^2 =90=S2\)

  • Get \(x\) and \(y\) by solving simultaneous equations (1) and (2)

If \(x\wedge y= R, x+y=S, x, y \in \mathbb{Z}^+_0, x\neq y\) and \(R\) and \(S\) are constants, is there a unique solution?

  • Get \(x\) and \(y\) by solving simultaneous equations (2) and (3)

60.4.3.2 Case 2 (with placeholders):

In the following two sequences, A has two missing numbers: x = 3 and y = 9.

\[A = 1, 2, p, 4, 5, 6, 7, 8,p,10\>\\B = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]

(1). \(\sum A[i] - \sum B[i] =x+y-2p=S_1\)

(2). \(\sum A[i]^2 - \sum B[i]^2=x^2+y^2-2p^2 =S_2\)

(3). \(\sum A[i]^3 - \sum B[i]^3=x^3+y^3-2p^3 =S_3\)

We can get x, y, p by solving the three simultaneous equations.

60.5 max sum subarray

  • and print out the max sum subarry sequence

60.6 histogram query

  • pdf

  • cdf

60.8 deadlock

  • bank account transfer - how to avoid deadlock?

  • transfer(account from, account to, double amount)

transfer(a,b,100.)
transfer(b,a,100.)

Just sort from and to and make sure the locking order is the same in the two function calls.

60.11 Best Time to Buy and Sell Stock (Unlimited Times)

http://www.lintcode.com/en/problem/best-time-to-buy-and-sell-stock-ii/

Note:

  • You can sell and buy the same stock at one day, but transaction mean buy + sell.
  • For the result of \(adjacent\_difference(\cdot)\), the first element is not a difference!

http://en.cppreference.com/w/cpp/algorithm/adjacent_difference

60.13 Best Time to Buy and Sell Stock (At Most K Transactions)

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

https://www.youtube.com/watch?v=oDhu5uGq_ic