Skip to content

Commit cc565d7

Browse files
authored
Added tasks 598, 599, 600.
1 parent 547196b commit cc565d7

File tree

9 files changed

+257
-0
lines changed

9 files changed

+257
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0501_0600.s0598_range_addition_ii;
2+
3+
// #Easy #Array #Math
4+
5+
public class Solution {
6+
/*
7+
* Since the incrementing starts from zero to op[0] and op[1], we only need to find the range that
8+
* has the most overlaps. Thus we keep finding the minimum of both x and y.
9+
*/
10+
public int maxCount(int m, int n, int[][] ops) {
11+
int x = m;
12+
int y = n;
13+
for (int[] op : ops) {
14+
x = Math.min(x, op[0]);
15+
y = Math.min(y, op[1]);
16+
}
17+
return x * y;
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
598\. Range Addition II
2+
3+
Easy
4+
5+
You are given an `m x n` matrix `M` initialized with all `0`'s and an array of operations `ops`, where <code>ops[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> means `M[x][y]` should be incremented by one for all <code>0 <= x < a<sub>i</sub></code> and <code>0 <= y < b<sub>i</sub></code>.
6+
7+
Count and return _the number of maximum integers in the matrix after performing all the operations_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/ex1.jpg)
12+
13+
**Input:** m = 3, n = 3, ops = [[2,2],[3,3]]
14+
15+
**Output:** 4
16+
17+
**Explanation:** The maximum integer in M is 2, and there are four of it in M. So return 4.
18+
19+
**Example 2:**
20+
21+
**Input:** m = 3, n = 3, ops = [[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3]]
22+
23+
**Output:** 4
24+
25+
**Example 3:**
26+
27+
**Input:** m = 3, n = 3, ops = []
28+
29+
**Output:** 9
30+
31+
**Constraints:**
32+
33+
* <code>1 <= m, n <= 4 * 10<sup>4</sup></code>
34+
* <code>0 <= ops.length <= 10<sup>4</sup></code>
35+
* `ops[i].length == 2`
36+
* <code>1 <= a<sub>i</sub> <= m</code>
37+
* <code>1 <= b<sub>i</sub> <= n</code>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0501_0600.s0599_minimum_index_sum_of_two_lists;
2+
3+
// #Easy #Array #String #Hash_Table
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class Solution {
11+
public String[] findRestaurant(String[] list1, String[] list2) {
12+
int min = 1000000;
13+
Map<String, Integer> hm = new HashMap<>();
14+
List<String> result = new ArrayList<>();
15+
fillMap(list1, hm);
16+
// find min value
17+
for (int i = 0; i < list2.length; i++) {
18+
if (hm.containsKey(list2[i])) {
19+
int value = hm.get(list2[i]) + i;
20+
// a new min value was found
21+
if (value < min) {
22+
min = value;
23+
// Clean the arraylist
24+
result.clear();
25+
// add new min value
26+
result.add(list2[i]);
27+
} else if (value == min) {
28+
result.add(list2[i]);
29+
}
30+
}
31+
}
32+
return result.toArray(new String[result.size()]);
33+
}
34+
35+
public void fillMap(String[] a, Map<String, Integer> hm) {
36+
for (int i = 0; i < a.length; i++) {
37+
hm.put(a[i], i);
38+
}
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
599\. Minimum Index Sum of Two Lists
2+
3+
Easy
4+
5+
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
6+
7+
You need to help them find out their **common interest** with the **least list index sum**. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
8+
9+
**Example 1:**
10+
11+
**Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
12+
13+
**Output:** ["Shogun"]
14+
15+
**Explanation:** The only restaurant they both like is "Shogun".
16+
17+
**Example 2:**
18+
19+
**Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
20+
21+
**Output:** ["Shogun"]
22+
23+
**Explanation:** The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
24+
25+
**Constraints:**
26+
27+
* `1 <= list1.length, list2.length <= 1000`
28+
* `1 <= list1[i].length, list2[i].length <= 30`
29+
* `list1[i]` and `list2[i]` consist of spaces `' '` and English letters.
30+
* All the stings of `list1` are **unique**.
31+
* All the stings of `list2` are **unique**.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0501_0600.s0600_non_negative_integers_without_consecutive_ones;
2+
3+
// #Hard #Dynamic_Programming
4+
5+
public class Solution {
6+
public int findIntegers(int n) {
7+
int[] f = new int[32];
8+
f[0] = 1;
9+
f[1] = 2;
10+
int ans = 0;
11+
int preBit = 0;
12+
for (int i = 2; i < 32; i++) {
13+
f[i] = f[i - 1] + f[i - 2];
14+
}
15+
for (int k = 31; k >= 0; k--) {
16+
if ((n & (1 << k)) != 0) {
17+
// if that bit is on
18+
ans += f[k];
19+
if (preBit != 0) {
20+
return ans;
21+
}
22+
preBit = 1;
23+
} else {
24+
preBit = 0;
25+
}
26+
}
27+
return ans + 1;
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
600\. Non-negative Integers without Consecutive Ones
2+
3+
Hard
4+
5+
Given a positive integer `n`, return the number of the integers in the range `[0, n]` whose binary representations **do not** contain consecutive ones.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 5
10+
11+
**Output:** 5
12+
13+
**Explanation:**
14+
15+
Here are the non-negative integers <= 5 with their corresponding binary representations:
16+
0 : 0
17+
1 : 1
18+
2 : 10
19+
3 : 11
20+
4 : 100
21+
5 : 101
22+
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 1
27+
28+
**Output:** 2
29+
30+
**Example 3:**
31+
32+
**Input:** n = 2
33+
34+
**Output:** 3
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0598_range_addition_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.CommonUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void maxCount() {
12+
assertThat(
13+
new Solution()
14+
.maxCount(
15+
3,
16+
3,
17+
CommonUtils
18+
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
19+
"[2,2],[3,3]")),
20+
equalTo(4));
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0501_0600.s0599_minimum_index_sum_of_two_lists;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findRestaurant() {
11+
assertThat(
12+
new Solution()
13+
.findRestaurant(
14+
new String[] {"Shogun", "Tapioca Express", "Burger King", "KFC"},
15+
new String[] {
16+
"Piatti",
17+
"The Grill at Torrey Pines",
18+
"Hungry Hunter Steakhouse",
19+
"Shogun"
20+
}),
21+
equalTo(new String[] {"Shogun"}));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0501_0600.s0600_non_negative_integers_without_consecutive_ones;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findIntegers() {
11+
assertThat(new Solution().findIntegers(5), equalTo(5));
12+
}
13+
14+
@Test
15+
void findIntegers2() {
16+
assertThat(new Solution().findIntegers(100000000), equalTo(514229));
17+
}
18+
}

0 commit comments

Comments
 (0)