Skip to content

Commit a6813f1

Browse files
committed
pb4
1 parent 502da5b commit a6813f1

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,20 @@ The following test should pass:
4747
node = Node('root', Node('left', Node('left.left')), Node('right'))
4848
assert deserialize(serialize(node)).left.left.val == 'left.left'
4949
```
50+
5051
### Solution
52+
5153
[JS](pb3/answer.js) (5:12)
54+
55+
56+
## 4
57+
>This problem was asked by Stripe.
58+
59+
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
60+
For example, the input `[3, 4, -1, 1]` should give `2`. The input `[1, 2, 0]` should give `3`.
61+
You can modify the input array in-place.
62+
63+
### Solution
64+
65+
[JS](pb4/answer.js) (3:47)
66+

pb4/Problem.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
>This problem was asked by Stripe.
2+
3+
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
4+
For example, the input `[3, 4, -1, 1]` should give `2`. The input `[1, 2, 0]` should give `3`.
5+
You can modify the input array in-place.

pb4/answer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// Time : 3:47
3+
const input1 = [3, 4, -1, 1];
4+
const input2 = [1, 2, 0];
5+
6+
const pb4 = (input) => {
7+
// Filter out negative elements
8+
const onlyPositives = input.filter( el => el > 0 );
9+
let i = 1;
10+
// Give the loop some limit
11+
while(i < 1000000000){
12+
if(!onlyPositives.includes(i))
13+
//Starts at 1, returns whenever an integer is missing
14+
return i;
15+
i++;
16+
}
17+
}
18+
19+
console.log(pb4(input1));
20+
console.log(pb4(input2));

0 commit comments

Comments
 (0)