Skip to content

Commit cdec29b

Browse files
committed
https://leetcode.cn/problems/global-and-local-inversions
1 parent 0cc5a47 commit cdec29b

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/global-and-local-inversions
49+
4850
https://leetcode.cn/problems/maximum-units-on-a-truck
4951

5052
https://leetcode.cn/problems/split-array-with-same-average

global-and-local-inversions/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function isIdealPermutation(nums: number[]): boolean {
2+
return nums.every((v, i) => Math.abs(v - i) <= 1);
3+
}
4+
export default isIdealPermutation;

maximum-units-on-a-truck/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export default function maximumUnits(
2-
boxTypes: number[][],
3-
truckSize: number
4-
): number {
5-
boxTypes.sort((a, b) => b[1] - a[1]);
6-
let res = 0;
7-
for (const boxType of boxTypes) {
8-
const numberOfBoxes = boxType[0];
9-
const numberOfUnitsPerBox = boxType[1];
10-
if (numberOfBoxes < truckSize) {
11-
res += numberOfBoxes * numberOfUnitsPerBox;
12-
truckSize -= numberOfBoxes;
13-
} else {
14-
res += truckSize * numberOfUnitsPerBox;
15-
break;
16-
}
17-
}
18-
return res;
19-
}
1+
export default function maximumUnits(
2+
boxTypes: number[][],
3+
truckSize: number,
4+
): number {
5+
boxTypes.sort((a, b) => b[1] - a[1]);
6+
let res = 0;
7+
for (const boxType of boxTypes) {
8+
const numberOfBoxes = boxType[0];
9+
const numberOfUnitsPerBox = boxType[1];
10+
if (numberOfBoxes < truckSize) {
11+
res += numberOfBoxes * numberOfUnitsPerBox;
12+
truckSize -= numberOfBoxes;
13+
} else {
14+
res += truckSize * numberOfUnitsPerBox;
15+
break;
16+
}
17+
}
18+
return res;
19+
}

0 commit comments

Comments
 (0)