forked from sl1673495/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/sl1673495/leetcode-javasc…
- Loading branch information
Showing
8 changed files
with
623 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters