-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumCommonValue.php
More file actions
44 lines (42 loc) · 1.21 KB
/
Copy pathMinimumCommonValue.php
File metadata and controls
44 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace App;
/**
* Minimum Common Value
*
* Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both
* arrays. If there is no common integer amongst nums1 and nums2, return -1. Note that an integer is said to be common
* to nums1 and nums2 if both arrays have at least one occurrence of that integer.
*
* Example 1:
* Input: nums1 = [1,2,3], nums2 = [2,4]
* Output: 2
* Explanation: The smallest element common to both arrays is 2, so we return 2.
*
* Example 2:
* Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
* Output: 2
* Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
*
* https://leetcode.com/problems/minimum-common-value
*/
class MinimumCommonValue
{
/**
* @param int[] $nums1
* @param int[] $nums2
* @return int
*/
public function getCommon(array $nums1, array $nums2): int
{
$hashNums1 = [];
foreach ($nums1 as $num1) {
$hashNums1[$num1] = true;
}
foreach ($nums2 as $num2) {
if (isset($hashNums1[$num2])) {
return $num2;
}
}
return -1;
}
}