Skip to content

Commit 7d73623

Browse files
committed
DEV 25, 2018
1 parent 9532601 commit 7d73623

3 files changed

+145
-6
lines changed

561__easy__array-partition-i.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
/**
2-
* @param
3-
* @return
4-
*/
1+
/*
2+
3+
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
4+
5+
Example 1:
6+
Input: [1,4,3,2]
7+
8+
Output: 4
9+
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
10+
Note:
11+
n is a positive integer, which is in the range of [1, 10000].
12+
All the integers in the array will be in the range of [-10000, 10000].
13+
14+
*/
515

616
/**
7-
*
8-
*
17+
* O(nlogn) time
18+
* O(n) space if merge sort, O(1) if quick sort.
19+
* ALSO NOTE: if use counting sort, time and space will be O(n+k) where k is the range of input
20+
* Runtime: 120 ms, faster than 97.91% of JavaScript
921
*/
1022
var arrayPairSum = function(nums) {
1123
nums = nums.sort((a, b) => a - b);
@@ -15,3 +27,29 @@ var arrayPairSum = function(nums) {
1527
}
1628
return res;
1729
};
30+
31+
/**
32+
* Use bucket sort
33+
* time O(n)
34+
* space O(n)
35+
* Runtime: 76 ms, faster than 100.00%
36+
*/
37+
var arrayPairSum = function(nums) {
38+
let arr = new Array(20001).fill(0); // num range of [-10000, 10000]
39+
let base = 10000;
40+
41+
for (let i = 0; i < nums.length; i++) {
42+
arr[nums[i] + base]++;
43+
}
44+
45+
let sum = 0;
46+
let adding = true;
47+
for (let i = 0; i < arr.length; i++) {
48+
while (arr[i] > 0) {
49+
if (adding) sum += i - base;
50+
adding = !adding;
51+
arr[i]--;
52+
}
53+
}
54+
return sum;
55+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
3+
Let's call an array A a mountain if the following properties hold:
4+
5+
A.length >= 3
6+
There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
7+
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
8+
9+
Example 1:
10+
11+
Input: [0,1,0]
12+
Output: 1
13+
Example 2:
14+
15+
Input: [0,2,1,0]
16+
Output: 1
17+
Note:
18+
19+
3 <= A.length <= 10000
20+
0 <= A[i] <= 10^6
21+
A is a mountain, as defined above.
22+
23+
*/
24+
25+
/**
26+
* Binary search
27+
* O(n) time
28+
* O(1) space
29+
*
30+
* 20m
31+
*/
32+
var peakIndexInMountainArray = function(A) {
33+
let left = 0;
34+
let right = A.length - 1;
35+
let curr = ~~(A.length / 2);
36+
37+
while (left < right) {
38+
if (A[curr - 1] < A[curr] && A[curr] > A[curr + 1]) {
39+
// low < high > low, peak found!
40+
return curr;
41+
} else if (A[curr - 1] < A[curr] && A[curr] < A[curr + 1]) {
42+
// ex: 5 - 6 - 7, up-trend so peak at right hand side
43+
left = curr;
44+
curr = ~~((left + right) / 2);
45+
} else {
46+
right = curr;
47+
curr = ~~((left + right) / 2);
48+
}
49+
}
50+
};

922__easy__sort-array-by-parity-ii.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
3+
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
4+
5+
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
6+
7+
You may return any answer array that satisfies this condition.
8+
9+
10+
Example 1:
11+
12+
Input: [4,2,5,7]
13+
Output: [4,5,2,7]
14+
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
15+
16+
17+
Note:
18+
19+
2 <= A.length <= 20000
20+
A.length % 2 == 0
21+
0 <= A[i] <= 1000
22+
23+
*/
24+
25+
/**
26+
* check even and odd index until both not satisfies the condition then swap them (in place swap)
27+
* time O(n)
28+
* space O(1)
29+
* Runtime: 104 ms, faster than 85.27% of JavaScript
30+
*/
31+
var sortArrayByParityII = function(A) {
32+
let n = A.length;
33+
let evenIdx = 0;
34+
let oddIdx = 1;
35+
36+
while (evenIdx < n && oddIdx < n) {
37+
while (evenIdx < n && A[evenIdx] % 2 == 0) {
38+
evenIdx += 2;
39+
}
40+
while (oddIdx < n && A[oddIdx] % 2 != 0) {
41+
oddIdx += 2;
42+
}
43+
if (evenIdx < n && oddIdx < n) {
44+
[A[evenIdx], A[oddIdx]] = [A[oddIdx], A[evenIdx]];
45+
evenIdx += 2;
46+
oddIdx += 2;
47+
}
48+
}
49+
50+
return A;
51+
};

0 commit comments

Comments
 (0)