Skip to content

Commit 38ce0aa

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 0085d5b commit 38ce0aa

4 files changed

+189
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@
489489

490490
[310. Minimum Height Trees](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/310.%20Minimum%20Height%20Trees.md)
491491

492+
[312. Burst Balloons](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/312.%20Burst%20Balloons.md)
493+
494+
[313. Super Ugly Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/313.%20Super%20Ugly%20Number.md)
495+
496+
[315. Count of Smaller Numbers After Self](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/315.%20Count%20of%20Smaller%20Numbers%20After%20Self.md)
497+
492498

493499
## Algorithm(4th_Edition)
494500
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/312. Burst Balloons.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
## 312. Burst Balloons
2+
3+
### Question
4+
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
5+
6+
Find the maximum coins you can collect by bursting the balloons wisely.
7+
8+
Note:
9+
* You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
10+
* 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
11+
12+
```
13+
Example:
14+
15+
Input: [3,1,5,8]
16+
Output: 167
17+
Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
18+
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
19+
```
20+
21+
### Thinking:
22+
* Method 1:backtrace TLE
23+
```Java
24+
class Solution {
25+
private int max = 0;
26+
public int maxCoins(int[] nums) {
27+
int result = 0;
28+
if(nums == null || nums.length == 0) return 0;
29+
backtrace(nums, new boolean[nums.length], 0, 0);
30+
return this.max;
31+
}
32+
private void backtrace(int[] nums, boolean[] visited, int count, int sum){
33+
if(count == nums.length){
34+
this.max = Math.max(sum, max);
35+
return;
36+
}
37+
for(int i = 0; i < nums.length; i++){
38+
int temp = 0;
39+
if(visited[i]) continue;
40+
visited[i] = true;
41+
int left = 1;
42+
for(int j = i - 1; j >= 0; j--){
43+
if(!visited[j]){
44+
left = nums[j]; break;
45+
}
46+
}
47+
int right = 1;
48+
for(int j = i + 1; j < nums.length; j++){
49+
if(!visited[j]){
50+
right = nums[j]; break;
51+
}
52+
}
53+
backtrace(nums, visited, count + 1, sum + left * nums[i] * right);
54+
visited[i] = false;
55+
}
56+
}
57+
}
58+
```
59+
60+
* Method 2: DP, this question is a little bit difficult and worth having more conclusion.
61+
1. We create a new array with nums[len + 2], first and last index are filled with 1.
62+
2. we create a dp[i][j], i means the starting position and j means end position, both included. The final result is dp[1][len].
63+
3. we define the last burst balloon index as mid, we have dp[i][j] = max(dp[i][j], dp[i][mid - 1] + arr[i - 1] * arr[mid] * arr[j + 1] + dp[mid + 1][j]).
64+
* dp[i][mid - 1]: the sub result of left side of last burst ballon(mid)
65+
* arr[i - 1] * arr[mid] * arr[j + 1]: last one * remained left * remained right.
66+
* dp[mid + 1][j]: the sub result of right side of last burst ballon(mid)
67+
3. How to traversal the array:
68+
* dist: the distance from left index.
69+
* left: the left position.[1, len - dist + 1]
70+
* right: [left, left + dist - 1]
71+
* mid: last burst ballon. [left, right]
72+
```Java
73+
class Solution {
74+
public int maxCoins(int[] nums) {
75+
if(nums == null || nums.length == 0) return 0;
76+
int len = nums.length;
77+
int[][] dp = new int[len + 2][len + 2];
78+
int[] arr = new int[len + 2];
79+
for(int i = 1; i < len + 1; i++){
80+
arr[i] = nums[i - 1];
81+
dp[i][i] = arr[i];
82+
}
83+
arr[0] = arr[len + 1] = 1;
84+
for(int dist = 1; dist <= len; dist++){
85+
for(int left = 1; left <= len - dist + 1; left++){
86+
int right = left + dist - 1;
87+
for(int mid = left; mid <= right; mid ++){
88+
dp[left][right] = Math.max(dp[left][right], dp[left][mid - 1] + arr[left - 1] * arr[mid] * arr[right + 1] + dp[mid + 1 ][right]);
89+
}
90+
}
91+
}
92+
return dp[1][len];
93+
}
94+
}
95+
```
96+
97+
### Reference
98+
1. [312. Burst Balloons](https://blog.csdn.net/zjucor/article/details/56481930)
99+
100+

leetcode/313. Super Ugly Number.md

+26
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,30 @@ class Solution {
4242
return min;
4343
}
4444
}
45+
```
46+
47+
### Second time
48+
1. This question is very similar to ugly number, only difference is that the prime array is dynamic, so we need to add several for-loops for finding the minimum facter and updating the index and factors.
49+
```Java
50+
class Solution {
51+
public int nthSuperUglyNumber(int n, int[] primes) {
52+
int[] dp = new int[n];
53+
dp[0] = 1;
54+
int[] index = new int[primes.length];
55+
int[] factor = Arrays.copyOf(primes, primes.length);
56+
for(int i = 1; i < n; i++){
57+
int min = Integer.MAX_VALUE;
58+
for(int j = 0; j < primes.length; j++){
59+
min = Math.min(min, factor[j]);
60+
}
61+
dp[i] = min;
62+
for(int j = 0; j < index.length; j++){
63+
if(factor[j] == min){
64+
factor[j] = dp[++index[j]] * primes[j];
65+
}
66+
}
67+
}
68+
return dp[n - 1];
69+
}
70+
}
4571
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 315. Count of Smaller Numbers After Self
2+
3+
### Question
4+
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
5+
6+
```
7+
Example:
8+
9+
Input: [5,2,6,1]
10+
Output: [2,1,1,0]
11+
Explanation:
12+
To the right of 5 there are 2 smaller elements (2 and 1).
13+
To the right of 2 there is only 1 smaller element (1).
14+
To the right of 6 there is 1 smaller element (1).
15+
To the right of 1 there is 0 smaller element.
16+
```
17+
18+
### Thinking:
19+
* Method 1: Slow, beats 18.69%
20+
* find the right closed index whose value is same as current one.
21+
* only need to calculate the value between these two and add the result of that index.
22+
```Java
23+
class Solution {
24+
public List<Integer> countSmaller(int[] nums) {
25+
Map<Integer, Integer> map = new HashMap<>();
26+
int[] arr = new int[nums.length];
27+
for(int i = nums.length - 1; i>= 0; i--){
28+
if(map.containsKey(nums[i])){
29+
arr[i] = map.get(nums[i]);
30+
}
31+
else{
32+
arr[i] = -1;
33+
}
34+
map.put(nums[i], i);
35+
}
36+
int[] result = new int[nums.length];
37+
for(int i = nums.length - 1; i >= 0; i--){
38+
int count = 0;
39+
if(arr[i] == -1){
40+
for(int j = i; j < nums.length; j++){
41+
if(nums[i] > nums[j]) count++;
42+
}
43+
result[i] = count;
44+
}else{
45+
for(int j = i; j < arr[i]; j++){
46+
if(nums[i] > nums[j]) count++;
47+
}
48+
result[i] = count + result[arr[i]];
49+
}
50+
}
51+
List<Integer> res = new LinkedList<>();
52+
for(int n : result) res.add(n);
53+
return res;
54+
}
55+
}
56+
```
57+

0 commit comments

Comments
 (0)