Skip to content

Commit 4e0c2f8

Browse files
solves #2540: Minimum Common Value in java
1 parent 5fe1765 commit 4e0c2f8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@
795795
| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria) | [![Java](assets/java.png)](src/CategorizeBoxAccordingToCriteria.java) | |
796796
| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) | [![Java](assets/java.png)](src/MaximumCountOfPositiveIntegerAndNegativeInteger.java) | |
797797
| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array) | [![Java](assets/java.png)](src/DifferenceBetweenElementSumAndDigitSumOfAnArray.java) | |
798-
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value) | | |
798+
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value) | [![Java](assets/java.png)](src/MinimumCommonValue.java) | |
799799
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum) | | |
800800
| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board) | | |
801801
| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array) | | |

src/MinimumCommonValue.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/minimum-common-value
2+
// T: O(m + n)
3+
// S: O(1)
4+
5+
public class MinimumCommonValue {
6+
public int getCommon(int[] array1, int[] array2) {
7+
for (int i = 0, j = 0 ; i < array1.length && j < array2.length ; ) {
8+
if (array1[i] == array2[j]) return array1[i];
9+
10+
if (array1[i] < array2[j]) {
11+
i++;
12+
} else {
13+
j++;
14+
}
15+
}
16+
return -1;
17+
}
18+
}

0 commit comments

Comments
 (0)