-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |