Skip to content

Commit 01784b9

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 4b43ccd + ccaaf58 commit 01784b9

File tree

21 files changed

+948
-28
lines changed

21 files changed

+948
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.longluo.contest.biweekly_contest_108;
2+
3+
/**
4+
* https://leetcode.cn/contest/biweekly-contest-108
5+
*/
6+
public class Problem1 {
7+
8+
public static int alternatingSubarray(int[] nums) {
9+
int n = nums.length;
10+
11+
int ans = 0;
12+
13+
for (int i = 0; i < n; i++) {
14+
int prev = nums[i];
15+
int cnt = 0;
16+
int step = 1;
17+
18+
for (int j = i + 1; j < n; j++) {
19+
if (nums[j] != prev + step) {
20+
break;
21+
}
22+
23+
prev = nums[j];
24+
cnt++;
25+
step *= -1;
26+
}
27+
28+
if (cnt > 0) {
29+
ans = Math.max(ans, cnt + 1);
30+
}
31+
}
32+
33+
return ans == 0 ? -1 : ans;
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println("2 ?= " + alternatingSubarray(new int[]{4, 5, 6}));
38+
System.out.println("4 ?= " + alternatingSubarray(new int[]{2, 3, 4, 3, 4}));
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.longluo.contest.biweekly_contest_108;
2+
3+
import java.util.*;
4+
5+
/**
6+
* https://leetcode.cn/contest/biweekly-contest-108
7+
*/
8+
public class Problem2 {
9+
10+
public static List<Integer> relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) {
11+
Map<Integer, Integer> posMap = new HashMap<>();
12+
13+
for (int x : nums) {
14+
posMap.put(x, posMap.getOrDefault(x, 0) + 1);
15+
}
16+
17+
for (int i = 0; i < moveFrom.length; i++) {
18+
int from = moveFrom[i];
19+
int to = moveTo[i];
20+
21+
if (from == to) {
22+
continue;
23+
}
24+
25+
posMap.put(to, posMap.getOrDefault(to, 0) + posMap.get(from));
26+
posMap.remove(from);
27+
}
28+
29+
List<Integer> ans = new ArrayList<>();
30+
31+
for (int key : posMap.keySet()) {
32+
ans.add(key);
33+
}
34+
35+
Collections.sort(ans);
36+
37+
return ans;
38+
}
39+
40+
public static void main(String[] args) {
41+
System.out.println("[1] ?= " + relocateMarbles(new int[]{3, 4}, new int[]{4, 3, 1, 2, 2, 3, 2, 4, 1}, new int[]{3, 1, 2, 2, 3, 2, 4, 1, 1}));
42+
System.out.println("[11, 20, 23] ?= " + relocateMarbles(new int[]{4, 6, 6, 9, 18}, new int[]{18, 6, 17, 4, 9, 19, 2}, new int[]{23, 17, 20, 19, 11, 2, 20}));
43+
System.out.println("[5, 6, 8, 9] ?= " + relocateMarbles(new int[]{1, 6, 7, 8}, new int[]{1, 7, 2}, new int[]{2, 9, 5}));
44+
System.out.println("[2] ?= " + relocateMarbles(new int[]{1, 1, 3, 3}, new int[]{1, 3}, new int[]{2, 2}));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.longluo.contest.biweekly_contest_109;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* https://leetcode.cn/contest/biweekly-contest-109
7+
*/
8+
public class Problem1 {
9+
10+
public static boolean isGood(int[] nums) {
11+
Arrays.sort(nums);
12+
13+
int len = nums.length;
14+
15+
int n = nums[len - 1];
16+
if (n != len - 1) {
17+
return false;
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
if (nums[i] != (i + 1)) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
29+
public static void main(String[] args) {
30+
System.out.println("true ?= " + isGood(new int[]{1, 1}));
31+
System.out.println("false ?= " + isGood(new int[]{2, 1, 3}));
32+
System.out.println("true ?= " + isGood(new int[]{1, 3, 3, 2}));
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.longluo.contest.biweekly_contest_109;
2+
3+
import java.util.*;
4+
5+
/**
6+
* https://leetcode.cn/contest/biweekly-contest-109
7+
*/
8+
public class Problem2 {
9+
10+
public static String sortVowels(String s) {
11+
Set<Character> vowels = new HashSet<>();
12+
13+
for (char ch : "AEIOUaeiou".toCharArray()) {
14+
vowels.add(ch);
15+
}
16+
17+
List<Character> list = new ArrayList<>();
18+
19+
for (char ch : s.toCharArray()) {
20+
if (vowels.contains(ch)) {
21+
list.add(ch);
22+
}
23+
}
24+
25+
if (list.size() == 0) {
26+
return s;
27+
}
28+
29+
Collections.sort(list);
30+
31+
StringBuilder sb = new StringBuilder();
32+
33+
int idx = 0;
34+
35+
for (char ch : s.toCharArray()) {
36+
if (vowels.contains(ch)) {
37+
sb.append(list.get(idx));
38+
idx++;
39+
} else {
40+
sb.append(ch);
41+
}
42+
}
43+
44+
return sb.toString();
45+
}
46+
47+
public static void main(String[] args) {
48+
System.out.println("lEOtcede ?= " + sortVowels("lEetcOde"));
49+
System.out.println("lYmpH ?= " + sortVowels("lYmpH"));
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.longluo.contest.biweekly_contest_110;
2+
3+
/**
4+
* https://leetcode.cn/contest/biweekly-contest-110
5+
*/
6+
public class Problem1 {
7+
8+
public static int accountBalanceAfterPurchase(int purchaseAmount) {
9+
int shiwei = purchaseAmount / 10;
10+
int gewei = purchaseAmount % 10;
11+
12+
if (gewei >= 5) {
13+
shiwei++;
14+
}
15+
16+
return 100 - 10 * shiwei;
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println("90 ?= " + accountBalanceAfterPurchase(9));
21+
System.out.println("80 ?= " + accountBalanceAfterPurchase(15));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.longluo.contest.biweekly_contest_110;
2+
3+
import com.longluo.datastructure.ListNode;
4+
import com.longluo.datastructure.Node;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* https://leetcode.cn/contest/biweekly-contest-110
11+
*/
12+
public class Problem2 {
13+
14+
public static ListNode insertGreatestCommonDivisors(ListNode head) {
15+
List<Integer> nums = new ArrayList<>();
16+
17+
ListNode pNode = head;
18+
19+
while (pNode != null) {
20+
nums.add(pNode.val);
21+
pNode = pNode.next;
22+
}
23+
24+
for (int i = nums.size() - 1; i > 0; i--) {
25+
int first = nums.get(i);
26+
int second = nums.get(i - 1);
27+
28+
int result = gcd(first, second);
29+
nums.add(i, result);
30+
}
31+
32+
pNode = head;
33+
34+
for (int i = 1; i < nums.size(); i++) {
35+
pNode.next = new ListNode(nums.get(i));
36+
pNode = pNode.next;
37+
}
38+
39+
return head;
40+
}
41+
42+
private static int gcd(int a, int b) {
43+
return b == 0 ? a : gcd(b, a % b);
44+
}
45+
46+
public static void main(String[] args) {
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.longluo.contest.biweekly_contest_111;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* https://leetcode.cn/contest/biweekly-contest-111
8+
*/
9+
public class Problem1 {
10+
11+
public static int countPairs(List<Integer> nums, int target) {
12+
int n = nums.size();
13+
14+
int ans = 0;
15+
16+
for (int i = 0; i < n; i++) {
17+
for (int j = i + 1; j < n; j++) {
18+
if (nums.get(i) + nums.get(j) < target) {
19+
ans++;
20+
}
21+
}
22+
}
23+
24+
return ans;
25+
}
26+
27+
public static void main(String[] args) {
28+
List<Integer> tst1 = new ArrayList<>();
29+
tst1.add(-1);
30+
tst1.add(1);
31+
tst1.add(2);
32+
tst1.add(3);
33+
tst1.add(1);
34+
35+
System.out.println("3 ?= " + countPairs(tst1, 2));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.longluo.contest.biweekly_contest_112;
2+
3+
/**
4+
* https://leetcode.cn/contest/biweekly-contest-112
5+
*/
6+
public class Problem1 {
7+
8+
public static boolean canBeEqual(String s1, String s2) {
9+
if (s1.equals(s2)) {
10+
return true;
11+
}
12+
13+
int[] cnt1 = new int[26];
14+
15+
int[] cnt2 = new int[26];
16+
17+
for (int i = 0; i < 4; i++) {
18+
cnt1[s1.charAt(i) - 'a']++;
19+
cnt2[s2.charAt(i) - 'a']++;
20+
}
21+
22+
for (int i = 0; i < 26; i++) {
23+
if (cnt1[i] != cnt2[i]) {
24+
return false;
25+
}
26+
}
27+
28+
for (int i = 0; i < 4; i++) {
29+
char a1 = s1.charAt(i);
30+
char a2 = s2.charAt(i);
31+
32+
if (a1 == a2) {
33+
continue;
34+
}
35+
36+
boolean flag = false;
37+
38+
for (int j = 0; j < 4; j++) {
39+
if (s2.charAt(j) == a1 && Math.abs(i - j) == 2) {
40+
flag = true;
41+
break;
42+
}
43+
}
44+
45+
if (!flag) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
53+
public static boolean canBeEqual_bf(String s1, String s2) {
54+
boolean[] masks = new boolean[4];
55+
56+
for (int i = 0; i < 4; i++) {
57+
char ch = s1.charAt(i);
58+
59+
boolean flag = false;
60+
61+
for (int j = 0; j < 4; j += 2) {
62+
int loc = (i + j) % 4;
63+
64+
if (!masks[loc] && s2.charAt(loc) == ch) {
65+
flag = true;
66+
masks[loc] = true;
67+
break;
68+
}
69+
}
70+
71+
if (!flag) {
72+
return false;
73+
}
74+
}
75+
76+
return true;
77+
}
78+
79+
public static void main(String[] args) {
80+
System.out.println("true ?= " + canBeEqual("abcd", "cdab"));
81+
System.out.println("false ?= " + canBeEqual("abcd", "dacb"));
82+
83+
System.out.println("true ?= " + canBeEqual_bf("abcd", "abcd"));
84+
System.out.println("true ?= " + canBeEqual_bf("abcd", "cdab"));
85+
System.out.println("false ?= " + canBeEqual_bf("abcd", "dacb"));
86+
}
87+
}

0 commit comments

Comments
 (0)