//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)

最新文章

  1. MIP改造常见问题二十问
  2. sql Lloader
  3. 初识less
  4. IT学习网站集结
  5. iOS:后台定位并实时向服务器发送位置
  6. merge into的用法
  7. DataSet中的relation
  8. js的时间操作方法
  9. STM32之定时器
  10. TCL 双引号和花括号的区别
  11. iOSNSDate的相关操作
  12. Linux下查看alert日志文件的两种方法
  13. Linux中find的使用(转)
  14. LeetCode 112. Path Sum 二叉树的路径和 C++
  15. Vue(基础四)_总结五种父子组件之间的通信方式
  16. (最大上升子序列)Monkey and Banana -- hdu -- 1069
  17. 替换NSUserDefaults的方案
  18. Delphi XE开发 Android 开机自动启动
  19. 【紫书】Undraw the Trees UVA - 10562 递归,字符串
  20. avg(xxxxxx)什么时候能独自出现?

热门文章

  1. [平台建设] Spark任务的诊断调优
  2. Shell调试Debug的三种方式
  3. postgresql使用pg_dump工具进行数据库迁移
  4. 在使用django admin的后台搜索时报错
  5. PyCharm - 关联mysql失败 - Server returns invalid timezone. Go to &#39;Advanced&#39; tab and set &#39;serverTimezone&#39; property manually.
  6. ch01系统基础信息模块详解
  7. spring boot热部署 -- 实现 后端java热更新 -- 详细操作 【idea 的 JRebel破解】
  8. let var const 区别
  9. Go语言系列之time
  10. Maven+ajax+SSM实现新增