Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created October 19, 2021 12:48
Show Gist options
  • Select an option

  • Save blacksheep557/7e98dc703c447be7ffd806cba7cc1992 to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/7e98dc703c447be7ffd806cba7cc1992 to your computer and use it in GitHub Desktop.
// isPositiveDominant([1, 1, 1, 1, -3, -4])// false
// // There is only 1 unique positive value (1).
// // There are 2 unique negative values (-3, -4).
// isPositiveDominant([5, 99, 832, -3, -4])// true
// isPositiveDominant([5, 0])// true
isPositiveDominant([0, -4, -1])// false
function isPositiveDominant(arr) {
const posSet = new Set()
const negSet = new Set()
arr.forEach(element => {
if (element < 0) negSet.add(element)
else posSet.add(element)
});
return posSet.size > negSet.size
}
// kSmallestPairs([1, 7, 11], [2, 4, 6], 3)
// ➞ [[1,2],[1,4],[1,6]]
// kSmallestPairs([1, 1, 2], [1, 2, 3], 2)
// ➞ [[1,1],[1,1]]
// kSmallestPairs([1, 2], [3], 3)
// ➞ [[1,3],[2,3]]
function kSmallestPairs(arr1, arr2, k) {
let bigArr = [...arr1, ...arr2].map(e => Number(e)).sort((a, b) => a-b)
const result = []
let pt1 = 0
let pt2 = 1
while (result.length < k) {
result.push([bigArr[pt1], bigArr[pt2]]);
pt2++
if (pt2 === bigArr.length) {
pt1 ++
pt2 = pt1 + 1
}
}
console.log(result)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment