Skip to content

Improved tasks 1903, 1993, 2062, 2111, 2272 #1714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package g1901_2000.s1903_largest_odd_number_in_string;

// #Easy #String #Math #Greedy #2022_05_11_Time_6_ms_(23.18%)_Space_43.2_MB_(81.76%)
// #Easy #String #Math #Greedy #2024_03_29_Time_1_ms_(100.00%)_Space_45.3_MB_(55.03%)

public class Solution {
public String largestOddNumber(String num) {
String str = "";
for (int i = num.length() - 1; i >= 0; i--) {
if (Integer.parseInt("" + num.charAt(i)) % 2 == 1) {
return num.substring(0, i + 1);
char c = num.charAt(i);
if (c % 2 == 1) {
str = num.substring(0, i + 1);
break;
}
}
return "";
return str;
}
}
131 changes: 76 additions & 55 deletions src/main/java/g1901_2000/s1993_operations_on_tree/LockingTree.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,103 @@
package g1901_2000.s1993_operations_on_tree;

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Tree #Design
// #2022_05_19_Time_394_ms_(23.03%)_Space_167.4_MB_(5.26%)
// #2024_03_29_Time_58_ms_(99.38%)_Space_47.6_MB_(83.13%)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@SuppressWarnings("unchecked")
public class LockingTree {
private int[][] a;
private HashMap<Integer, List<Integer>> map = new HashMap<>();
private List<Integer>[] graph;
private boolean[] locked;
private int[] parent;
private int[] users;
private int[] control;

public LockingTree(int[] parent) {
int l = parent.length;
a = new int[l][2];
for (int i = 0; i < l; i++) {
a[i][0] = parent[i];
a[i][1] = -1;
map.putIfAbsent(parent[i], new ArrayList<>());
List<Integer> p = map.get(parent[i]);
p.add(i);
map.put(parent[i], p);
int n = parent.length;
this.parent = parent;
graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
graph[parent[i]].add(i);
}
locked = new boolean[n];
users = new int[n];
control = new int[n];
}

public boolean lock(int num, int user) {
int userId = a[num][1];
if (userId == -1) {
a[num][1] = user;
return true;
}
return false;
private void setLock(int id, int user) {
locked[id] = true;
users[id] = user;
}

public boolean unlock(int num, int user) {
int y = a[num][1];
if (y == user) {
a[num][1] = -1;
return true;
private void subNodeUnlock(int id) {
for (int child : graph[id]) {
locked[child] = false;
if (control[child] <= 0) {
continue;
}
control[child] = 0;
subNodeUnlock(child);
}
return false;
}

public boolean upgrade(int num, int user) {
int par = num;
while (par >= 0) {
int lop = a[par][1];
if (lop != -1) {
return false;
}
par = a[par][0];
public boolean lock(int id, int user) {
if (locked[id]) {
return false;
}
int f = 0;
LinkedList<Integer> que = new LinkedList<>();
int[] v = new int[a.length];
que.add(num);
v[num] = 1;
while (!que.isEmpty()) {
int t = que.get(0);
que.remove(0);
List<Integer> p = map.getOrDefault(t, new ArrayList<>());
for (int e : p) {
if (a[e][1] != -1) {
f = 1;
a[e][1] = -1;
setLock(id, user);
if (control[id] == 0) {
int node = parent[id];
while (node != -1) {
control[node]++;
if (locked[node] || control[node] > 1) {
break;
}
if (v[e] == 0) {
que.add(e);
v[e] = 1;
node = parent[node];
}
}
return true;
}

public boolean unlock(int id, int user) {
if (!locked[id] || users[id] != user) {
return false;
}
locked[id] = false;
if (control[id] == 0) {
int node = parent[id];
while (node != -1) {
control[node]--;
if (locked[node] || control[node] >= 1) {
break;
}
node = parent[node];
}
}
return true;
}

public boolean upgrade(int id, int user) {
if (locked[id] || control[id] == 0) {
return false;
}
int cur = parent[id];
while (cur != -1) {
if (locked[cur]) {
return false;
}
cur = parent[cur];
}
if (f == 1) {
a[num][1] = user;
return true;
setLock(id, user);
if (control[id] > 0) {
control[id] = 0;
subNodeUnlock(id);
}
return false;
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
package g2001_2100.s2062_count_vowel_substrings_of_a_string;

// #Easy #String #Hash_Table #2022_05_29_Time_34_ms_(23.83%)_Space_41.9_MB_(71.28%)

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
// #Easy #String #Hash_Table #2024_03_29_Time_1_ms_(99.82%)_Space_41.5_MB_(72.24%)

public class Solution {
public int countVowelSubstrings(String word) {
int count = 0;
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
Set<Character> window = new HashSet<>();
for (int i = 0; i < word.length(); i++) {
window.clear();
if (vowels.contains(word.charAt(i))) {
window.add(word.charAt(i));
for (int j = i + 1; j < word.length(); j++) {
if (!vowels.contains(word.charAt(j))) {
break;
} else {
window.add(word.charAt(j));
if (window.size() == 5) {
count++;
}
}
final int length = word.length();
boolean[] vows = new boolean[128];
vows['a'] = true;
vows['e'] = true;
vows['i'] = true;
vows['o'] = true;
vows['u'] = true;
int[] counts = new int[128];
int uniqVows = 0;
int originalBegin = 0;
int begin = 0;
int result = 0;
for (int i = 0; i < length; i++) {
char ch = word.charAt(i);
if (vows[ch]) {
counts[ch]++;
if (counts[ch] == 1) {
uniqVows++;
}
while (uniqVows == 5) {
uniqVows -= --counts[word.charAt(begin)] == 0 ? 1 : 0;
begin++;
}
result += begin - originalBegin;
} else {
if (uniqVows != 0) {
uniqVows = 0;
counts['a'] = 0;
counts['e'] = 0;
counts['i'] = 0;
counts['o'] = 0;
counts['u'] = 0;
}
originalBegin = begin = i + 1;
}
}
return count;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
package g2101_2200.s2111_minimum_operations_to_make_the_array_k_increasing;

// #Hard #Array #Binary_Search #2022_05_31_Time_97_ms_(22.90%)_Space_123.6_MB_(54.96%)
// #Hard #Array #Binary_Search #2024_03_29_Time_12_ms_(100.00%)_Space_60.7_MB_(31.91%)

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class Solution {
public int kIncreasing(int[] a, int k) {
int n = a.length;
public int kIncreasing(int[] arr, int k) {
int n = arr.length;
int res = 0;
for (int s = 0; s < k; s++) {
List<Integer> dp = new ArrayList<>();
for (int i = s; i < n; i += k) {
if (!bsearch(dp, a[i])) {
dp.add(a[i]);
int[] dp = new int[n / k + 5];
for (int i = 0; i < k; i++) {
int lis = 0;
Arrays.fill(dp, 0);
for (int j = i; j < n; j += k) {
int low = 0;
int high = lis;
while (low < high) {
int mid = (low + high) >> 1;
if (arr[j] < dp[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
dp[low] = arr[j];
if (high == lis) {
lis++;
}
}
res += dp.size();
res += lis;
}
return n - res;
}

private boolean bsearch(List<Integer> dp, int target) {
if (dp.isEmpty()) {
return false;
}
int lo = 0;
int hi = dp.size() - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (dp.get(mid) <= target) {
lo = mid + 1;
} else {
hi = mid;
}
}

if (dp.get(lo) > target) {
dp.set(lo, target);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g2201_2300.s2272_substring_with_largest_variance;

// #Hard #Array #Dynamic_Programming #2022_06_16_Time_469_ms_(23.66%)_Space_43.7_MB_(32.44%)
// #Hard #Array #Dynamic_Programming #2024_03_29_Time_159_ms_(39.25%)_Space_41.9_MB_(97.66%)

public class Solution {
public int largestVariance(String s) {
Expand Down
Loading