Skip to content

Commit 26b1f5b

Browse files
authored
Merge pull request #1153 from aakhtar3/greedy
refactor: js - greedy
2 parents f802945 + 36ba672 commit 26b1f5b

8 files changed

+418
-56
lines changed

javascript/134-Gas-Station.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ var canCompleteCircuit = function (gas, cost) {
1010
let res = 0;
1111

1212
//Checks if theres enough gas to complete a cycle
13-
if (gas.reduce((a, b) => a + b) - cost.reduce((a, b) => a + b) < 0) {
14-
return -1;
15-
}
13+
if (gas.reduce((a, b) => a + b) - cost.reduce((a, b) => a + b) < 0) return -1;
1614

1715
// Finds the first appearence of a positive netDistance, if the cycle can't
1816
// be completed (netDistance < 0), starts cycle again @ the next positive netDistance value.
@@ -24,5 +22,27 @@ var canCompleteCircuit = function (gas, cost) {
2422
res = i + 1;
2523
}
2624
}
25+
2726
return res;
2827
};
28+
29+
/**
30+
* @param {number[]} gas
31+
* @param {number[]} cost
32+
* @return {number}
33+
*/
34+
var canCompleteCircuit = function(gas, cost) {
35+
let [ totalTank, currTank, startingStation ] = [ 0, 0, 0 ]
36+
37+
for (let i = 0; i < gas.length; i++) {
38+
totalTank += gas[i] - cost[i];
39+
currTank += gas[i] - cost[i];
40+
41+
const isEmpty = currTank < 0;
42+
if (isEmpty) { startingStation = (i + 1); currTank = 0; }
43+
}
44+
45+
return 0 <= totalTank
46+
? startingStation
47+
: -1;
48+
}

javascript/1899-Merge-Triplets-to-Form-Target-Triplet.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,46 @@ var mergeTriplets = function (triplets, target) {
2525

2626
return good.size === 3;
2727
};
28+
29+
/**
30+
* https://leetcode.com/problems/merge-triplets-to-form-target-triplet/
31+
* Time O(N) | Space O(1)
32+
* @param {number[][]} triplets
33+
* @param {number[]} target
34+
* @return {boolean}
35+
*/
36+
var mergeTriplets = function(triplets, target, res = new Array(3).fill(0)) {
37+
for (const [ a, b, c ] of triplets) { /* Time O(N) */
38+
const [ _a, _b, _c ] = target;
39+
40+
const isTargetGreater = (a <= _a) && (b <= _b) && (c <= _c);
41+
if (!isTargetGreater) continue;
42+
43+
const [ __a, __b, __c ] = res;
44+
res = [ Math.max(__a, a), Math.max(__b, b), Math.max(__c, c) ];
45+
}
46+
47+
return res.every((val, i) => val === target[i])/* Time O(N) */
48+
};
49+
50+
/**
51+
* https://leetcode.com/problems/merge-triplets-to-form-target-triplet/
52+
* Time O(N) | Space O(1)
53+
* @param {number[][]} triplets
54+
* @param {number[]} target
55+
* @return {boolean}
56+
*/
57+
var mergeTriplets = function(triplets, target, res = new Array(3).fill(false)) {
58+
for (const [ a, b, c ] of triplets) {/* Time O(N) */
59+
const [ _a, _b, _c ] = target;
60+
61+
const isTargetGreater = (a <= _a) && (b <= _b) && (c <= _c);
62+
if (!isTargetGreater) continue;
63+
64+
res[0] |= (a === _a);
65+
res[1] |= (b === _b);
66+
res[2] |= (c === _c);
67+
}
68+
69+
return res[0] && res[1] && res[2];
70+
}

javascript/45-Jump-Game-II.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
/**
22
* https://leetcode.com/problems/jump-game-ii/
3-
* Time: O(n)
3+
* Time O(N) | Space O(1)
44
* @param {number[]} nums
55
* @return {number}
66
*/
77
var jump = function (nums) {
8-
//Start at index 0
9-
let left = 0;
10-
let right = 0;
11-
let jumps = 0;
8+
let [ left, right, jumps ] = [ 0, 0, 0 ];
129

13-
//Once right >= last index, the last jump can be made
1410
while (right < nums.length - 1) {
15-
let maxReach = 0;
16-
17-
//Use maxReach of previous jump to find the maxReach of the current jump
18-
for (var i = left; i < right + 1; i++) {
19-
maxReach = Math.max(maxReach, nums[i] + i);
20-
}
11+
const maxReach = getMaxReach(nums, left, right);
2112

2213
left = right + 1;
2314
right = maxReach;
2415
jumps += 1;
2516
}
17+
2618
return jumps;
2719
};
20+
21+
const getMaxReach = (nums, left, right, maxReach = 0) => {
22+
for (let i = left; i < right + 1; i++) {
23+
const reach = nums[i] + i;
24+
maxReach = Math.max(maxReach, reach);
25+
}
26+
27+
return maxReach;
28+
}
29+
30+
/**
31+
* https://leetcode.com/problems/jump-game-ii/
32+
* Time O(N) | Space O(1)
33+
* @param {number[]} nums
34+
* @return {number}
35+
*/
36+
var jump = function(nums) {
37+
let [ jumps, currentJumpEnd, farthest ] = [ 0, 0, 0];
38+
39+
for (let i = 0; i < nums.length - 1; i++) {
40+
farthest = Math.max(farthest, (i + nums[i]));
41+
42+
const canJump = i === currentJumpEnd
43+
if (canJump) { jumps++; currentJumpEnd = farthest; }
44+
}
45+
46+
return jumps;
47+
}

javascript/53-Maximum-Subarray.js

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
11
/**
2+
* https://leetcode.com/problems/maximum-subarray/
3+
* Time O(N^2) | Space O(1)
24
* @param {number[]} nums
35
* @return {number}
46
*/
5-
var maxSubArray = function (nums) {
6-
let curr = nums[0];
7-
let max = nums[0];
7+
var maxSubArray = function(nums, maxSum = -Infinity) {
8+
for (let i = 0, sum = 0; i < nums.length; i++) {
9+
for (let j = i; j < nums.length; j++) {
10+
sum += nums[j];
11+
maxSum = Math.max(maxSum, sum);
12+
}
13+
}
14+
15+
return maxSum;
16+
}
17+
18+
/**
19+
* https://leetcode.com/problems/maximum-subarray/
20+
* Time O(N * log(N)) | Space O(log(N))
21+
* @param {number[]} nums
22+
* @return {number}
23+
*/
24+
var maxSubArray = function(nums, left = 0, right = nums.length - 1) {
25+
const isBaseCase = (right < left)
26+
if (isBaseCase) return -Infinity;
27+
28+
const mid = (left + right) >> 1;
29+
const guess = nums[mid];
30+
const leftSum = getLeftSumFromMid(nums, mid, left)
31+
const rightSum = getRightSumFromMid(nums, mid, right)
32+
const sum = guess + leftSum + rightSum;
33+
34+
const leftHalf = maxSubArray(nums, left, (mid - 1));
35+
const rightHalf = maxSubArray(nums, (mid + 1), right);
36+
37+
return Math.max(sum, leftHalf, rightHalf);
38+
}
839

40+
const getLeftSumFromMid = (nums, mid, left, sum = 0, maxSum = 0) => {
41+
for (let i = (mid - 1); left <= i; i--) {
42+
sum += nums[i];
43+
maxSum = Math.max(maxSum, sum);
44+
}
45+
46+
return maxSum;
47+
}
48+
49+
const getRightSumFromMid = (nums, mid, right, sum = 0, maxSum = 0) => {
50+
for (let i = (mid + 1); i <= right; i++) {
51+
sum += nums[i];
52+
maxSum = Math.max(maxSum, sum);
53+
}
54+
55+
return maxSum;
56+
}
57+
58+
/**
59+
* https://leetcode.com/problems/maximum-subarray/
60+
* Time O(N) | Space O(1)
61+
* @param {number[]} nums
62+
* @return {number}
63+
*/
64+
var maxSubArray = function(nums) {
65+
let [ runningSum, maxSum ] = [ nums[0], nums[0] ]
66+
967
for (let i = 1; i < nums.length; i++) {
10-
curr = Math.max(curr + nums[i], nums[i]);
11-
max = Math.max(max, curr);
68+
const num = nums[i]
69+
const sum = runningSum + num
70+
71+
runningSum = Math.max(num, sum)
72+
maxSum = Math.max(maxSum, runningSum)
1273
}
13-
return max;
14-
};
74+
75+
return maxSum
76+
};

