Skip to content

Instantly share code, notes, and snippets.

@ahmet-cetinkaya
Created December 31, 2020 17:02
Show Gist options
  • Save ahmet-cetinkaya/c9e79784c150c932b647e1dec90a2154 to your computer and use it in GitHub Desktop.
Save ahmet-cetinkaya/c9e79784c150c932b647e1dec90a2154 to your computer and use it in GitHub Desktop.
LeetCode Solutions - 383. Ransom Note
const canConstruct = (ransomNote, magazine) => {
const dicMap = new Map();
for (const c of magazine) dicMap.set(c, ~~dicMap.get(c) + 1);
for (const c of ransomNote) {
if (!dicMap.get(c)) return false;
dicMap.set(c, ~~dicMap.get(c) - 1);
}
return true;
};
const canConstruct = (ransomNote, magazine) => {
let dicArr = [...magazine];
for (const c of ransomNote) {
const index = dicArr.indexOf(c);
if (index < 0) return false;
dicArr.splice(index, 1);
}
return true;
};
const canConstruct = (ransomNote, magazine) => {
let dicObj = {};
for (let i = 0; i < magazine.length; ++i) {
if (dicObj[magazine[i]]) ++dicObj[magazine[i]];
else dicObj[magazine[i]] = 1;
}
for (let i = 0; i < ransomNote.length; ++i) {
if (dicObj[ransomNote[i]] === undefined || dicObj[ransomNote[i]] <= 0) return false;
--dicObj[ransomNote[i]];
}
return true;
};
@ahmet-cetinkaya
Copy link
Author

ransomNote.js

Result
Runtime: 92 ms, faster than 88.69% of JavaScript online submissions for Ransom Note.
Memory Usage: 41.2 MB, less than 88.43% of JavaScript online submissions for Ransom Note.

ransomNote2.js

Result
Runtime: 104 ms, faster than 56.56% of JavaScript online submissions for Ransom Note.
Memory Usage: 43.1 MB, less than 31.88% of JavaScript online submissions for Ransom Note.

ransomNote3.js

Result
Runtime: 112 ms, faster than 31.36% of JavaScript online submissions for Ransom Note.
Memory Usage: 41.1 MB, less than 90.75% of JavaScript online submissions for Ransom Note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment