-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
95fd709
commit 92ff159
Showing
14 changed files
with
164 additions
and
54 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
This file was deleted.
Oops, something went wrong.
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 @@ | ||
import { romanToInt } from './romanToInt'; | ||
|
||
describe('13. Roman to Integer', () => { | ||
test('romanToInt', () => { | ||
expect(romanToInt('III')).toBe(3); | ||
expect(romanToInt('LVIII')).toBe(58); | ||
expect(romanToInt('MCMXCIV')).toBe(1994); | ||
}); | ||
}); |
File renamed without changes.
8 changes: 0 additions & 8 deletions
8
src/page-1/14. Longest Common Prefix/longest-common-prefix.spec.ts
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/page-1/14. Longest Common Prefix/longestCommonPrefix.test.ts
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,8 @@ | ||
import { longestCommonPrefix } from './longestCommonPrefix'; | ||
|
||
describe('14. Longest Common Prefix', () => { | ||
test('longestCommonPrefix', () => { | ||
expect(longestCommonPrefix(['flower', 'flow', 'flight'])).toBe('fl'); | ||
expect(longestCommonPrefix(['dog', 'racecar', 'car'])).toBe(''); | ||
}); | ||
}); |
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
src/page-1/4. Median of Two Sorted Arrays/findMedianSortedArrays.test.ts
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,13 @@ | ||
import { findMedianSortedArrays, findMedianSortedArrays2 } from './findMedianSortedArrays'; | ||
|
||
describe('4. Median of Two Sorted Arrays', () => { | ||
test('findMedianSortedArrays', () => { | ||
expect(findMedianSortedArrays([1, 3], [2])).toBe(2); | ||
expect(findMedianSortedArrays([1, 2], [3, 4])).toBe(2.5); | ||
}); | ||
|
||
test('findMedianSortedArrays2', () => { | ||
expect(findMedianSortedArrays2([1, 3], [2])).toBe(2); | ||
expect(findMedianSortedArrays2([1, 2], [3, 4])).toBe(2.5); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
src/page-1/4. Median of Two Sorted Arrays/findMedianSortedArrays.ts
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,47 @@ | ||
type FindMedianSortedArrays = (nums1: number[], nums2: number[]) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const findMedianSortedArrays: FindMedianSortedArrays = (nums1, nums2) => { | ||
const total = nums1.length + nums2.length; | ||
const merged = [...nums1, ...nums2].sort((a, b) => a - b); | ||
if (total % 2 === 1) return merged[(total - 1) / 2]; | ||
return (merged[total / 2 - 1] + merged[total / 2]) / 2; | ||
}; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const findMedianSortedArrays2: FindMedianSortedArrays = (nums1, nums2) => { | ||
function findKth(nums1: number[], nums2: number[], k: number): number { | ||
let index1 = 0; | ||
let index2 = 0; | ||
let kth = k; | ||
|
||
while (true) { | ||
if (index1 === nums1.length) return nums2[index2 + kth - 1]; | ||
if (index2 === nums2.length) return nums1[index1 + kth - 1]; | ||
|
||
if (kth === 1) return Math.min(nums1[index1], nums2[index2]); | ||
|
||
const halfK = Math.floor(kth / 2); | ||
const newIndex1 = Math.min(index1 + halfK, nums1.length) - 1; | ||
const newIndex2 = Math.min(index2 + halfK, nums2.length) - 1; | ||
const pivot1 = nums1[newIndex1]; | ||
const pivot2 = nums2[newIndex2]; | ||
|
||
if (pivot1 <= pivot2) { | ||
kth -= newIndex1 - index1 + 1; | ||
index1 = newIndex1 + 1; | ||
} else { | ||
kth -= newIndex2 - index2 + 1; | ||
index2 = newIndex2 + 1; | ||
} | ||
} | ||
} | ||
|
||
const total = nums1.length + nums2.length; | ||
if (total % 2 === 1) return findKth(nums1, nums2, Math.floor(total / 2) + 1); | ||
return (findKth(nums1, nums2, total / 2) + findKth(nums1, nums2, total / 2 + 1)) / 2; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
import { bench } from 'vitest'; | ||
|
||
import { isPalindrome, isPalindrome2 } from './isPalindrome'; | ||
|
||
describe('9. Palindrome Number', () => { | ||
// fastest | ||
bench('isPalindrome', () => { | ||
isPalindrome(121); | ||
isPalindrome(-121); | ||
isPalindrome(10); | ||
}); | ||
|
||
bench('isPalindrome2', () => { | ||
isPalindrome2(121); | ||
isPalindrome2(-121); | ||
isPalindrome2(10); | ||
}); | ||
}); |
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,15 @@ | ||
import { isPalindrome, isPalindrome2 } from './isPalindrome'; | ||
|
||
describe('9. Palindrome Number', () => { | ||
test('isPalindrome', () => { | ||
expect(isPalindrome(121)).toBe(true); | ||
expect(isPalindrome(-121)).toBe(false); | ||
expect(isPalindrome(10)).toBe(false); | ||
}); | ||
|
||
test('isPalindrome2', () => { | ||
expect(isPalindrome2(121)).toBe(true); | ||
expect(isPalindrome2(-121)).toBe(false); | ||
expect(isPalindrome2(10)).toBe(false); | ||
}); | ||
}); |
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,50 @@ | ||
type IsPalindrome = (x: number) => boolean; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const isPalindrome: IsPalindrome = (x) => { | ||
// Negative numbers and numbers ending in zero (except 0 itself) are not palindromes | ||
if (x < 0 || (x % 10 === 0 && x !== 0)) return false; | ||
|
||
let [a, b] = [x, 0]; | ||
|
||
// Loop until the first half of the number is greater than or equal to the second half | ||
while (a > b) { | ||
// Move the last digit of 'a' to 'b' | ||
b = b * 10 + (a % 10); | ||
|
||
// Remove the last digit from 'a' | ||
a = Math.floor(a / 10); | ||
} | ||
|
||
// Check if the number is a palindrome | ||
// Either the reversed number (b) matches the remaining part (a) | ||
// or the reversed number without its last digit (b / 10) matches the remaining part (a) | ||
return a === b || Math.floor(b / 10) === a; | ||
}; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const isPalindrome2: IsPalindrome = (x) => { | ||
// Negative numbers are not palindromes | ||
if (x < 0) return false; | ||
|
||
// Convert the number to a string | ||
const str = x.toString(); | ||
|
||
// Initialize two pointers for the start and end of the string | ||
let [left, right] = [0, str.length - 1]; | ||
|
||
// Compare characters from the start and end moving towards the center | ||
while (left < right) { | ||
if (str[left] !== str[right]) return false; | ||
|
||
left += 1; | ||
right -= 1; | ||
} | ||
|
||
// If all characters matched, it is a palindrome | ||
return true; | ||
}; |