Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd588c2

Browse files
allow ++ on for loops
1 parent bc491d5 commit bd588c2

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

‎.eslintrc.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212

1313
// https://eslint.org/docs/rules/no-plusplus
1414
// allows unary operators ++ and -- in the afterthought (final expression) of a for loop.
15-
"allowForLoopAfterthoughts": true,
15+
"no-plusplus": [2,{"allowForLoopAfterthoughts": true}],
1616

1717
// Allow for..of
1818
"no-restricted-syntax": [0, "ForOfStatement"],
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/**
2-
* 8.7 Permutations without Dups: Write a method to compute all permutations of a string of unique characters.
2+
* 8.7 Permutations without Dups:
3+
*
4+
* Write a method to compute all permutations of
5+
* a string of unique characters.
36
*
47
* @param string
58
* @param prefix
@@ -9,19 +12,18 @@
912
function permutations(string = '', prefix = '', memo = {}) {
1013
if (string.length < 2) {
1114
return [prefix + string];
12-
} else if (string.length == 2) {
15+
} else if (string.length === 2) {
1316
return [prefix + string, prefix + string[1] + string[0]];
1417
} else if (memo[string]) {
15-
return memo[string].map((e) => prefix + e);
16-
} else {
17-
let results = [];
18-
for (var i = 0; i < string.length; i++) {
19-
const letter = string[i];
20-
results = results.concat(permutations(string.replace(letter, ''), letter, memo));
21-
}
22-
memo[string] = results;
23-
return results.map((e) => prefix + e);
18+
return memo[string].map(e => prefix + e);
19+
}
20+
let results = [];
21+
for (let i = 0; i < string.length; i++) {
22+
const letter = string[i];
23+
results = results.concat(permutations(string.replace(letter, ''), letter, memo));
2424
}
25+
memo[string] = results;
26+
return results.map(e => prefix + e);
2527
}
2628

2729
module.exports = permutations;

‎src/algorithms/combination-sum.js‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,44 @@ function combinationSum(
88
target,
99
solution = [],
1010
current = [],
11-
currentSum = 0,
1211
index = 0,
1312
) {
14-
if (currentSum === target) {
15-
solution.push(current);
13+
if (target < 0) {
14+
// By adding another candidate we've gone below zero.
15+
// This would mean that the last candidate was not acceptable.
16+
return solution;
1617
}
1718

18-
let candidate = candidates[index];
19-
let newSum = currentSum + candidate;
20-
let newCurrent = current.concat(candidate);
21-
22-
if (newSum <= target) {
23-
combinationSum(candidates, target, solution, newCurrent, newSum, index);
24-
} else if (index < candidates.length - 1) {
25-
const newIndex = index + 1;
26-
candidate = candidates[newIndex];
27-
newSum = currentSum;
28-
const reducedCurrent = current.slice(); // clone current
29-
while (newSum + candidate > target) {
30-
const deletedCandidate = reducedCurrent.pop();
31-
newSum -= deletedCandidate;
32-
}
33-
newSum += candidate;
34-
newCurrent = reducedCurrent.concat(candidate);
35-
combinationSum(candidates, target, solution, newCurrent, newSum, newIndex);
19+
if (target === 0) {
20+
// If after adding the previous candidate our remaining sum
21+
// became zero - we need to save the current combination since it is one
22+
// of the answers we're looking for.
23+
solution.push(current.slice());
24+
25+
return solution;
3626
}
3727

28+
// If we haven't reached zero yet let's continue to add all
29+
// possible candidates that are left.
30+
for (let candidateIndex = index; candidateIndex < candidates.length; candidateIndex += 1) {
31+
const currentCandidate = candidates[candidateIndex];
32+
33+
// Let's try to add another candidate.
34+
current.push(currentCandidate);
35+
36+
// Explore further option with current candidate being added.
37+
combinationSum(
38+
candidates,
39+
target - currentCandidate,
40+
solution,
41+
current,
42+
candidateIndex,
43+
);
44+
45+
// BACKTRACKING.
46+
// Let's get back, exclude current candidate and try another ones later.
47+
current.pop();
48+
}
3849

3950
return solution;
4051
}

‎src/algorithms/combination-sum.spec.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ describe('combinationSum', () => {
1717
expect(combinationSum([2], 1)).toEqual([]);
1818
});
1919

20-
fit('should not find solution', () => {
20+
it('should find solution using two values', () => {
2121
expect(combinationSum([1, 2], 3)).toEqual([
2222
[1, 1, 1],
2323
[1, 2],
2424
]);
2525
});
26+
27+
it('should move on with next index', () => {
28+
expect(combinationSum([1, 10, 2], 3)).toEqual([
29+
[1, 1, 1],
30+
[1, 2],
31+
]);
32+
});
2633
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /