当前位置:网站首页>Leetcode skimming -- guess the size of numbers II 375 medium
Leetcode skimming -- guess the size of numbers II 375 medium
2022-07-25 20:49:00 【Fire breathing dragon and water arrow turtle】
Guess the size of the numbers II Ideas and source code
Guess the size of the numbers II The title of is shown in the figure below , This problem belongs to mathematics and dynamic programming , It mainly investigates the use of dynamic programming methods and the understanding of mathematical ideas of the topic . The title of this article, the author thought 2 Methods , They are memory search method and dynamic programming method , The memory search method uses Java Compiling , The dynamic programming method uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved by using the idea of memory search method , Initialize the parameters first , Then define a circular recursive function , The logic implemented inside the function is as follows : Pass in the lower and upper limit parameters , Judge whether the lower limit parameter is greater than or equal to the upper limit parameter , If so, go straight back 0 And end , Judge whether the corresponding array value of the current position is not 0, If yes, the current result of the array will be returned directly . Start traversing the loop , Find the maximum value of nearby elements , Then compare the historical value with the maximum value of nearby elements , Save the smaller value , Until the loop traversal ends and the historical minimum value is saved to the current element value . Recursively search with this logic until the end and finally return the result . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
static int N = 210;
static int[][] arr = new int[N][N];
public int getMoneyAmount(int n) {
return searchDFS(1, n);
}
int searchDFS(int a, int b) {
if (a >= b){
return 0;
}
if(arr[a][b] != 0){
return arr[a][b];
}
int res = 0x3f3f3f3f;
for (int jr = a; jr <= b; jr++) {
int ind = Math.max(searchDFS(a,jr-1), searchDFS(jr+1,b))+jr;
res = Math.min(res,ind);
}
arr[a][b] = res;
return res;
}
}

obviously , Our memory based search method works well , Dynamic programming can also be used to solve . First initialize a dynamic programming array and assign values , Then start traversal , Each value of dynamic programming is equal to the sum of the subscript and the value on the left , Then inside the area , Compare and assign values according to the rules of the transfer equation of the dynamic programming of the problem , Until the loop ends and returns the final result . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def getMoneyAmount(self, n: int) -> int:
dpArr = [[0] * (n + 1) for _ in range(n + 1)]
for ir in range(n - 1, 0, -1):
for jr in range(ir + 1, n + 1):
dpArr[ir][jr] = jr + dpArr[ir][jr - 1]
for kr in range (ir, jr):
dpArr[ir][jr] = min(dpArr[ir][jr], kr + max(dpArr[ir][kr-1],dpArr[kr+1][jr]))
return dpArr[1][n]

As a result Java The efficiency of version memory search method is good , and Python The speed of the version of dynamic programming method is relatively general , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- Fanoutexchange switch code tutorial
- Open source SPL enhances mangodb computing
- Leetcode-79: word search
- Vulnhub | dc: 5 | [actual combat]
- [tensorrt] dynamic batch reasoning
- process.env
- Clickhouse notes 02 -- installation test clickvisual
- Rand1 generates rand9
- Introduction to MySQL engine and InnoDB logical storage structure
- [MCU] 51 MCU burning those things
猜你喜欢

leetcode-6129:全 0 子数组的数目
![[noi simulation] string matching (suffix automata Sam, Mo team, block)](/img/db/3ccb00e78bba293acdae91ffa72a2c.png)
[noi simulation] string matching (suffix automata Sam, Mo team, block)

JMeter - interface test

Why did I choose to become a network engineer after graduating from weak current for 3 months

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

Recommended books | essentials of industrial digital transformation: methods and Practice

The database empties the table data and makes the primary key start from 1

Leetcode-79: word search

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

How to choose a microservice registration center?
随机推荐
Network protocol: TCP part2
When MySQL resets the root password and modifies the password, an error occurs. The password field does not exist
LeetCode刷题——猜数字大小II#375#Medium
A detailed explanation of SCP command
牛客-TOP101-BM38
Today's sleep quality record 75 points
Kubernetes advanced part learning notes
Leetcode-6130: designing digital container systems
毕业从事弱电3个月,我为什么会选择转行网络工程师
leetcode-6125:相等行列对
Explain in detail the principle of MySQL master-slave replication "suggestions collection"
Step num problem
2022.7.24-----leetcode.1184
两数,三数之和
Based on pexels image material API, sort out the material resource library
Recommended books | essentials of industrial digital transformation: methods and Practice
[leetcode] 28. Implement strstr ()
leetcode-6129:全 0 子数组的数目
Matlab---eeglab check EEG signal
Remote—实战