中级农民
- 积分
- 118
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-11-5
- 最后登录
- 1970-1-1
|
Spet 17th 2018 --- 5 Problems
Weekly Contest 102
## 904. Fruit Into Baskets (Medium)
### Corner cases
1. Input maybe empty.
2. Input may have only one tree.
3. Input may have only one kind of trees.
### Bugs
1. [WA] After you clear one basket and put new fruit in, the new length should be the length of the subarray starting from last position of the eliminated fruit + 1 to the position now.
### Solution
#### Best Solution
The algorithm seems like LRU. Use pointers to record the two kind of fruits in your baskets and their recently visited position. When you facing a new tree, first check whether you already got this kind of fruit in you basket. If the answer is yes, increase your length and update recently visited position of this kind (basket). If the answer is no, clear the basket with the oldest recently visited position and put the new fruit in.
- Time complexity: O(n)
- Space complexity: O(1)
## 905. Sort Array By Parity (Easy)
### Corner Cases
1. Input array is empty.
2. All elements are odd or even.
3. Only one element in array.
### Bugs
1. Nope.
### Solution
#### Best Solution
One pointer i forwards and another pointer j backwards. Pointer i stops when it finds an odd number and pointer j stop when it find an even number. Swap elements at i and j. Continue.
- Time complexity: O(n)
- Space complexity: O(1)
## 906. Super Palindromes (Hard)
### Corner Cases
1. Use [L, L] on some specific number to test your program.
2. [1, 10^18 - 1] to test your time complexity.
### Bugs
1. [WA] `StringBuilder.reverse()` change the `StringBuilder` itself other than generate a new one.
2. [WA] The same as `StringBuilder.append()`.
3. Several trasfer functions between `String` and `Integer` does not support `StringBuilder` or `Long`.
### Solution
#### Best Solution
Enumerate a Integer, make a palindrome from it, get check wether the square of that palindrom is also a palindrome and falls into the right range. The range of square is [1, 10^18], so it would be enough for us to enumerate to 10^5.
Also, Long type would be enough to store those two inputs.
The question would be a nice practice to check if you are clear about the conversion between number, string and stringbuilder.
## 907. Sum of Subarray Minimus (Hard)
### Corner Cases
1. Input is empty.
2. Only one element in input.
3. All the elements in input are the same.
### Bugs
1. [WA] First time try to find out all `left[]` and `right[]` by one scan without any data structures.
### Solutions
#### Best Solution - Stack
Actually, the problem is trying to find the range for each number, for every subarries in this range, the number will be the subarray's minimum.
Then, the question is transferred into finding such `left[]` and `right[]` standing for the left and right side of each number's range, with the initial value the position of the number itself.
We utilize a stack here, scan the array forwards. Before you push a new element in, pop out all elements greated than it. The farthest `left[]` of the elements it poped out will be its `left[]`.
One special occasion is if there are duplicates in the array. The subarray including both of them should only be calculated once. The solution is: in one direction, we pop out all elements greater than new element in the stack; in the other direction, we pop out all elements greater than or equal to the new element.
## 786. Kth Smallest Prime Fraction (Hard)
This is a classic interview question from PonyAI.
### Corner Cases
1. Cases like [1, 3000] is strong enough.
### Bugs
1. Eps related operations are very delicated.
2. `Math.floor()` and `Math.ceil()` operations.
### Solutions
#### 1. Sort
N prime numbers in the array, then there will be at most n^2 fractions. Sort all of them and pick out the kth smallest one.
- Time complexity: o(n^2 log(n^2))
- Space complexity: o(n^2)
### HeapSort
Firstly add the smallest fraction into heap. Everytime you pop the top out of heap, push fractions next to it into the heap. Repeat this operation for K times.
- Time complexity: O(K log(n^2))
- Space complexity: O(n^2)
### Binary Search + TreeSet
Use binary search to enumerate the fractions from [0, 1], check if there is exactly K fractions from the array less than it (O(n) for each check). If so, find the greatest fractions less than it from the array. Such a operation could be done easily with the help of `TreeSet.floor()`.
- Time complexity: O(n log(1 / eps)) (1 / eps is nearly 10^7)
- Space complexity: O(n ^ 2)
|
|