Skip to content

Commit c5d26fe

Browse files
2 parents 311b9c1 + 5419d4f commit c5d26fe

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

concepts/Currying.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,55 @@ console.log(snowDragons);
8080

8181
## Conclusion
8282

83-
> A curryable function is a function that takes every argument by itself and returns a new function that expects the next dependency to the function until all dependencies are fulfilled and the final value is returned
83+
> A curryable function is a function that takes every argument by itself and returns a new function that expects the next dependency to the function until all dependencies are fulfilled and the final value is returned
84+
85+
### Question with Currying, Recursion, Closures
86+
87+
> sum(1)(2)(3)(4)..(n)() | Amazon UI/Frontend Javascript Interview Question
88+
89+
```js
90+
const sum = function(a) {
91+
return function(b) {
92+
return function(c) {
93+
return function(d) {
94+
...
95+
...
96+
...
97+
return a + b + c + d + ... n;
98+
}
99+
}
100+
}
101+
}
102+
103+
// Recursive way to solve the problem as we see a repeating pattern
104+
105+
const sum = function(a) {
106+
return function(b) {
107+
if (b) {
108+
return sum(a + b);
109+
} else {
110+
return a;
111+
}
112+
}
113+
}
114+
115+
// The above recursive solution, will keep returning a function (the sum function in above case, until arguments are provided)
116+
// While returning a function each time, the arguments are added and the sum function gets called
117+
// The sum function returns a function with the value passed in parameter 'a' stored in lexical scope or through the closure concept
118+
// Once there are no more arguments specified, we simply return the value of 'a' which is the added total result
119+
120+
// In ES6 syntax:
121+
122+
const sum = a => {
123+
return b => {
124+
if (b) {
125+
return sum(a + b);
126+
} else {
127+
return a;
128+
}
129+
}
130+
}
131+
132+
// Simplified to one line
133+
const sum = a => b => b ? sum(a + b) : a;
134+
```

exercises/leetcode/google/canJump.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
Determine if you are able to reach the last index.
7+
8+
Example 1:
9+
10+
Input: [2,3,1,1,4]
11+
Output: true
12+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
13+
Example 2:
14+
15+
Input: [3,2,1,0,4]
16+
Output: false
17+
Explanation: You will always arrive at index 3 no matter what. Its maximum
18+
jump length is 0, which makes it impossible to reach the last index.
19+
*/
20+
21+
// O(n2) : Time Complexity
22+
// O(n) : Space Complexity
123
const INDEX = {
224
'G': 'GOOD',
325
'B': 'BAD',
@@ -22,4 +44,18 @@ const canJump = nums => {
2244
}
2345

2446
return map[0] == INDEX.G;
47+
}
48+
49+
// Greedy Approach
50+
51+
const canJump = nums => {
52+
let lastPos = nums.length - 1;
53+
54+
for (let i = nums.length - 1; i >= 0; i--) {
55+
if (i + nums[i] >= lastPos) {
56+
lastPos = i;
57+
}
58+
}
59+
60+
return lastPos == 0;
2561
}

exercises/leetcode/sortColors.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent,
3+
* with the colors in the order red, white and blue.
4+
5+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
6+
7+
Note: You are not suppose to use the library's sort function for this problem.
8+
9+
Example:
10+
11+
Input: [2,0,2,1,1,0]
12+
Output: [0,0,1,1,2,2]
13+
Follow up:
14+
15+
A rather straight forward solution is a two-pass algorithm using counting sort.
16+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
17+
Could you come up with a one-pass algorithm using only constant space?
18+
*/
19+
20+
const sortColors = nums => {
21+
let p0 = 0;
22+
let curr = 0;
23+
let p2 = nums.length - 1;
24+
25+
while (curr <= p2) {
26+
if (nums[curr] === 0) {
27+
[nums[curr], nums[p0]] = [nums[p0], nums[curr]];
28+
curr++;
29+
p0++;
30+
} else if (nums[curr] === 2) {
31+
[nums[curr], nums[p2]] = [nums[p2], nums[curr]];
32+
p2--;
33+
} else {
34+
curr++;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)