Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated tags
  • Loading branch information
javadev committed Jun 17, 2025
commit 1811863299f2570e4c10ab91358bad8d1c6634c8
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
package g3501_3600.s3582_generate_tag_for_video_caption;

// #Easy #2025_06_16_Time_12_ms_(100.00%)_Space_45.47_MB_(100.00%)
// #Easy #String #Simulation #2025_06_17_Time_2_ms_(99.93%)_Space_43.54_MB_(89.89%)

@SuppressWarnings("java:S135")
public class Solution {
public String generateTag(String caption) {
if (caption.trim().isEmpty()) {
return "#";
}
String[] arr = caption.trim().split("\\s+");
StringBuilder res = new StringBuilder("#");
String firstWord = arr[0];
firstWord =
firstWord.substring(0, 1).toLowerCase()
+ (firstWord.length() > 1 ? firstWord.substring(1).toLowerCase() : "");
res.append(firstWord);
for (int i = 1; i < arr.length; i++) {
String w = arr[i];
if (w.isEmpty()) {
continue;
StringBuilder sb = new StringBuilder();
sb.append('#');
boolean space = false;
caption = caption.trim();
for (int i = 0; i < caption.length(); i++) {
char c = caption.charAt(i);
if (c == ' ') {
space = true;
}
if (c >= 'A' && c <= 'Z') {
if (space) {
space = !space;
sb.append(c);
} else {
sb.append(Character.toLowerCase(c));
}
}
w =
w.substring(0, 1).toUpperCase()
+ (w.length() > 1 ? w.substring(1).toLowerCase() : "");
res.append(w);
if (res.length() >= 100) {
break;
if (c >= 'a' && c <= 'z') {
if (space) {
space = !space;
sb.append(Character.toUpperCase(c));
} else {
sb.append(c);
}
}
}
return res.length() > 100 ? res.substring(0, 100) : res.toString();
if (sb.length() > 100) {
return sb.substring(0, 100);
}
return sb.toString();
}
}
33 changes: 16 additions & 17 deletions src/main/java/g3501_3600/s3583_count_special_triplets/Solution.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package g3501_3600.s3583_count_special_triplets;

// #Medium #2025_06_16_Time_250_ms_(100.00%)_Space_62.22_MB_(100.00%)

import java.util.HashMap;
import java.util.Map;
// #Medium #Array #Hash_Table #Counting #2025_06_17_Time_30_ms_(99.81%)_Space_60.90_MB_(89.67%)

public class Solution {
public int specialTriplets(int[] nums) {
int mod = 1_000_000_007;
int res = 0;
Map<Integer, Integer> left = new HashMap<>();
Map<Integer, Integer> right = new HashMap<>();
for (int num : nums) {
right.put(num, right.getOrDefault(num, 0) + 1);
long ans = 0;
int[] sum = new int[200002];
int[] left = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int curr = nums[i];
sum[curr]++;
left[i] = sum[curr * 2];
}
for (int num : nums) {
right.put(num, right.get(num) - 1);
int ci = left.getOrDefault(num * 2, 0);
int ck = right.getOrDefault(num * 2, 0);
res = (res + ci * ck) % mod;
left.put(num, left.getOrDefault(num, 0) + 1);
for (int i = 0; i < nums.length; i++) {
int curr = nums[i];
long leftCount = curr == 0 ? left[i] - 1 : left[i];
long rightCount = sum[curr * 2] - left[i];
if (leftCount != 0 && rightCount != 0) {
ans += leftCount * rightCount;
}
}
return res;
return (int) (ans % 1000000007);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3501_3600.s3584_maximum_product_of_first_and_last_elements_of_a_subsequence;

// #Medium #2025_06_16_Time_4_ms_(87.17%)_Space_61.31_MB_(100.00%)
// #Medium #Array #Two_Pointers #2025_06_17_Time_4_ms_(86.42%)_Space_60.92_MB_(51.72%)

public class Solution {
public long maximumProduct(int[] nums, int m) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,123 +1,142 @@
package g3501_3600.s3585_find_weighted_median_node_in_tree;

// #Hard #2025_06_16_Time_162_ms_(100.00%)_Space_141.58_MB_(100.00%)
// #Hard #Array #Dynamic_Programming #Tree #Binary_Search #Depth_First_Search
// #2025_06_17_Time_66_ms_(94.96%)_Space_142.62_MB_(49.64%)

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

@SuppressWarnings("java:S2234")
public class Solution {
private int log;
private long[] dist;
private List<List<int[]>> adj;
private int[] depth;
private int[][] up;
private long[] dist;
private int[][] parent;
private int longMax;
private int nodes;

public int[] findMedian(int n, int[][] edges, int[][] queries) {
List<List<int[]>> adj = new ArrayList<>();
nodes = n;
if (n > 1) {
longMax = (int) Math.ceil(Math.log(n) / Math.log(2));
} else {
longMax = 1;
}
adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int[] edge : edges) {
adj.get(edge[0]).add(new int[] {edge[1], edge[2]});
adj.get(edge[1]).add(new int[] {edge[0], edge[2]});
int u = edge[0];
int v = edge[1];
int w = edge[2];
adj.get(u).add(new int[] {v, w});
adj.get(v).add(new int[] {u, w});
}
dist = new long[n];
depth = new int[n];
log = 0;
while (1 << log < n) {
log++;
}
up = new int[n][log];
for (int[] u : up) {
Arrays.fill(u, -1);
dist = new long[n];
parent = new int[longMax][n];
for (int i = 0; i < longMax; i++) {
Arrays.fill(parent[i], -1);
}
dfs(0, -1, adj, 0, 0);
dfs(0, -1, 0, 0L);
buildLcaTable();
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int[] query = queries[i];
int first = query[0];
int second = query[1];
long distance = getDistance(first, second);
long needed = (distance + 1) / 2;
int mid = lca(first, second);
if (getDistance(first, mid) < needed) {
needed -= getDistance(first, mid);
first = mid;
} else {
second = mid;
}
if (depth[first] <= depth[second]) {
long curDistance = getDistance(first, second);
for (int j = log - 1; j >= 0; j--) {
if (up[second][j] == -1
|| curDistance - getDistance(up[second][j], second) < needed) {
continue;
}
curDistance -= getDistance(up[second][j], second);
second = up[second][j];
}
ans[i] = second;
} else {
long curDistance = 0;
for (int j = log - 1; j >= 0; j--) {
if (up[first][j] == -1
|| curDistance + getDistance(up[first][j], first) >= needed) {
continue;
}
curDistance += getDistance(up[first][j], first);
first = up[first][j];
}
ans[i] = up[first][0];
}
int[] sabrelonta;
for (int qIdx = 0; qIdx < queries.length; qIdx++) {
sabrelonta = queries[qIdx];
int u = sabrelonta[0];
int v = sabrelonta[1];
ans[qIdx] = findMedianNode(u, v);
}

return ans;
}

private long getDistance(int from, int to) {
if (from == to) {
return 0;
private void dfs(int u, int p, int d, long currentDist) {
depth[u] = d;
parent[0][u] = p;
dist[u] = currentDist;
for (int[] edge : adj.get(u)) {
int v = edge[0];
int w = edge[1];
if (v == p) {
continue;
}
dfs(v, u, d + 1, currentDist + w);
}
int ancesor = lca(from, to);
return dist[from] + dist[to] - 2 * dist[ancesor];
}

private int lca(int first, int second) {
if (depth[first] < depth[second]) {
return lca(second, first);
private void buildLcaTable() {
for (int k = 1; k < longMax; k++) {
for (int node = 0; node < nodes; node++) {
if (parent[k - 1][node] != -1) {
parent[k][node] = parent[k - 1][parent[k - 1][node]];
}
}
}
for (int i = log - 1; i >= 0; i--) {
if (depth[first] - (1 << i) >= depth[second]) {
first = up[first][i];
}

private int getKthAncestor(int u, int k) {
for (int p = longMax - 1; p >= 0; p--) {
if (u == -1) {
break;
}
if (((k >> p) & 1) == 1) {
u = parent[p][u];
}
}
if (first == second) {
return second;
return u;
}

private int getLCA(int u, int v) {
if (depth[u] < depth[v]) {
int temp = u;
u = v;
v = temp;
}
for (int i = log - 1; i >= 0; i--) {
if (depth[first] != -1 && up[first][i] != up[second][i]) {
first = up[first][i];
second = up[second][i];
u = getKthAncestor(u, depth[u] - depth[v]);
if (u == v) {
return u;
}
for (int p = longMax - 1; p >= 0; p--) {
if (parent[p][u] != -1 && parent[p][u] != parent[p][v]) {
u = parent[p][u];
v = parent[p][v];
}
}
first = up[first][0];
return first;
return parent[0][u];
}

private void dfs(int current, int parent, List<List<int[]>> adj, long curDist, int curDepth) {
up[current][0] = parent;
for (int i = 1; i < log; i++) {
if (up[current][i - 1] != -1) {
up[current][i] = up[up[current][i - 1]][i - 1];
}
private int findMedianNode(int u, int v) {
if (u == v) {
return u;
}
dist[current] = curDist;
depth[current] = curDepth;
for (int[] next : adj.get(current)) {
if (next[0] == parent) {
continue;
int lca = getLCA(u, v);
long totalPathWeight = dist[u] + dist[v] - 2 * dist[lca];
long halfWeight = (totalPathWeight + 1) / 2L;
if (dist[u] - dist[lca] >= halfWeight) {
int curr = u;
for (int p = longMax - 1; p >= 0; p--) {
int nextNode = parent[p][curr];
if (nextNode != -1 && (dist[u] - dist[nextNode] < halfWeight)) {
curr = nextNode;
}
}
return parent[0][curr];
} else {
long remainingWeightFromLCA = halfWeight - (dist[u] - dist[lca]);
int curr = v;
for (int p = longMax - 1; p >= 0; p--) {
int nextNode = parent[p][curr];
if (nextNode != -1
&& depth[nextNode] >= depth[lca]
&& (dist[nextNode] - dist[lca]) >= remainingWeightFromLCA) {
curr = nextNode;
}
}
dfs(next[0], current, adj, curDist + next[1], curDepth + 1);
return curr;
}
}
}