Sword finger Offer 14- I. cut rope
Here is a rope with length n. please cut the rope into m segments of integer length (M and N are integers, n > 1 and M > 1). The length of each segment of rope is recorded as k[0],k[1]...k[m-1]. What is the possible maximum product of k[0]*k[1]*...*k[m-1]? For example, when the length of the rope is 8, we cut it into three segments with lengths of 2, 3 and 3 respectively. At this time, the maximum product is 18.
Example 1:
input: 2 output: 1 explain: 2 = 1 + 1, 1 × 1 = 1
Example 2:
input: 10 output: 36 explain: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
Tips:
- 2 <= n <= 58
class Solution { public: int foo(int n, int m){ if(n == 1){ return 0; } int x = foo(n - 1, m); return (m + x) % n; } int lastRemaining(int n, int m) { return foo(n, m); } };
Algorithm idea
The problem is that n is split into as many 3 as possible (4 does not need to be split). For example, the best case of 5 is split into 3 and 2, 6 into 3 and 3, and 7 into 4 and 3... Here are some small proofs
By arithmetic geometric inequality
∑
i
=
1
n
a
i
n
≥
∏
i
=
1
n
a
i
n
{\sum_{i=1}^na_i\over n} \ge {\sqrt[n]{\prod_{i=1}^n ai}}
n∑i=1nai≥ni=1∏nai
If and only if ai are all equal, the maximum value is obtained by cumulative multiplication.
Let x be the value of ai and f be the result
f
(
x
)
=
x
n
x
f(x) = x^{\frac nx}
f(x)=xxn
Switch
l
n
f
=
n
x
l
n
x
lnf = \frac nxlnx
lnf=xnlnx
Two side derivation
f
′
f
=
−
n
l
n
x
x
2
+
n
x
2
\frac {f'}{f} = -\frac {nlnx}{x^2}+\frac n{x^2}
ff′=−x2nlnx+x2n
arrangement
f
′
=
n
x
n
x
x
2
(
1
−
l
n
x
)
f'=\frac {nx^{\frac nx}}{x^2}(1-lnx)
f′=x2nxxn(1−lnx)
Therefore, it can be obtained that when x = e (natural logarithm) f reaches the maximum value and X is an integer from monotonicity, 3 should be the optimal solution.
Sword finger Offer 57 - II. Continuous positive sequence with sum of s
Enter a positive integer target and output a sequence of consecutive positive integers with a sum of target (at least two numbers).
The numbers in the sequence are arranged from small to large, and different sequences are arranged from small to large according to the first number.
Example 1:
Input: target = 9 Output:[[2,3,4],[4,5]]
Example 2:
Input: target = 15 Output:[[1,2,3,4,5],[4,5,6],[7,8]]
Limitations:
- 1 <= target <= 10^5
class Solution { public: vector<vector<int>> ans; void ansPush(queue<int> q){ vector<int> ve; while(q.empty() == false){ ve.push_back(q.front()); q.pop(); } ans.push_back(ve); } vector<vector<int>> findContinuousSequence(int target) { queue<int> q; ans.clear(); int sum = 0, cur = 0; for(int i = 1; i <= target / 2 + 1; ++ i){ if(sum > target){ while(sum > target){ sum -= q.front(); q.pop(); } } if(sum == target){ ansPush(q); sum -= q.front(); q.pop(); } q.push(i); sum += i; } if(sum > target){ while(sum > target){ sum -= q.front(); q.pop(); } } if(sum == target){ ansPush(q); sum -= q.front(); q.pop(); } return ans; } };
Algorithm idea
This problem should be considered as a classic sliding window. Two pointers point to both sides of the window. If the size and sum of elements in the window are less than target, the right pointer moves to the right. If it is greater than target, the left pointer moves to the left until the whole array is traversed.
Sword finger Offer 62. The last remaining number in the circle
The N numbers 0,1, ···, n-1 are arranged in a circle, starting from the number 0, and the m-th number is deleted from the circle each time (counting from the next number after deletion). Find the last number left in the circle.
For example, the five numbers 0, 1, 2, 3 and 4 form a circle. Starting from the number 0, delete the third number each time, then the first four deleted numbers are 2, 0, 4 and 1 in turn, so the last remaining number is 3.
Example 1:
input: n = 5, m = 3 output: 3
Example 2:
input: n = 10, m = 17 output: 2
Limitations:
- 1 <= n <= 10^5
- 1 <= m <= 10^6
class Solution { public: int foo(int n, int m){ if(n == 1){ return 0; } int x = foo(n - 1, m); return (m + x) % n; } int lastRemaining(int n, int m) { return foo(n, m); } };
Algorithm idea
This problem uses a backtracking algorithm. The backtracking value is the number of bits of the target value in the current array. It is easy to know that as the array becomes shorter, there is only one element left in the array. This is the target value and the position is 0. Simply simulate the process of taking recursion
n = 5, k = 3 Extracted value position target(3)Location of(Original array) First round: 0 1 2 3 4 ==> 2 2 3 Second round: 0 1 3 4 0 1 3 4 ==> 0 0 2 Third round: 1 3 4 1 3 4 ==> 4 2 1 Fourth round: 1 3 1 3 ==> 1 0 1 Fifth round: 3 ==> 0 0
The restriction requires the relationship between f(n,m) and f(n - 1,m). N is the current array length and M is the m of the topic
Because m > n is actually traversing the array, M can be converted to m%n. We call the remaining array of f(n-1,m) layer as the upper array. Because it is reserved, the elements in the upper array will not be taken away. Therefore, it means that there are m%n elements in front of the upper array, that is, the position of target comes to (m+x)%n, so it can be pushed to the recursive array.
This problem is a Joseph Ring problem. It's really not simple
[sword finger Offer] series:
[sword finger Offer] stack
[sword finger Offer] linked list
[sword finger Offer] string
[sword finger Offer] search algorithm
[sword finger Offer] search algorithm (1)
[sword finger Offer] search and backtracking algorithm
[sword finger Offer] search and backtracking algorithm (1)
[sword finger Offer] dynamic programming
[sword finger Offer] dynamic programming (1)
[sword finger Offer] dynamic programming (2)
[sword finger Offer] double pointer
[sword finger Offer] double pointer (1)
[sword finger Offer] double pointer (2)
[sword finger Offer] search and backtracking algorithm (2)
[sword finger Offer] prime search and backtracking algorithm (3)
[sword finger Offer] sort
[sword finger Offer] sort (1)
[sword finger Offer] search and backtracking algorithm (4)
[sword finger Offer] search and backtracking algorithm (5)
[sword finger Offer] divide and conquer algorithm
[sword finger Offer] bit operation
[sword finger Offer] bit operation (1)
[sword finger Offer] mathematics