-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: new problem solution - 287 . Find the Duplicate Number
- Loading branch information
Showing
3 changed files
with
138 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
59 changes: 59 additions & 0 deletions
59
problems/algorithms/findTheDuplicateNumber/FindTheDuplicateNumber.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,59 @@ | ||
// Source : https://leetcode.com/problems/find-the-duplicate-number/ | ||
// Author : squxq | ||
// Date : 2023-09-20 | ||
|
||
import { findDuplicate } from "./FindTheDuplicateNumber"; // Replace with the actual path to your code file | ||
|
||
describe("findDuplicate", () => { | ||
it("Test Case 1", () => { | ||
const nums = [1, 3, 4, 2, 2]; | ||
expect(findDuplicate(nums)).toBe(2); | ||
}); | ||
|
||
it("Test Case 2", () => { | ||
const nums = [3, 1, 3, 4, 2]; | ||
expect(findDuplicate(nums)).toBe(3); | ||
}); | ||
|
||
it("Test Case 3 (Minimum Input Size)", () => { | ||
const nums = [1]; | ||
expect(findDuplicate(nums)).toBe(1); | ||
}); | ||
|
||
it("Test Case 4 (Maximum Input Size)", () => { | ||
const n = 100000; | ||
const nums = Array.from({ length: n }, (_, i) => i + 1); | ||
nums.push(99999); // Add a duplicate | ||
expect(findDuplicate(nums)).toBe(99999); | ||
}); | ||
|
||
it("Test Case 5 (Duplicate at the Beginning)", () => { | ||
const nums = [2, 2, 3, 4, 5]; | ||
expect(findDuplicate(nums)).toBe(2); | ||
}); | ||
|
||
it("Test Case 6 (Duplicate at the End)", () => { | ||
const nums = [1, 2, 3, 4, 5, 5]; | ||
expect(findDuplicate(nums)).toBe(5); | ||
}); | ||
|
||
it("Test Case 7 (Duplicate in the Middle)", () => { | ||
const nums = [1, 2, 3, 3, 4, 5]; | ||
expect(findDuplicate(nums)).toBe(3); | ||
}); | ||
|
||
it("Test Case 8 (Random Order)", () => { | ||
const nums = [5, 2, 1, 3, 4, 4]; | ||
expect(findDuplicate(nums)).toBe(4); | ||
}); | ||
|
||
it("Test Case 9 (Duplicates at Both Ends)", () => { | ||
const nums = [1, 2, 1, 3, 4, 5, 5]; | ||
expect(findDuplicate(nums)).toBe(1); | ||
}); | ||
|
||
it("Test Case 10 (All Elements Are the Same)", () => { | ||
const nums = [7, 7, 7, 7, 7]; | ||
expect(findDuplicate(nums)).toBe(7); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
problems/algorithms/findTheDuplicateNumber/FindTheDuplicateNumber.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,78 @@ | ||
// Source : https://leetcode.com/problems/find-the-duplicate-number/ | ||
// Author : squxq | ||
// Date : 2023-09-20 | ||
|
||
/***************************************************************************************************** | ||
* | ||
* Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] | ||
* inclusive. | ||
* | ||
* There is only one repeated number in nums, return this repeated number. | ||
* | ||
* You must solve the problem without modifying the array nums and uses only constant extra space. | ||
* | ||
* Example 1: | ||
* | ||
* Input: nums = [1,3,4,2,2] | ||
* Output: 2 | ||
* | ||
* Example 2: | ||
* | ||
* Input: nums = [3,1,3,4,2] | ||
* Output: 3 | ||
* | ||
* Constraints: | ||
* | ||
* 1 <= n <= 10^5 | ||
* nums.length == n + 1 | ||
* 1 <= nums[i] <= n | ||
* All the integers in nums appear only once except for precisely one integer which appears | ||
* two or more times. | ||
* | ||
* Follow up: | ||
* | ||
* How can we prove that at least one duplicate number must exist in nums? | ||
* Can you solve the problem in linear runtime complexity? | ||
******************************************************************************************************/ | ||
|
||
// number[] -> number | ||
// return the repeated number in the given an array of integers n + 1 integers where each integer is in the range [1, n] | ||
|
||
/** Stub: | ||
function findDuplicate(nums: number[]): number { return 0 } | ||
*/ | ||
|
||
/** Wrong Implementation of Floyd's Tortoise and Hare Algorithm | ||
function findDuplicate(nums: number[]): number { | ||
if (nums.length < 3) return nums[0] | ||
let d: number = 1, h: number = 2 | ||
while(nums[d] !== nums[h]) { | ||
if (d === nums.length - 1) d = 0 | ||
else if (h + 2 > nums.length - 1) h = nums.length - h - 2 | ||
d++ | ||
h += 2 | ||
} | ||
return nums[d] | ||
}; | ||
*/ | ||
|
||
export function findDuplicate(nums: number[]): number { | ||
let slow: number = nums[0] as number; | ||
let fast: number = nums[0] as number; | ||
|
||
do { | ||
slow = nums[slow] as number; | ||
fast = nums[nums[fast] as number] as number; | ||
} while (slow !== fast); | ||
|
||
let ptr1: number = nums[0] as number; | ||
let ptr2: number = slow; | ||
while (ptr1 !== ptr2) { | ||
ptr1 = nums[ptr1] as number; | ||
ptr2 = nums[ptr2] as number; | ||
} | ||
|
||
return ptr1; | ||
} |