Skip to content

Commit f3d09d7

Browse files
add 1099
1 parent 694eb0f commit f3d09d7

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Your ideas/fixes/algorithms are more than welcome!
4343
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
4444
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
4545
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | | | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
46+
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | | | |Easy||
4647
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
4748
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | | | |Easy||
4849
|1085|[Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1085.java) | | | |Easy||
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1099. Two Sum Less Than K
5+
*
6+
* Given an array A of integers and integer K,
7+
* return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K.
8+
* If no i, j exist satisfying this equation, return -1.
9+
*
10+
* Example 1:
11+
* Input: A = [34,23,1,24,75,33,54,8], K = 60
12+
* Output: 58
13+
* Explanation:
14+
* We can use 34 and 24 to sum 58 which is less than 60.
15+
*
16+
* Example 2:
17+
* Input: A = [10,20,30], K = 15
18+
* Output: -1
19+
* Explanation:
20+
* In this case it's not possible to get a pair sum less that 15.
21+
*
22+
* Note:
23+
* 1 <= A.length <= 100
24+
* 1 <= A[i] <= 1000
25+
* 1 <= K <= 2000
26+
* */
27+
public class _1099 {
28+
public static class Solution1 {
29+
public int twoSumLessThanK(int[] A, int K) {
30+
int maxSum = Integer.MIN_VALUE;
31+
for (int i = 0; i < A.length - 1; i++) {
32+
for (int j = i + 1; j < A.length; j++) {
33+
if (A[i] + A[j] < K) {
34+
maxSum = Math.max(maxSum, A[i] + A[j]);
35+
}
36+
}
37+
}
38+
return maxSum == Integer.MIN_VALUE ? -1 : maxSum;
39+
}
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1099;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1099Test {
10+
private static _1099.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1099.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(58, solution1.twoSumLessThanK(new int[]{34, 23, 1, 24, 75, 33, 54, 8}, 60));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.twoSumLessThanK(new int[]{10, 20, 30}, 15));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy