Skip to content

Commit 54a3194

Browse files
committed
2023-01-23 update: added "2540. Minimum Common Value"
1 parent 8700284 commit 54a3194

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
443443
| 2525. Categorize Box According to Criteria | [Link](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/CategorizeBoxAccordingToCriteria.java) |
444444
| 2529. Maximum Count of Positive Integer and Negative Integer | [Link](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MaximumCountOfPositiveIntegerAndNegativeInteger.java) |
445445
| 2535. Difference Between Element Sum and Digit Sum of an Array | [Link](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/description/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/DifferenceBetweenElementSumAndDigitSumOfAnArray.java) |
446+
| 2540. Minimum Common Value | [Link](https://leetcode.com/problems/minimum-common-value/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MinimumCommonValue.java) |
446447

447448
### Medium
448449

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/minimum-common-value/
4+
public class MinimumCommonValue {
5+
6+
private final int[] nums1;
7+
private final int[] nums2;
8+
9+
public MinimumCommonValue(int[] nums1, int[] nums2) {
10+
this.nums1 = nums1;
11+
this.nums2 = nums2;
12+
}
13+
14+
public int solution() {
15+
int lastMin = 0;
16+
for (int num : nums1) {
17+
for (int i = lastMin; i < nums2.length; i++) {
18+
int curr = nums2[i];
19+
if (curr == num) {
20+
return num;
21+
}
22+
if (curr > num) {
23+
lastMin = i;
24+
break;
25+
}
26+
}
27+
}
28+
return -1;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class MinimumCommonValueTest {
8+
9+
@Test
10+
public void defaultTests() {
11+
assertEquals(
12+
2,
13+
new MinimumCommonValue(
14+
new int[]{1, 2, 3},
15+
new int[]{2, 4}
16+
).solution()
17+
);
18+
assertEquals(
19+
2,
20+
new MinimumCommonValue(
21+
new int[]{1, 2, 3, 6},
22+
new int[]{2, 3, 4, 5}
23+
).solution()
24+
);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)