Skip to content

Bump minimatch from 3.0.4 to 3.1.2 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions distance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* A problem collected from Voz =))
*
* Given an array of strings, write a function return the minimum distance between a and b in array
*/

function solution(list = [], a = '', b = '') {
if (list.length === 0) return -1;
if (a.length === 0 || b.length === 0) return -1;

let firstIdx = -1, secondIdx = -1;
let minDist = list.length;

for (let i = 0; i < list.length; ++i) {
if (list[i] === a) {
firstIdx = i
minDist = Math.min(Math.abs(firstIdx - secondIdx), minDist);
} else if (list[i] === b) {
secondIdx = i
minDist = Math.min(Math.abs(firstIdx - secondIdx), minDist);
}
}

return minDist;
}

console.log(solution(["cat", "dog", "bird", "fish", "cat","duck","chicken","dog"], 'dog', 'duck'));
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading