Skip to content

Commit c6e93e3

Browse files
2 parents 66ee459 + c484723 commit c6e93e3

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
3+
4+
Formally the function should:
5+
6+
Return true if there exists i, j, k
7+
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
8+
Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.
9+
10+
Example 1:
11+
12+
Input: [1,2,3,4,5]
13+
Output: true
14+
Example 2:
15+
16+
Input: [5,4,3,2,1]
17+
Output: false
18+
*/
19+
/**
20+
* @param {number[]} nums
21+
* @return {boolean}
22+
*/
23+
const increasingTriplet = nums => {
24+
let first = Number.MAX_SAFE_INTEGER;
25+
let second = Number.MAX_SAFE_INTEGER;
26+
27+
for (let num of nums) {
28+
if (num <= first) {
29+
first = num;
30+
} else if (num <=second) {
31+
second = num;
32+
} else {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}

exercises/leetcode/jumpGame.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
3+
4+
Each element in the array represents your maximum jump length at that position.
5+
6+
Your goal is to reach the last index in the minimum number of jumps.
7+
8+
Example:
9+
10+
Input: [2,3,1,1,4]
11+
Output: 2
12+
Explanation: The minimum number of jumps to reach the last index is 2.
13+
Jump 1 step from index 0 to 1, then 3 steps to the last index.
14+
Note:
15+
16+
You can assume that you can always reach the last index.
17+
*/
18+
19+
const minJump = nums => {
20+
const jumpsArray = [0];
21+
22+
for (let i = 1; i < nums.length; i++) {
23+
jumpsArray[i] = Number.MAX_SAFE_INTEGER;
24+
}
25+
for (let i = 1; i < nums.length; i++) {
26+
for (let j = 0; j < i; j++) {
27+
if (i <= j + nums[j]) {
28+
jumpsArray[i] = Math.min(jumpsArray[i], jumpsArray[j] + 1);
29+
}
30+
}
31+
}
32+
33+
return jumpsArray[jumpsArray.length - 1];
34+
}
35+
36+
// Optimal Solution
37+
38+
const jump = nums => {
39+
if (nums.length < 2) {
40+
return 0;
41+
}
42+
43+
let i = 0;
44+
let jumps = 0;
45+
46+
while (i + nums[i] < nums.length - 1) {
47+
let maxVal = 0;
48+
let maxValIndex = 0;
49+
50+
for (let j = 1; j <= nums[i]; j++) {
51+
if (nums [i + j] + j > maxVal) {
52+
maxVal = nums[i + j] + j;
53+
maxValIndex = i + j;
54+
}
55+
}
56+
i = maxValIndex;
57+
jumps++;
58+
}
59+
60+
return jumps + 1;
61+
}

exercises/leetcode/missingRanges.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
3+
4+
Example:
5+
6+
Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
7+
Output: ["2", "4->49", "51->74", "76->99"]
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @param {number} lower
13+
* @param {number} upper
14+
* @return {string[]}
15+
*/
16+
const missingRanges = (nums, lower, upper) => {
17+
nums = [lower - 1, ...nums, upper + 1];
18+
19+
const results = [];
20+
21+
for (let i = 1; i < nums.length; i++) {
22+
if (nums[i] !== nums[i - 1] && nums[i] - 1 !== nums[i -1]) {
23+
let num1 = nums[i - 1] + 1;
24+
let num2 = nums[i] - 1;
25+
results.push(
26+
num1 + (num2 === num1 ? "" : "->" + num2)
27+
);
28+
}
29+
}
30+
31+
return results;
32+
}

0 commit comments

Comments
 (0)