Skip to content

Commit 2b58e5a

Browse files
authored
Added tasks 1984, 1985, 2001, 2002, 2003.
1 parent 85afd0f commit 2b58e5a

File tree

15 files changed

+651
-0
lines changed

15 files changed

+651
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1901_2000.s1984_minimum_difference_between_highest_and_lowest_of_k_scores;
2+
3+
// #Easy #Array #Sorting #Sliding_Window #2022_05_22_Time_10_ms_(7.97%)_Space_47_MB_(57.18%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minimumDifference(int[] nums, int k) {
9+
Arrays.sort(nums);
10+
int minDiff = nums[nums.length - 1];
11+
for (int i = 0; i <= nums.length - k; i++) {
12+
minDiff = Math.min(minDiff, nums[i + k - 1] - nums[i]);
13+
}
14+
return minDiff;
15+
}
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
1984\. Minimum Difference Between Highest and Lowest of K Scores
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`, where `nums[i]` represents the score of the <code>i<sup>th</sup></code> student. You are also given an integer `k`.
6+
7+
Pick the scores of any `k` students from the array so that the **difference** between the **highest** and the **lowest** of the `k` scores is **minimized**.
8+
9+
Return _the **minimum** possible difference_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [90], k = 1
14+
15+
**Output:** 0
16+
17+
**Explanation:** There is one way to pick score(s) of one student:
18+
19+
- [**90**]. The difference between the highest and lowest score is 90 - 90 = 0.
20+
21+
The minimum possible difference is 0.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [9,4,1,7], k = 2
26+
27+
**Output:** 2
28+
29+
**Explanation:** There are six ways to pick score(s) of two students:
30+
31+
- [**9**,**4**,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
32+
33+
- [**9**,4,**1**,7]. The difference between the highest and lowest score is 9 - 1 = 8.
34+
35+
- [**9**,4,1,**7**]. The difference between the highest and lowest score is 9 - 7 = 2.
36+
37+
- [9,**4**,**1**,7]. The difference between the highest and lowest score is 4 - 1 = 3.
38+
39+
- [9,**4**,1,**7**]. The difference between the highest and lowest score is 7 - 4 = 3.
40+
41+
- [9,4,**1**,**7**]. The difference between the highest and lowest score is 7 - 1 = 6.
42+
43+
The minimum possible difference is 2.
44+
45+
**Constraints:**
46+
47+
* `1 <= k <= nums.length <= 1000`
48+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1901_2000.s1985_find_the_kth_largest_integer_in_the_array;
2+
3+
// #Medium #Array #String #Sorting #Heap_Priority_Queue #Divide_and_Conquer #Quickselect
4+
// #2022_05_22_Time_34_ms_(77.42%)_Space_65.7_MB_(20.14%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public String kthLargestNumber(String[] nums, int k) {
10+
Arrays.sort(nums, (n1, n2) -> compareStringInt(n2, n1));
11+
return nums[k - 1];
12+
}
13+
14+
private int compareStringInt(String n1, String n2) {
15+
if (n1.length() != n2.length()) {
16+
return n1.length() < n2.length() ? -1 : 1;
17+
}
18+
for (int i = 0; i < n1.length(); i++) {
19+
int n1Digit = n1.charAt(i) - '0';
20+
int n2Digit = n2.charAt(i) - '0';
21+
if (n1Digit > n2Digit) {
22+
return 1;
23+
} else if (n2Digit > n1Digit) {
24+
return -1;
25+
}
26+
}
27+
return 0;
28+
}
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
1985\. Find the Kth Largest Integer in the Array
2+
3+
Medium
4+
5+
You are given an array of strings `nums` and an integer `k`. Each string in `nums` represents an integer without leading zeros.
6+
7+
Return _the string that represents the_ <code>k<sup>th</sup></code> _**largest integer** in_ `nums`.
8+
9+
**Note**: Duplicate numbers should be counted distinctly. For example, if `nums` is `["1","2","2"]`, `"2"` is the first largest integer, `"2"` is the second-largest integer, and `"1"` is the third-largest integer.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = ["3","6","7","10"], k = 4
14+
15+
**Output:** "3"
16+
17+
**Explanation:**
18+
19+
The numbers in nums sorted in non-decreasing order are ["3","6","7","10"].
20+
21+
The 4<sup>th</sup> largest integer in nums is "3".
22+
23+
**Example 2:**
24+
25+
**Input:** nums = ["2","21","12","1"], k = 3
26+
27+
**Output:** "2"
28+
29+
**Explanation:**
30+
31+
The numbers in nums sorted in non-decreasing order are ["1","2","12","21"].
32+
33+
The 3<sup>rd</sup> largest integer in nums is "2".
34+
35+
**Example 3:**
36+
37+
**Input:** nums = ["0","0"], k = 2
38+
39+
**Output:** "0"
40+
41+
**Explanation:**
42+
43+
The numbers in nums sorted in non-decreasing order are ["0","0"].
44+
45+
The 2<sup>nd</sup> largest integer in nums is "0".
46+
47+
**Constraints:**
48+
49+
* <code>1 <= k <= nums.length <= 10<sup>4</sup></code>
50+
* `1 <= nums[i].length <= 100`
51+
* `nums[i]` consists of only digits.
52+
* `nums[i]` will not have any leading zeros.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2001_2100.s2001_number_of_pairs_of_interchangeable_rectangles;
2+
3+
// #Medium #Array #Hash_Table #Math #Counting #Number_Theory
4+
// #2022_05_22_Time_34_ms_(99.02%)_Space_121.4_MB_(69.27%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
static long factorial(long n) {
10+
long m = 0;
11+
12+
while (n > 0) {
13+
m += n;
14+
n = n - 1;
15+
}
16+
17+
return m;
18+
}
19+
20+
public long interchangeableRectangles(int[][] rec) {
21+
22+
double[] ratio = new double[rec.length];
23+
24+
for (int i = 0; i < rec.length; i++) {
25+
ratio[i] = (double) rec[i][0] / rec[i][1];
26+
}
27+
28+
Arrays.sort(ratio);
29+
30+
long res = 0;
31+
int k = 0;
32+
33+
for (int j = 0; j < ratio.length - 1; j++) {
34+
if (ratio[j] == ratio[j + 1]) {
35+
k++;
36+
}
37+
38+
if (ratio[j] != ratio[j + 1] || j + 2 == ratio.length) {
39+
res += factorial(k);
40+
k = 0;
41+
}
42+
}
43+
44+
return res;
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2001\. Number of Pairs of Interchangeable Rectangles
2+
3+
Medium
4+
5+
You are given `n` rectangles represented by a **0-indexed** 2D integer array `rectangles`, where <code>rectangles[i] = [width<sub>i</sub>, height<sub>i</sub>]</code> denotes the width and height of the <code>i<sup>th</sup></code> rectangle.
6+
7+
Two rectangles `i` and `j` (`i < j`) are considered **interchangeable** if they have the **same** width-to-height ratio. More formally, two rectangles are **interchangeable** if <code>width<sub>i</sub>/height<sub>i</sub> == width<sub>j</sub>/height<sub>j</sub></code> (using decimal division, not integer division).
8+
9+
Return _the **number** of pairs of **interchangeable** rectangles in_ `rectangles`.
10+
11+
**Example 1:**
12+
13+
**Input:** rectangles = [[4,8],[3,6],[10,20],[15,30]]
14+
15+
**Output:** 6
16+
17+
**Explanation:** The following are the interchangeable pairs of rectangles by index (0-indexed):
18+
19+
- Rectangle 0 with rectangle 1: 4/8 == 3/6.
20+
21+
- Rectangle 0 with rectangle 2: 4/8 == 10/20.
22+
23+
- Rectangle 0 with rectangle 3: 4/8 == 15/30.
24+
25+
- Rectangle 1 with rectangle 2: 3/6 == 10/20.
26+
27+
- Rectangle 1 with rectangle 3: 3/6 == 15/30.
28+
29+
- Rectangle 2 with rectangle 3: 10/20 == 15/30.
30+
31+
**Example 2:**
32+
33+
**Input:** rectangles = [[4,5],[7,8]]
34+
35+
**Output:** 0
36+
37+
**Explanation:** There are no interchangeable pairs of rectangles.
38+
39+
**Constraints:**
40+
41+
* `n == rectangles.length`
42+
* <code>1 <= n <= 10<sup>5</sup></code>
43+
* `rectangles[i].length == 2`
44+
* <code>1 <= width<sub>i</sub>, height<sub>i</sub> <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package g2001_2100.s2002_maximum_product_of_the_length_of_two_palindromic_subsequences;
2+
3+
// #Medium #String #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2022_05_22_Time_69_ms_(89.94%)_Space_53.8_MB_(41.42%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
public class Solution {
12+
public int maxProduct(String s) {
13+
if (s.length() == 2) {
14+
return 1;
15+
}
16+
List<State> list = new ArrayList<>();
17+
char[] chars = s.toCharArray();
18+
Set<State> visited = new HashSet<>();
19+
for (int i = 0; i < chars.length; ++i) {
20+
int mask = 1 << i;
21+
recur(chars, new State(i, i, 0, mask), list, visited);
22+
recur(chars, new State(i, i + 1, 0, mask), list, visited);
23+
}
24+
list.sort((a, b) -> b.cnt - a.cnt);
25+
int res = 1;
26+
Set<Integer> explored = new HashSet<>();
27+
for (int i = 0; i < list.size() - 1; ++i) {
28+
if (explored.contains(i)) {
29+
continue;
30+
}
31+
State cur = list.get(i);
32+
if (cur.cnt == 1) {
33+
break;
34+
}
35+
36+
for (int j = i + 1; j < list.size(); ++j) {
37+
State cand = list.get(j);
38+
if ((cur.mask & cand.mask) < 1) {
39+
if (explored.add(j)) {
40+
res = Math.max(res, cur.cnt * cand.cnt);
41+
}
42+
break;
43+
}
44+
}
45+
}
46+
return res;
47+
}
48+
49+
private void recur(char[] chars, State s, List<State> list, Set<State> visited) {
50+
if (s.i < 0 || s.j >= chars.length) {
51+
return;
52+
}
53+
if (!visited.add(s)) {
54+
return;
55+
}
56+
57+
if (chars[s.i] == chars[s.j]) {
58+
int m = s.mask | (1 << s.i) | (1 << s.j);
59+
int nextCnt = s.cnt + (s.i < s.j ? 2 : 1);
60+
list.add(new State(s.i, s.j, nextCnt, m));
61+
recur(chars, new State(s.i - 1, s.j + 1, nextCnt, m), list, visited);
62+
}
63+
recur(chars, new State(s.i - 1, s.j, s.cnt, s.mask), list, visited);
64+
recur(chars, new State(s.i, s.j + 1, s.cnt, s.mask), list, visited);
65+
}
66+
67+
private static class State {
68+
int i;
69+
int j;
70+
int cnt;
71+
int mask;
72+
73+
public State(int i, int j, int cnt, int mask) {
74+
this.i = i;
75+
this.j = j;
76+
this.cnt = cnt;
77+
this.mask = mask;
78+
}
79+
80+
@Override
81+
public boolean equals(Object o) {
82+
if (o == null || o.getClass() != this.getClass()) {
83+
return false;
84+
}
85+
State s = (State) o;
86+
return this.i == s.i && this.j == s.j && this.mask == s.mask;
87+
}
88+
89+
@Override
90+
public int hashCode() {
91+
return (this.i * 31 + this.j) * 31 + this.mask;
92+
}
93+
}
94+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2001\. Number of Pairs of Interchangeable Rectangles
2+
3+
Medium
4+
5+
You are given `n` rectangles represented by a **0-indexed** 2D integer array `rectangles`, where <code>rectangles[i] = [width<sub>i</sub>, height<sub>i</sub>]</code> denotes the width and height of the <code>i<sup>th</sup></code> rectangle.
6+
7+
Two rectangles `i` and `j` (`i < j`) are considered **interchangeable** if they have the **same** width-to-height ratio. More formally, two rectangles are **interchangeable** if <code>width<sub>i</sub>/height<sub>i</sub> == width<sub>j</sub>/height<sub>j</sub></code> (using decimal division, not integer division).
8+
9+
Return _the **number** of pairs of **interchangeable** rectangles in_ `rectangles`.
10+
11+
**Example 1:**
12+
13+
**Input:** rectangles = [[4,8],[3,6],[10,20],[15,30]]
14+
15+
**Output:** 6
16+
17+
**Explanation:** The following are the interchangeable pairs of rectangles by index (0-indexed):
18+
19+
- Rectangle 0 with rectangle 1: 4/8 == 3/6.
20+
21+
- Rectangle 0 with rectangle 2: 4/8 == 10/20.
22+
23+
- Rectangle 0 with rectangle 3: 4/8 == 15/30.
24+
25+
- Rectangle 1 with rectangle 2: 3/6 == 10/20.
26+
27+
- Rectangle 1 with rectangle 3: 3/6 == 15/30.
28+
29+
- Rectangle 2 with rectangle 3: 10/20 == 15/30.
30+
31+
**Example 2:**
32+
33+
**Input:** rectangles = [[4,5],[7,8]]
34+
35+
**Output:** 0
36+
37+
**Explanation:** There are no interchangeable pairs of rectangles.
38+
39+
**Constraints:**
40+
41+
* `n == rectangles.length`
42+
* <code>1 <= n <= 10<sup>5</sup></code>
43+
* `rectangles[i].length == 2`
44+
* <code>1 <= width<sub>i</sub>, height<sub>i</sub> <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)