Skip to content

Commit

Permalink
143rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 10, 2024
1 parent 95fd709 commit 92ff159
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 54 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Must-do List for Interview Prep
[27]: ./src/page-1/27.%20Remove%20Element/remove-element.ts
[26]: ./src/page-1/26.%20Remove%20Duplicates%20from%20Sorted%20Array/remove-duplicates.ts
[189]: ./src/page-2/189.%20Rotate%20Array/rotate.ts
[13]: ./src/page-1/13.%20Roman%20to%20Integer/roman-to-int.ts
[13]: ./src/page-1/13.%20Roman%20to%20Integer/romanToInt.ts
[58]: ./src/page-1/58.%20Length%20of%20Last%20Word/length-of-last-word.ts
[14]: ./src/page-1/14.%20Longest%20Common%20Prefix/longest-common-prefix.ts
[28]: ./src/page-1/28.%20Implement%20strStr()/str-str.ts
Expand Down Expand Up @@ -451,10 +451,11 @@ Must-do List for Interview Prep
| 33. Search in Rotated Sorted Array | Solution | Medium |
| 34. Find First and Last Position of Element in Sorted Array | [Solution][34] | Medium |
| 153. Find Minimum in Rotated Sorted Array | Solution | Medium |
| 4. Median of Two Sorted Arrays | Solution | Hard |
| 4. Median of Two Sorted Arrays | [Solution][4] | Hard |

[35]: ./src/page-1/35.%20Search%20Insert%20Position/search-insert.ts
[34]: ./src/page-1/34.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/search-range.ts
[4]: ./src/page-1/4.%20Median%20of%20Two%20Sorted%20Arrays/findMedianSortedArrays.ts

| Heap | | |
| ------------------------------------ | -------- | ------ |
Expand Down Expand Up @@ -486,7 +487,7 @@ Must-do List for Interview Prep
| 50. Pow(x, n) | [Solution][50] | Medium |
| 149. Max Points on a Line | Solution | Hard |

[9]: ./src/page-1/9.%20Palindrome%20Number/is-palindrome.ts
[9]: ./src/page-1/9.%20Palindrome%20Number/isPalindrome.ts
[66]: ./src/page-1/66.%20Plus%20One/plus-one.ts
[69]: ./src/page-1/69.%20Sqrt(x)/my-sqrt.ts
[50]: ./src/page-1/50.%20Pow(x,%20n)/pow.ts
Expand Down
11 changes: 0 additions & 11 deletions src/page-1/13. Roman to Integer/roman-to-int.spec.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/page-1/13. Roman to Integer/romanToInt.test.ts
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);
});
});

This file was deleted.

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('');
});
});
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);
});
});
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;
};
10 changes: 0 additions & 10 deletions src/page-1/9. Palindrome Number/is-palindrome.spec.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/page-1/9. Palindrome Number/is-palindrome.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/page-1/9. Palindrome Number/isPalindrome.bench.ts
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);
});
});
15 changes: 15 additions & 0 deletions src/page-1/9. Palindrome Number/isPalindrome.test.ts
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);
});
});
50 changes: 50 additions & 0 deletions src/page-1/9. Palindrome Number/isPalindrome.ts
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;
};

0 comments on commit 92ff159

Please sign in to comment.