# LeetCode 381. Insert Delete GetRandom O1 Duplicates allowed

Design a data structure that supports all following operations in average O(1) time.

Note: Duplicate elements are allowed.
insert(val): Inserts an item val to the collection.
remove(val): Removes an item val from the collection if present.
getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.

# LeetCode 380. Insert Delete GetRandom O1

Design a data structure that supports all following operations in average O(1) time.

insert(val): Inserts an item val to the set if not already present.
remove(val): Removes an item val from the set if present.
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

# LeetCode 378. Kth Smallest Element in a Sorted Matrix

Description
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,

return 13.

# LeetCode 377. 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.

# LeetCode 376. Wiggle Subsequence

Description
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.

LD 是个众所周知的糕富帅，从不把买切糕的钱当钱，只求量和品质，不惜价。但他还是会纠结买哪些切糕好，现假设切糕的价钱有 5 种分别为每两 2，2.5，3，3.5，4（百RMB），并且数量都假设为无限。现在告诉你 LD 一共有几百 RMB。请你告诉 LD 他一共有几种买切糕的方案（钱可以不花光，但一定要买，不花钱 LD 会不开心的）。

# LeetCode 375. Guess Number Higher or Lower II

Description
We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I’ll tell you whether the number I picked is higher or lower.

However, when you guess a particular number x, and you guess wrong, you pay \$x. You win the game when you guess the number I picked.

Example:

n = 10, I pick 8.

First round: You guess 5, I tell you that it’s higher. You pay 5. Second round: You guess 7, I tell you that it’s higher. You pay5.Secondround:Youguess7,Itellyouthatit

shigher.Youpay7.
Third round: You guess 9, I tell you that it’s lower. You pay \$9.

Game over. 8 is the number I picked.

You end up paying 5 +5+7 + 9 =9=21.
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.

# LeetCode 374. Guess Number Higher or Lower

Description
We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I’ll tell you whether the number is higher or lower.

You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example :

Input: n = 10, pick = 6
Output: 6

# LeetCode 373. Find K Pairs with Smallest Sums

Description
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.

Define a pair (u,v) which consists of one element from the first array and one element from the second array.

Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.