-
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
8d6cb96
commit 6740357
Showing
2 changed files
with
40 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,17 @@ | ||
import { getCommon } from './getCommon'; | ||
|
||
describe('2540. Minimum Common Value', () => { | ||
test('getCommon', () => { | ||
{ | ||
const nums1 = [1, 2, 3]; | ||
const nums2 = [2, 4]; | ||
expect(getCommon(nums1, nums2)).toBe(2); | ||
} | ||
|
||
{ | ||
const nums1 = [1, 2, 3, 6]; | ||
const nums2 = [2, 3, 4, 5]; | ||
expect(getCommon(nums1, nums2)).toBe(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,23 @@ | ||
type getCommon = (nums1: number[], nums2: number[]) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const getCommon: getCommon = (nums1, nums2) => { | ||
let pointer1 = 0; | ||
let pointer2 = 0; | ||
|
||
while (pointer1 < nums1.length && pointer2 < nums2.length) { | ||
if (nums1[pointer1] === nums2[pointer2]) { | ||
return nums1[pointer1]; | ||
} | ||
|
||
if (nums1[pointer1] < nums2[pointer2]) { | ||
pointer1 += 1; | ||
} else { | ||
pointer2 += 1; | ||
} | ||
} | ||
|
||
return -1; | ||
}; |