[用JS來寫演算法和了解資料結構] Day4 Data Structures - 陣列 Array - Leetcode #905, #561

[用JS來寫演算法和了解資料結構] Day4 Data Structures - 陣列 Array - Leetcode #905, #561

Leetcode 陣列題型

這篇會跟著 Hannah 的鐵人賽刷的題目,會慢慢再更新其他題目。

leetcode #905. Sort Array By Parity

按奇偶排序陣列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Given an array A of non-negative integers, //正整數的陣列
return an array consisting of all the even elements of A,
followed by all the odd elements of A.
// 回傳陣列,前面是偶數後面是奇數。

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Note:

1 <= A.length <= 5000
0 <= A[i] <= 5000
*/

/**
* @param {number[]} A
* @return {number[]}
*/
var sortArrayByParity = function (A) {};

這題我的想法是把偶、奇數分別丟到兩個陣列,最後再合併。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var sortArrayByParity = function (A) {
let evenAry = [];
let oddAry = [];
A.forEach(n => {
if (n % 2 === 0) {
evenAry.push(n);
} else {
oddAry.push(n);
}
});
return [...evenAry, ...oddAry];
};

sortArrayByParity([3, 1, 2, 4]);

一行 code.. 但跑兩次 filter 應該是 n^2

1
2
3
4
5
var sortArrayByParity = function (A) {
return A.filter(n => n % 2 == 0).concat(A.filter(n => n % 2 == 1));
};

sortArrayByParity([3, 1, 2, 4]);

偶數從前面插入 unshift ,奇數才後面 push

1
2
3
4
5
6
7
8
9
var sortArrayByParity = function (A) {
let ary = [];
A.forEach(item => {
item % 2 == 0 ? ary.unshift(item) : ary.push(item);
});
return ary;
};

sortArrayByParity([3, 1, 2, 4]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var sortArrayByParity = function(nums) {
if (nums.length === 1) return nums;
let left = 0;
let right = nums.length - 1;

while (left < right) {
if (nums[left] % 2 === 1 && nums[right] % 2 === 0) {
[nums[left], nums[right]] = [nums[right], nums[left]];
}
if (nums[left] % 2 === 0) left++;
if (nums[right] % 2 === 1) right--;
}
return nums;
};
sortArrayByParity([3, 1, 2, 4]);

leetcode #561. Array Partition I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.

// 給一個 有2倍數量 值的陣列nums
// 比較兩兩成對後的最小值的加總,再比較各種可能中的找出最大值
// 回傳找出的最大值

Example 1:

Input: nums = [1,4,3,2]
Output: 4
Explanation: All possible pairings (ignoring the ordering of elements) are:
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
So the maximum possible sum is 4.

Example 2:

Input: nums = [6,2,6,5,1,2]
Output: 9
Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6).
min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.

Constraints:

1 <= n <= 104
nums.length == 2 * n
-104 <= nums[i] <= 104 // 有可能有負值

/**
* @param {number[]} nums
* @return {number}
*/
var arrayPairSum = function (nums) {};

小排到大後,一對中的較小值 (因為已經排序好了所以就是一對中的第一個) 的 加總

1
2
3
4
5
6
7
8
9
10
11
var arrayPairSum = function (nums) {
let len = nums.length;
let sum = 0;
nums = nums.sort((a, b) => a - b);
for (let i = 0; i < len; i += 2) {
sum += nums[i];
}
return sum;
};

arrayPairSum([6, 2, 6, 5, 1, 2, 7, 9]); // 1+2+6+7 = 16

學習與參考資料

[用JS來寫演算法和了解資料結構] Day4 Data Structures - 陣列 Array - Leetcode #905, #561

https://kaiyuncheng.github.io/2023/09/29/leetcodeDay4/

Author

KaiYun Cheng

Posted on

2023-09-29

Updated on

2024-04-14

Licensed under

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×