https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/submissions/596428450/
2062分
刺激
```js
var maxTotalFruits = function (fruits, startPos, k) {
const cache = new Map();
for (let index = 0; index < fruits.length; index++) {
const fruit = fruits;
cache.set(fruit, fruit);
}
const nextArr = new Array(k + 1);
const preArr = new Array(k + 1);
let ans = 0;
preArr = 0;
nextArr = 0;
for (let index = 1; index <= k; index++) {
nextArr =
(nextArr ?? 0) + (cache.get(startPos + index) ?? 0);
preArr =
(preArr ?? 0) + (cache.get(startPos - index) ?? 0);
}
ans = Math.max(nextArr, preArr);
let step = 1;
while (step * 2 < k) {
ans = Math.max(ans, preArr + nextArr);
ans = Math.max(ans, nextArr + preArr);
step++;
}
return ans+(cache.get(startPos)??0);
};
```
https://leetcode.cn/problems/maximize-win-from-two-segments/submissions/596544309/
2100分题
这个做了一下午
艰难
```js
function maximizeWin(prizePositions, k) {
const mx = new Array(prizePositions.length).fill(0);
let left = 0;
let ans = 0;
for (let right = 0; right < prizePositions.length; right++) {
const prizePosition = prizePositions;
while (prizePosition - k > prizePositions) {
left++;
}
mx = Math.max(mx ?? 0, right - left + 1);
ans = Math.max(ans,(mx??0)+right - left + 1);
}
return ans
}
```
https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-continuous/submissions/596560947/
过了
```js
var minOperations = function (nums) {
nums = nums.sort((a, b) => a - b);
let border = 1;
const n = nums.length - 1;
for (let index = 1; index < nums.length; index++) {
const num = nums;
if (num !== nums) {
nums = num;
border++;
}
}
let left = 0;
let ans = 0;
for (let index = 0; index < border; index++) {
const num = nums;
if (num - n > nums) {
left++;
}
ans = Math.max(ans, index - left + 1);
}
return nums.length-ans;
};
```
https://leetcode.cn/problems/shortest-and-lexicographically-smallest-beautiful-string/submissions/596571110/
过
```js
var shortestBeautifulSubstring = function (s, k) {
let left = 0,
num = 0;
let ans = "";
for (let index = 0; index < s.length; index++) {
const char = s;
if (char == "1") {
num++;
}
while (num > k) {
if (s == "1") {
num--;
}
left++;
}
while(s == "0"){
left++
}
if (num == k) {
const str = s.slice(left, index + 1);
if (ans == "") {
ans = str;
} else if (str.length<ans.length||(str.length==ans.length&&str < ans)) {
ans = str;
}
}
}
return ans;
};
```
https://leetcode.cn/problems/sort-array-by-parity-ii/submissions/596581859/
秒了
```js
var sortArrayByParityII = function (nums) {
const arr = [];
for (let index = 0; index < nums.length; index++) {
let num = nums;
if (index % 2 !== num % 2) {
for (let indey = index + 1; indey < nums.length; indey++) {
if (index % 2 == nums % 2) {
const temp = num;
num = nums;
nums = temp;
break;
}
}
}
arr.push(num);
}
return arr;
};
```
https://leetcode.cn/problems/sort-array-by-parity/submissions/596582284/
过
```js
var sortArrayByParity = function (nums) {
const arr = [];
let noMap = false;
for (let index = 0; index < nums.length; index++) {
let num = nums;
let isFind = false;
if (!noMap) {
for (let indey = index + 1; indey < nums.length; indey++) {
if (nums % 2 == 0) {
const temp = num;
num = nums;
nums = temp;
isFind = true;
break;
}
}
}
if (isFind == false) {
noMap = true;
}
arr.push(num);
}
return arr;
};
```
https://leetcode.cn/problems/replace-the-substring-for-balanced-string/submissions/596587043/
过
```js
var balancedString = function (s) {
const arr = {
Q: 0,
W: 0,
E: 0,
R: 0,
};
const average = s.length / 4;
for (let index = 0; index < s.length; index++) {
const char = s;
arr++;
}
if (arr.Q == arr.W && arr.Q == arr.W && arr.Q == arr.E && arr.Q == arr.R) {
return 0;
}
let left = 0;
let ans=Number.MAX_SAFE_INTEGER
for (let index = 0; index < s.length; index++) {
const char = s;
arr--;
while(arr.Q <=average && arr.W<=average && arr.E <=average&& arr.R <=average){
ans=Math.min(ans,index-left+1);
arr]++;
left++
}
}
return ans
};
```
https://leetcode.cn/problems/minimum-size-subarray-in-infinite-array/submissions/596606950/
秒
```js
var minSizeSubarray = function (nums, target) {
let right = 0,
left = 0;
let total = 0;
ans = Number.MAX_SAFE_INTEGER;
let allTotal = 0;
for (let index = 0; index < nums.length; index++) {
allTotal += nums;
}
const len = Math.floor(target / allTotal);
target = target % allTotal;
while (left < nums.length) {
total += nums;
while (total > target) {
total -= nums;
left++;
}
if (total == target) {
ans = Math.min(ans, right - left + 1);
}
right++;
}
return ans == Number.MAX_SAFE_INTEGER ? -1 : ans+len*nums.length;
};
```
https://leetcode.cn/problems/longest-nice-substring/submissions/596608781/
秒了
```js
var longestNiceSubstring = function (s) {
let ans = [-1, -1];
for (let index = 0; index < s.length; index++) {
for (let indey = index + 1; indey < s.length; indey++) {
let char = new Array(128).fill(0);
for (let indez = index; indez <= indey; indez++) {
char.charCodeAt()]++;
}
let isSuccess = true;
for (let index = 65; index <= 90; index++) {
if ((char!=0) !== (char!=0)) {
isSuccess = false;
break;
}
}
if (isSuccess && ans - ans+1 < indey - index + 1) {
ans=
}
}
}
return ans==-1?"":s.slice(ans,ans+1)
};
```
https://leetcode.cn/problems/smallest-range-covering-elements-from-k-lists/submissions/596772866/
过了
```js
class Heap {
constructor(comparator = (a, b) => a - b) {
this.heap = [];
this.comparator = comparator;
}
getParentIndex(index) {
return Math.floor((index - 1) / 2);
}
getLeftChildIndex(index) {
return 2 * index + 1;
}
getRightChildIndex(index) {
return 2 * index + 2;
}
swap(index1, index2) {
const temp = this.heap;
this.heap = this.heap;
this.heap = temp;
}
insert(value) {
this.heap.push(value);
this.heapifyUp();
}
heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = this.getParentIndex(index);
if (this.comparator(this.heap, this.heap) < 0) {
this.swap(index, parentIndex);
index = parentIndex;
} else {
break;
}
}
}
extractMin() {
if (this.heap.length === 0) {
return null;
}
if (this.heap.length === 1) {
return this.heap.pop();
}
const min = this.heap;
this.heap = this.heap.pop();
this.heapifyDown(0);
return min;
}
getMin() {
if (this.heap.length === 0) {
return null;
}
return this.heap;
}
heapifyDown(index) {
let smallest = index;
const leftChildIndex = this.getLeftChildIndex(index);
const rightChildIndex = this.getRightChildIndex(index);
if (
leftChildIndex < this.heap.length &&
this.comparator(this.heap, this.heap) < 0
) {
smallest = leftChildIndex;
}
if (
rightChildIndex < this.heap.length &&
this.comparator(this.heap, this.heap) < 0
) {
smallest = rightChildIndex;
}
if (smallest !== index) {
this.swap(index, smallest);
this.heapifyDown(smallest);
}
}
peek() {
return this.heap.length > 0 ? this.heap : null;
}
size() {
return this.heap.length;
}
}
/**
* @param {number[][]} nums
* @Return {number[]}
*/
var smallestRange = function (nums) {
const heap = new Heap((a, b) => a - b);
let max = Number.MIN_SAFE_INTEGER;
for (let index = 0; index < nums.length; index++) {
const arr = nums;
max = Math.max(max, arr);
heap.insert(, index, 0]);
}
let ansL = heap.getMin(); // 第一个合法区间的左端点
let ansR = max; // 第一个合法区间的右端点
while (true) {
let min = heap.getMin();
if (min + 1 >= nums].length) {
break;
}
min = heap.extractMin();
const nextNum = nums] + 1];
heap.insert(, min + 1]);
max = Math.max(max, nextNum);
min = heap.getMin();
if (ansR - ansL > max - min) {
ansR = max;
ansL = min;
}
}
return ;
};
```