Skip to content

Commit c1b74c5

Browse files
committed
weekly-239
Signed-off-by: ashKIK <[email protected]>
1 parent 42e7129 commit c1b74c5

4 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-239/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/
3+
4+
public class MinimumAdjacentSwapsToReachTheKthSmallestNumber {
5+
6+
public int getMinSwaps(String num, int k) {
7+
char[] original = num.toCharArray();
8+
char[] nums = original.clone();
9+
while (k-- > 0) {
10+
nextPermutation(nums);
11+
}
12+
int ans = 0;
13+
for (int i = 0; i < nums.length; i++) {
14+
int j = i + 1;
15+
while (original[i] != nums[i]) {
16+
ans++;
17+
swap(nums, i, j++);
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
public void nextPermutation(char[] nums) {
24+
if (nums.length <= 1) {
25+
return;
26+
}
27+
int i = nums.length - 2;
28+
while (i >= 0 && nums[i] >= nums[i + 1]) {
29+
i--;
30+
}
31+
if (i >= 0) {
32+
int j = nums.length - 1;
33+
while (j > i && nums[j] <= nums[i]) {
34+
j--;
35+
}
36+
swap(nums, i, j);
37+
}
38+
reverse(nums, i + 1, nums.length - 1);
39+
}
40+
41+
private void reverse(char[] nums, int left, int right) {
42+
while (left < right) {
43+
swap(nums, left++, right--);
44+
}
45+
}
46+
47+
private void swap(char[] nums, int i, int j) {
48+
char temp = nums[j];
49+
nums[j] = nums[i];
50+
nums[i] = temp;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-239/problems/minimum-distance-to-the-target-element/
3+
4+
public class MinimumDistanceToTheTargetElement {
5+
6+
public int getMinDistance(int[] nums, int target, int start) {
7+
int min = Integer.MAX_VALUE;
8+
for (int i = 0; i < nums.length; i++) {
9+
if (nums[i] == target) {
10+
min = Math.min(min, Math.abs(i - start));
11+
}
12+
}
13+
return min;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
import java.util.TreeMap;
7+
8+
// https://leetcode.com/contest/weekly-contest-239/problems/minimum-interval-to-include-each-query/
9+
10+
public class MinimumIntervalToIncludeEachQuery {
11+
12+
public int[] minInterval(int[][] intervals, int[] queries) {
13+
Arrays.sort(intervals, (a, b) -> (b[0] - a[0] - (b[1] - a[1])));
14+
TreeMap<Integer, List<Integer>> treeMap = new TreeMap<>();
15+
for (int i = 0; i < queries.length; i++) {
16+
treeMap.computeIfAbsent(queries[i], a -> new ArrayList<>()).add(i);
17+
}
18+
int[] result = new int[queries.length];
19+
Arrays.fill(result, -1);
20+
for (int[] interval : intervals) {
21+
int left = interval[0];
22+
int right = interval[1];
23+
Map<Integer, List<Integer>> sub =
24+
treeMap.subMap(left, true, right, true);
25+
for (List<Integer> list : sub.values()) {
26+
for (int i : list) {
27+
result[i] = right - left + 1;
28+
}
29+
}
30+
treeMap.keySet().removeAll(Set.copyOf(sub.keySet()));
31+
}
32+
return result;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-239/problems/splitting-a-string-into-descending-consecutive-values/
3+
4+
public class SplittingAStringIntoDescendingConsecutiveValues {
5+
6+
// TC: O(n*log(n))
7+
public boolean splitString(String s) {
8+
int start = 0;
9+
int end = s.length() - 1;
10+
while (start < end) {
11+
if (binarySearch(s, end, eval(s, start, end) - 1)) {
12+
return true;
13+
}
14+
end--;
15+
}
16+
return false;
17+
}
18+
19+
private long eval(String s, int start, int end) {
20+
long num = 0;
21+
for (int i = start; i < end; i++) {
22+
num = num * 10 + (long) (s.charAt(i) - '0');
23+
}
24+
return num;
25+
}
26+
27+
private boolean binarySearch(String s, int index, long target) {
28+
if (eval(s, index, s.length()) == target) {
29+
return true;
30+
}
31+
int left = index;
32+
int right = s.length();
33+
while (left < right) {
34+
int mid = right - (right - left) / 2;
35+
long num = eval(s, index, mid);
36+
if (num == target) {
37+
return binarySearch(s, mid, target - 1);
38+
}
39+
if (num > target) {
40+
right = mid - 1;
41+
} else {
42+
left = mid;
43+
}
44+
}
45+
return false;
46+
}
47+
48+
// TC: O(n^2)
49+
public boolean splitString2(String s) {
50+
return dfs(s, 0, 0, 0);
51+
}
52+
53+
private boolean dfs(String s, int pos, long curVal, long lastVal) {
54+
if (pos == s.length()) {
55+
return lastVal != 0;
56+
}
57+
if (curVal == 0 && lastVal != 0) {
58+
return false;
59+
}
60+
long val = 0;
61+
for (int i = pos; i < s.length(); i++) {
62+
val = val * 10 + (s.charAt(i) - '0');
63+
if (curVal == 0 || curVal == val + 1) {
64+
if (dfs(s, i + 1, val, curVal)) {
65+
return true;
66+
}
67+
}
68+
}
69+
return false;
70+
}
71+
}

0 commit comments

Comments
 (0)