Skip to content

Commit 2dab7f0

Browse files
committed
update
1 parent ef3359f commit 2dab7f0

7 files changed

+239
-0
lines changed

combination.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function combinations (m, n) {
2+
const nums = [0, 1, 2, 3, 4];
3+
const result = [];
4+
5+
backtracking(nums, '', 0, 4);
6+
7+
function backtracking(nums, str, start, len) {
8+
if (str.length === 3) {
9+
const t = str.split('').sort().join('');
10+
if (result.indexOf(t) === -1) {
11+
result.push(t);
12+
}
13+
}
14+
15+
for (let i = start; i <= len; i++) {
16+
backtracking(nums, str + nums[i], start + 1, len);
17+
}
18+
19+
}
20+
21+
22+
return result;
23+
}
24+
25+
console.log(combinations(3, 5));

insertion-sort.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Algorithms: Implement Insertion Sort
3+
* The next sorting method we'll look at is insertion sort. This method works by building up a sorted array at the beginning of the list. It begins the sorted array with the first element. Then it inspects the next element and swaps it backwards into the sorted array until it is in sorted position. It continues iterating through the list and swapping new items backwards into the sorted portion until it reaches the end. This algorithm has quadratic time complexity in the average and worst cases.
4+
*
5+
* Instructions: Write a function insertionSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
6+
*/
7+
8+
function insertionSort(array) {
9+
for (let i = 1, l = array.length; i < l; i++) {
10+
const val = array[i];
11+
let j = i - 1;
12+
13+
while (j >= 0 && array[j] > val) {
14+
array[j + 1] = array[j];
15+
j--;
16+
}
17+
18+
array[j + 1] = val;
19+
}
20+
return array;
21+
}
22+
23+
24+
console.log(insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));

matching-parenthesis-position.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* I like parentheticals (a lot).
3+
*
4+
* "Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing."
5+
*
6+
* Write a function that, given a sentence like the one above, along with the position of an opening parenthesis, finds the corresponding closing parenthesis.
7+
*
8+
* Example: if the example string above is input with the number 10 (position of the first parenthesis), the output should be 79 (position of the last parenthesis).
9+
*/
10+
11+
function getClosingParen(sentence, openingIndex) {
12+
let balance = 0;
13+
14+
for (let i = openingIndex, l = sentence.length; i < l; i++) {
15+
let char = sentence.charAt(i);
16+
17+
if (char === '(') {
18+
balance++;
19+
} else if (char === ')') {
20+
balance--;
21+
}
22+
23+
if (balance === 0) {
24+
return i;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
31+
let str = 'Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.';
32+
33+
console.log(getClosingParen(str, 10));
34+

pair-wise.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const assert = require('assert');
2+
/**
3+
* Algorithms: Pairwise
4+
* Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices.
5+
*
6+
* You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element. For instance, pairwise([1, 1, 2], 3) creates a pair [2, 1] using the 1 at indice 0 rather than the 1 at indice 1, because 0+2 < 1+2.
7+
*
8+
* For example pairwise([7, 9, 11, 13, 15], 20) returns 6. The pairs that sum to 20 are [7, 13] and [9, 11]. We can then write out the array with their indices and values.
9+
* Index 0 1 2 3 4
10+
* Value 7 9 11 13 15
11+
*
12+
* Below we'll take their corresponding indices and add them.
13+
*
14+
* 7 + 13 = 20 → Indices 0 + 3 = 3
15+
* 9 + 11 = 20 → Indices 1 + 2 = 3
16+
* 3 + 3 = 6 → Return 6
17+
*/
18+
19+
function pairwise(arr, target) {
20+
let ans = 0;
21+
const nums = {};
22+
23+
for (let i = 0, l = arr.length; i < l; i++) {
24+
const val = target - arr[i];
25+
26+
if (nums[val] !== undefined && nums[val].length > 0) {
27+
ans += nums[val].shift() + i;
28+
} else {
29+
nums[arr[i]] = nums[arr[i]] || [];
30+
nums[arr[i]].push(i);
31+
}
32+
}
33+
return ans;
34+
}
35+
36+
37+
assert.deepStrictEqual(pairwise([1,4,2,3,0,5], 7), 11);
38+
assert.deepStrictEqual(pairwise([1, 3, 2, 4], 4), 1);
39+
assert.deepStrictEqual(pairwise([1, 1, 1], 2), 1);
40+
assert.deepStrictEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
41+
assert.deepStrictEqual(pairwise([], 100), 0);
42+
43+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* You have a list of integers, and for each index you want to find the product of every integer except the integer at that index.
3+
*
4+
* Write a function get_products_of_all_ints_except_at_index() that takes a list of integers and returns a list of the products.
5+
*
6+
* For example, given:
7+
*
8+
* [1, 7, 3, 4]
9+
*
10+
* your function would return:
11+
*
12+
* [84, 12, 28, 21]
13+
*
14+
* by calculating:
15+
*
16+
* [7 * 3 * 4, 1 * 3 * 4, 1 * 7 * 4, 1 * 7 * 3]
17+
*
18+
* Here's the catch: You can't use division in your solution!
19+
*/
20+
21+
function productAll(arr) {
22+
if (arr.length < 2) {
23+
throw Error('Required at least two numbers');
24+
}
25+
26+
let ans = [];
27+
let products = 1;
28+
29+
for (let i = 0, l = arr.length; i < l; i++) {
30+
ans[i] = products;
31+
products *= arr[i];
32+
}
33+
34+
products = 1;
35+
36+
for (let i = ans.length - 1; i >= 0; i--) {
37+
ans[i] *= products;
38+
products *= arr[i];
39+
}
40+
41+
return ans;
42+
}
43+
44+
45+
console.log(productAll([1, 7, 3, 4]));
46+
47+
48+
49+

recursive-string-permutations.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Permutation
3+
*
4+
* Write a recursive function for generating all permutations of an input string
5+
*/
6+
function permutation(str) {
7+
const result = [];
8+
9+
backtracking(str.split(''), 0, str.length - 1, result);
10+
11+
return result;
12+
13+
function backtracking(str, start, end, result) {
14+
if (start === end) {
15+
result.push(str.join(''));
16+
return;
17+
}
18+
19+
for (let i = start; i <= end; i++) {
20+
swap(str, i, start);
21+
backtracking(str, start + 1, end, result);
22+
swap(str, start, i);
23+
}
24+
}
25+
26+
function swap(arr, i, j) {
27+
const t = arr[i];
28+
arr[i] = arr[j];
29+
arr[j] = t;
30+
}
31+
}
32+
33+
34+
35+
console.log(permutation('cat'));

selection-sort.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Algorithms: Implement Selection Sort
3+
* Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list, and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.
4+
*
5+
* Instructions: Write a function selectionSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
6+
*/
7+
8+
function selectionSort(array) {
9+
for (let i = 0, l = array.length; i < l - 1; i++) {
10+
let low = i;
11+
for (let j = i + 1; j < l; j++) {
12+
if (array[j] < array[low]) {
13+
low = j;
14+
}
15+
}
16+
17+
if (low !== i) {
18+
const t = array[i];
19+
array[i] = array[low];
20+
array[low] = t;
21+
}
22+
23+
}
24+
25+
return array;
26+
}
27+
28+
29+
console.log(selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));

0 commit comments

Comments
 (0)