javascript/55-Jump-Game.js

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,98 @@
11
/**
2+
* Time O(2^N) | Space O(N)
23
* @param {number[]} nums
34
* @return {boolean}
45
*/
5-
var canJump = function (nums) {
6-
let goal = nums.length - 1;
6+
var canJump = (nums, index = 0) => {
7+
const isBaseCase = index === nums.length - 1;
8+
if (isBaseCase) return true;
79

8-
for (let i = nums.length - 2; i >= 0; i--) {
9-
if (i + nums[i] >= goal) {
10-
goal = i;
10+
const furthestJump = Math.min(index + nums[index], (nums.length - 1));
11+
for (let nextIndex = (index + 1); nextIndex <= furthestJump; nextIndex++) {
12+
if (canJump(nums, nextIndex)) return true;
13+
}
14+
15+
return false;
16+
}
17+
18+
/**
19+
* Time O(N^2) | Space O(N)
20+
* @param {number[]} nums
21+
* @return {boolean}
22+
*/
23+
var canJump = (nums) => {
24+
const memo = new Array(nums.length).fill(0);
25+
memo[memo.length - 1] = 1;
26+
27+
return canJumpFromIndex(nums, memo);
28+
}
29+
30+
const canJumpFromIndex = (nums, memo, index = 0) => {
31+
if (memo[index] !== 0) return memo[index] === 1;
32+
33+
const furthestJump = Math.min(index + nums[index], nums.length - 1);
34+
for (let nextIndex = (index + 1); nextIndex <= furthestJump; nextIndex++) {
35+
if (!canJumpFromIndex(nums, memo, nextIndex)) continue
36+
37+
memo[index] = 1;
38+
return true;
39+
}
40+
41+
memo[index] = -1;
42+
return false;
43+
}
44+
45+
/**
46+
* Time O(N^2) | Space O(N)
47+
* @param {number[]} nums
48+
* @return {boolean}
49+
*/
50+
var canJump = (nums) => {
51+
const memo = new Array(nums.length).fill(0)
52+
memo[memo.length - 1] = 1;
53+
54+
for (let i = (nums.length - 2); 0 <= i; i--) {
55+
const furthestJump = Math.min(i + nums[i], nums.length - 1);
56+
for (let j = (i + 1); j <= furthestJump; j++) {
57+
const isGood = memo[j] === 1
58+
if (isGood) { memo[i] = 1; break; }
1159
}
1260
}
13-
if (goal === 0) return true;
14-
else return false;
15-
};
61+
62+
return memo[0] === 1;
63+
}
64+
65+
/**
66+
* Time O(N) | Space O(1)
67+
* @param {number[]} nums
68+
* @return {boolean}
69+
*/
70+
var canJump = (nums, max = 0, index = 0) => {
71+
while (index < nums.length) {
72+
const num = nums[index]
73+
const jumps = num + index
74+
75+
const canNotReachEnd = max < index
76+
if (canNotReachEnd) return false
77+
78+
max = Math.max(max, jumps)
79+
index++
80+
}
81+
82+
return true
83+
}
84+
85+
/**
86+
* Time O(N) | Space O(1)
87+
* @param {number[]} nums
88+
* @return {boolean}
89+
*/
90+
var canJump = (nums, right = nums.length - 1) => {
91+
for (let i = right; 0 <= i; i--) {
92+
const isJumpable = right <= (i + nums[i])
93+
if (isJumpable) right = i;
94+
}
95+
96+
return right === 0;
97+
}
98+

0 commit comments

Comments
 (0)