Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1673495 committed May 30, 2020
2 parents 7f309d4 + 6e8944c commit 9dc8f00
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 3 deletions.
443 changes: 443 additions & 0 deletions report.20200527.011151.36368.0.001.json

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions 动态规划/可获得的最大点数-1423.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 力扣超时 卡在第26个用例
let maxScore = function (cardPoints, k) {
let n = cardPoints.length

let prevTimeChunk = []
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
if (!prevTimeChunk[i]) {
prevTimeChunk[i] = []
}
prevTimeChunk[i][j] = 0
}
}

let currentTimeChunk = []

for (let time = 1; time <= k; time++) {
for (let i = n - 1; i >= 0; i--) {
for (let j = i; j < n; j++) {
if (!currentTimeChunk[i]) {
currentTimeChunk[i] = []
}

// 只剩一个可选 有次数的情况下就选这一项 否则为0
if (i === j) {
currentTimeChunk[i][j] = time > 0 ? cardPoints[i] : 0
}

let pickHead = cardPoints[i]
let pickTail = cardPoints[j]

currentTimeChunk[i][j] = Math.max(
pickHead + (prevTimeChunk[i + 1] ? prevTimeChunk[i + 1][j] || 0 : 0),
pickTail + (prevTimeChunk[i][j - 1] || 0)
)
}
}
prevTimeChunk = currentTimeChunk
currentTimeChunk = []
}

return prevTimeChunk[0][n - 1]
}

console.log(maxScore([1,79,80,1,1,1,200,1], 3))
32 changes: 32 additions & 0 deletions 可被K整除的子数组.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 暴力解 超时了
* @param {number[]} A
* @param {number} K
* @return {number}
*/
var subarraysDivByK = function (A, K) {
let prevPrefix = []
let currentPrefix = []
let count = 0
for (let i = A.length - 1; i >= 0; i--) {
let num = A[i]
judge(num)
for (let prev of prevPrefix) {
let sum = prev + num
judge(sum)
}
prevPrefix = currentPrefix
currentPrefix = []
}

function judge(num) {
if (num % K === 0 || num === 0) {
count++
}
currentPrefix.push(num)
}

return count
}

console.log(subarraysDivByK([4, 5, 0, -2, -3, 1], 5))
21 changes: 19 additions & 2 deletions 工具/排序速度.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const glob = require("glob")
const path = require("path")
const swap = require("./交换")
const random = require("./随机值")

function sortTest(sortFns, source, desc) {
console.log(desc)
Expand All @@ -14,7 +15,14 @@ function sortTest(sortFns, source, desc) {
const copy = source.slice()
const start = new Date().getTime()

fn(copy)
try {
fn(copy)
} catch (e) {
return (table[fn.sortName] = {
结果: "程序异常",
原因: e.message,
})
}

const end = new Date().getTime()
const time = end - start
Expand All @@ -36,7 +44,6 @@ function getRandomArray(count) {
for (let i = 0; i < count; i++) {
arr.push(Math.floor(i * Math.random() * 10))
}

return arr
}

Expand All @@ -55,6 +62,14 @@ function getNearlyArray(count, swapTime) {
return arr
}

function getRangedArray(count, min, max) {
const arr = []
for (let i = 0; i < count; i++) {
arr.push(random(min, max))
}
return arr
}

function isSorted(target, source) {
return (
target.toString() ===
Expand All @@ -71,4 +86,6 @@ glob("排序/*.js", (err, result) => {
.map((p) => require(path.resolve(p)))
.filter(Boolean)
sortTest(sortFunctions, () => getRandomArray(10000), "普通数组排序")
sortTest(sortFunctions, () => getNearlyArray(10000), "近似数组排序")
sortTest(sortFunctions, () => getRangedArray(10000), "大量重复值元素排序")
})
5 changes: 5 additions & 0 deletions 工具/随机值.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function random(low, high) {
return Math.round(Math.random() * (high - low)) + low
}

module.exports = random
51 changes: 51 additions & 0 deletions 排序/快速排序-三路.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const swap = require("../工具/交换")
const random = require("../工具/随机值")

/**
* 三路快速排序
* 将 arr[l...r] 分为 < v, === v, > v三部分
* 之后递归的对 < v, > v 两部分三路快排
* @param {number[]} arr
*/
function quickSort(arr) {
_quickSort(arr, 0, arr.length - 1)
return arr
}

/**
* 对 arr[l...r] 部分进行快速排序
* @param {number[]} arr
* @param {number} l 左边界
* @param {number} r 右边界
*/
function _quickSort(arr, l, r) {
if (l >= r) {
return
}
let [p, q] = partition(arr, l, r)
_quickSort(arr, l, p)
_quickSort(arr, q, r)
}

/**
* 对 arr[l...r] 部分进行快速排序
* @param {number[]} arr
* @param {number} l 左边界
* @param {number} r 右边界
* @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r]
*/
function partition(arr, left, right) {
// 取一个基准值 取随机值
let rand = random(left, right)
swap(arr, left, rand)
let pivot = arr[left]

// 三路 注意看注释里的区间
let lt = left // arr[left + 1...lt] < v
let gt = right + 1 // arr[gt...r] > v
let index = left + 1 // arr[lt + 1...index] === v
}

quickSort.sortName = "快速排序(三路)"

module.exports = quickSort
24 changes: 24 additions & 0 deletions 排序/快速排序-空间.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function quickSort(arr) {
if (arr.length === 1 || arr.length === 0) {
return arr
}
const left = []

const right = []
const ref = arr[0]

for (let i = 1; i < arr.length; i++) {
let num = arr[i]
if (num < ref) {
left.push(num)
} else {
right.push(num)
}
}

return [...quickSort(left), ref, ...quickSort(right)]
}

quickSort.sortName = "快速排序(空间版)"

module.exports = quickSort
5 changes: 4 additions & 1 deletion 排序/快速排序.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const swap = require("../工具/交换")
const random = require("../工具/随机值")

/**
*
Expand Down Expand Up @@ -32,7 +33,9 @@ function _quickSort(arr, l, r) {
* @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r]
*/
function partition(arr, left, right) {
// 取一个基准值 取第一项
// 取一个基准值 取随机值
let rand = random(left, right)
swap(arr, left, rand)
let pivot = arr[left]

// arr[left+1...index] < pivot, arr[index+1...i) > pivot
Expand Down

0 comments on commit 9dc8f00

Please sign in to comment.