# LeetCode Weekly Contest 38

https://leetcode.com/contest/leetcode-weekly-contest-38/

## Maximum Product of Three Numbers

#### Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]

Output: 6

Example 2:

Input: [1,2,3,4]

Output: 24

Note:

1. The length of the given array will be in range $[3,10^4]$ and all elements are in the range $[-1000, 1000]$.
2. Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer.

## Course Schedule III

#### Course Schedule III

There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st day.

Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken.

Example:
Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]

Output: 3

Explanation:

There’re totally 4 courses, but you can take 3 courses at most:

First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.

Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day.

Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.

The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

Note:

1. The integer 1 <= d, t, n <= 10,000.
2. You can’t take two courses simultaneously.

## K Inverse Pairs Array

#### K Inverse Pairs Array

Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs.

We define an inverse pair as following: For ith and jth element in the array, if i < jand a[i] > a[j] then it’s an inverse pair; Otherwise, it’s not.

Since the answer may very large, the answer should be modulo 10^9 + 7.

Example 1:
Input: n = 3, k = 0
Output: 1
Explanation:
Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pair.

Example 2:
Input: n = 3, k = 1
Output: 2
Explanation:
The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.

Note:
The integer n is in the range [1, 1000] and k is in the range [0, 1000].

……

$$f(n+1,k+1)=f(n,k+1)+f(n+1,k)-f(n,n-k)$$

• $n=0$ 时函数值都要置为0
• $k=0$ 时函数值规定为1
• $k>n(n-1)/2$时函数值为0 取等号时为1
• 刚刚说到的函数参数为负的问题