Skip to content

Commit

Permalink
feat(LeetCode): add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
plskz committed Apr 2, 2023
1 parent e3c25bc commit 17502d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LeetCode/longest-common-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function longestCommonPrefix(strs: string[]): string {
for (let i = 0; i < strs[0].length; i++) {
for (let s of strs) {
if (s[i] !== strs[0][i]) return s.slice(0, i);
}
}

return strs[0];
}
10 changes: 10 additions & 0 deletions LeetCode/missing-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function missingNumber(nums: number[]): number {
let n = nums.length;
let total = (n * (n + 1)) / 2;
let sum = nums.reduce((a, b) => a + b);

return total - sum;
}

console.log(missingNumber([0, 2, 3])); // 1
console.log(missingNumber([3, 0, 1])); // 2
26 changes: 26 additions & 0 deletions LeetCode/successful-pairs-of-spells-and-potions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function successfulPairs(
spells: number[],
potions: number[],
success: number
): number[] {
let ans: number[] = [];

potions.sort((a, b) => a - b);

for (let spell of spells) {
let left = 0;
let right = potions.length;

while (left < right) {
let mid = Math.floor(left + (right - left) / 2);
if (potions[mid] * spell < success) {
left = mid + 1;
} else {
right = mid;
}
}
ans.push(potions.length - left);
}

return ans;
}

0 comments on commit 17502d1

Please sign in to comment.