Skip to content

Instantly share code, notes, and snippets.

@kevinahn7
Created January 10, 2019 02:04
Show Gist options
  • Select an option

  • Save kevinahn7/86d66e7b7f3c87193677f058f65f2e9d to your computer and use it in GitHub Desktop.

Select an option

Save kevinahn7/86d66e7b7f3c87193677f058f65f2e9d to your computer and use it in GitHub Desktop.
// function fullyContains(first: Array, second: Array): boolean;
//
// given two **already sorted** arrays with unique elements,
// first and second, return true if the first array contains
// all the elements found in the second array
//
// fullyContains([1, 2, 3], [1, 2, 3]) => true
// fullyContains([1, 2, 3], [2, 3]) => true
// fullyContains([1, 2, 3], [4]) => false
// fullyContains([], []) => true
// fullyContains([], [1, 2, 3]) => false
// fullyContains([1, 2, 3], []) => true
// [1, 2, 3] [2, 3]
// [0, 4, 6, 8] [4, 6, 8] => true
// [0, 4, 6, 8] [4, 8] => true
// [0, 4, 6, 8] [4, 8] => true
// newSecondArray = [8]
// newFirstArray = [6, 8]
function fullyContains(first, second) {
for (let i = 0; i < first.length; i++) { // O(M)
if (first[i] === second[0]) {
second.shift()
}
}
if (second.length > 0) return false; // TODO: fix me
else return true;
}
function fullyContains(first /* len M */, second /* len N */) {
for (let j = 0; j < second.length; j++) { // O(N)
// if (!first.includes(second[j])) { // O(M)
if (!binarySearch(first, second[j])) { // O(log M)
return false;
}
}
return true;
} // O(M)
// non-unique?
// fullyContains([1, 1, 2, 2, 3, 3], [1, 2, 3]) => true
// fullyContains([1, 1, 2, 2, 3, 3], [1, 1, 2, 3]) => true
// fullyContains([1, 1, 2, 2, 3, 3], [1, 1, 2, 2, 3]) => true
// fullyContains([1, 1, 2, 2, 3, 3], [1, 1, 1, 2, 2, 3]) => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment