vue3 最长递增子序列 diff优化
2024-10-19 20:35:53
//vue3优化版(回头我会完善下算法思路)
function getSequence(arr) {
const p = arr.slice()
const result = [0]
let i, j, u, v, c
const len = arr.length
for (i = 0; i < len; i++) {
const arrI = arr[i]
if (arrI !== 0) {
j = result[result.length - 1]
if (arr[j] < arrI) {
p[i] = j
result.push(i)
continue
}
u = 0
v = result.length - 1
while (u < v) {
c = ((u + v) / 2) | 0
if (arr[result[c]] < arrI) {
u = c + 1
} else {
v = c
}
}
if (arrI < arr[result[u]]) {
if (u > 0) {
p[i] = result[u - 1]
}
result[u] = i
}
}
}
u = result.length
v = result[u - 1]
while (u-- > 0) {
result[u] = v
v = p[v]
}
return result
}
console.log(getSequence([10, 9, 2, 5, 3, 7, 101, 18]));
//算法原型——基础算法版
//Objective is to find the longest increasing subsequence in an array.
let nums = [10,9,2,5,3,7,101,18]
//O(n^2) solution that uses dynamic programming to figure out if we want the
//element in the subsequence or not.
if (nums.length == 0) {
return 0
}
//Every element initially starts with a subsequence of length 1
let dp = new Array(nums.length).fill(1)
//Use a nested iterator to compare all pairs of elements in the array
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
//If nums[i] = 5 && nums[j] = 2, then we can choose to add
//the previous subsequence to the current one
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1)
}
}
}
return Math.max(...dp)
最新文章
- MIP改造常见问题二十问
- sql Lloader
- 初识less
- IT学习网站集结
- iOS:后台定位并实时向服务器发送位置
- merge into的用法
- DataSet中的relation
- js的时间操作方法
- STM32之定时器
- TCL 双引号和花括号的区别
- iOSNSDate的相关操作
- Linux下查看alert日志文件的两种方法
- Linux中find的使用(转)
- LeetCode 112. Path Sum 二叉树的路径和 C++
- Vue(基础四)_总结五种父子组件之间的通信方式
- (最大上升子序列)Monkey and Banana -- hdu -- 1069
- 替换NSUserDefaults的方案
- Delphi XE开发 Android 开机自动启动
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
- avg(xxxxxx)什么时候能独自出现?
热门文章
- [平台建设] Spark任务的诊断调优
- Shell调试Debug的三种方式
- postgresql使用pg_dump工具进行数据库迁移
- 在使用django admin的后台搜索时报错
- PyCharm - 关联mysql失败 - Server returns invalid timezone. Go to &#39;Advanced&#39; tab and set &#39;serverTimezone&#39; property manually.
- ch01系统基础信息模块详解
- spring boot热部署 -- 实现 后端java热更新 -- 详细操作 【idea 的 JRebel破解】
- let var const 区别
- Go语言系列之time
- Maven+ajax+SSM实现新增