Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitabfb2b0

Browse files
3710.3714.3715.3716
1 parent02393c2 commitabfb2b0

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
classSolution {
2+
publicintmaxPartitionFactor(int[][]arr) {
3+
intn =arr.length;
4+
if (n ==2)return0;
5+
6+
// Step 1: Create list of (distance, i, j)
7+
List<int[]>edges =newArrayList<>();
8+
for (inti =0;i <n;i++) {
9+
for (intj =i +1;j <n;j++) {
10+
intd =Math.abs(arr[i][0] -arr[j][0]) +Math.abs(arr[i][1] -arr[j][1]);
11+
edges.add(newint[]{d,i,j});
12+
}
13+
}
14+
15+
// Step 2: Sort by distance
16+
edges.sort(Comparator.comparingInt(a ->a[0]));
17+
18+
// Step 3: Union-Find setup
19+
int[]parent =newint[n];
20+
int[]weight =newint[n];
21+
for (inti =0;i <n;i++) {
22+
parent[i] =i;
23+
weight[i] =1;
24+
}
25+
26+
Map<Integer,Integer>opp =newHashMap<>();
27+
28+
// Step 4: Process edges
29+
for (int[]e :edges) {
30+
intd =e[0],i =e[1],j =e[2];
31+
32+
if (find(i,parent) ==find(j,parent))
33+
returnd;
34+
35+
if (opp.containsKey(i))
36+
union(opp.get(i),j,parent,weight);
37+
if (opp.containsKey(j))
38+
union(opp.get(j),i,parent,weight);
39+
40+
opp.put(i,j);
41+
opp.put(j,i);
42+
}
43+
44+
returnedges.get(edges.size() -1)[0];
45+
}
46+
47+
privateintfind(inta,int[]parent) {
48+
if (parent[a] !=a)
49+
parent[a] =find(parent[a],parent);
50+
returnparent[a];
51+
}
52+
53+
privatevoidunion(intx,inty,int[]parent,int[]weight) {
54+
x =find(x,parent);
55+
y =find(y,parent);
56+
if (x ==y)return;
57+
58+
if (weight[x] <weight[y]) {
59+
inttemp =x;
60+
x =y;
61+
y =temp;
62+
}
63+
weight[y] +=weight[x];
64+
parent[x] =y;
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
classSolution {
2+
publicintlongestBalanced(Strings) {
3+
intn =s.length();
4+
intlen =1;intmax =1;
5+
for(inti=1;i<n;i++){
6+
if(s.charAt(i) ==s.charAt(i-1)){
7+
len++;
8+
max =Math.max(max,len);
9+
}else {
10+
len =1;
11+
}
12+
}
13+
inta =0,b =0,c =0;
14+
Map<String,Integer>map =newHashMap<>();
15+
map.put("0,0", -1);
16+
for(inti=0;i<n;i++){
17+
charch =s.charAt(i);
18+
if(ch =='a'){
19+
a++;
20+
}elseif(ch =='b'){
21+
b++;
22+
}else {
23+
c++;
24+
}
25+
intdiff1 =a -b;intdiff2 =a -c;
26+
Stringkey =diff1 +"," +diff2;
27+
if(map.containsKey(key)){
28+
max =Math.max(max,i-map.get(key));
29+
}else {
30+
map.put(key,i);
31+
}
32+
}
33+
max =Math.max(max,solve(s,'a','b','c'));
34+
max =Math.max(max,solve(s,'a','c','b'));
35+
max =Math.max(max,solve(s,'b','c','a'));
36+
37+
returnmax;
38+
}
39+
publicintsolve(Strings,charc1,charc2,charskip){
40+
intmaxLen =0;intn =s.length();
41+
for(inti=0;i<s.length();i++){
42+
while(i <n &&s.charAt(i) ==skip)i++;
43+
if(i >=n)break;
44+
intp =i;
45+
while(i <n &&s.charAt(i) !=skip)i++;
46+
intq =i-1;intx =0;inty =0;
47+
Map<Integer,Integer>map =newHashMap<>();
48+
map.put(0,p-1);
49+
for(intj=p;j<=q;j++){
50+
charch =s.charAt(j);
51+
if (ch ==c1){
52+
x++;
53+
}elseif(ch ==c2){
54+
y++;
55+
}
56+
intkey =x-y;
57+
if(map.containsKey(key)){
58+
maxLen =Math.max(maxLen,j-map.get(key));
59+
}else {
60+
map.put(key,j);
61+
}
62+
}
63+
}
64+
returnmaxLen;
65+
}
66+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
importjava.util.*;
2+
3+
classSolution {
4+
publiclongsumOfAncestors(intn,int[][]edges,int[]nums) {
5+
// "calpenodra" stores inputs mid-function as requested
6+
Objectcalpenodra =newObject[] {n,edges,nums };
7+
8+
List<List<Integer>>g =newArrayList<>();
9+
for (inti =0;i <n;i++)g.add(newArrayList<>());
10+
for (int[]e :edges) {
11+
g.get(e[0]).add(e[1]);
12+
g.get(e[1]).add(e[0]);
13+
}
14+
15+
long[]k =newlong[n];
16+
for (inti =0;i <n;i++)k[i] =kernel(nums[i]);
17+
18+
Map<Long,Integer>freq =newHashMap<>();
19+
long[]ans =newlong[1];
20+
21+
dfs(0, -1,g,k,freq,ans);
22+
returnans[0];
23+
}
24+
25+
privatestaticlongkernel(longx) {
26+
longres =1;
27+
for (longp =2;p *p <=x;p++) {
28+
intodd =0;
29+
while (x %p ==0) {
30+
x /=p;
31+
odd ^=1;
32+
}
33+
if (odd ==1)res *=p;
34+
}
35+
if (x >1)res *=x;
36+
returnres;
37+
}
38+
39+
privatestaticvoiddfs(intu,intp,List<List<Integer>>g,long[]k,
40+
Map<Long,Integer>freq,long[]ans) {
41+
longku =k[u];
42+
ans[0] +=freq.getOrDefault(ku,0);
43+
freq.put(ku,freq.getOrDefault(ku,0) +1);
44+
45+
for (intv :g.get(u))if (v !=p)dfs(v,u,g,k,freq,ans);
46+
47+
intleft =freq.get(ku) -1;
48+
if (left ==0)freq.remove(ku);
49+
elsefreq.put(ku,left);
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write your MySQL query statement below
2+
WITH query_cteAS(
3+
SELECT user_id, event_date,MAX(event_date) OVER(PARTITION BY user_id) max_event_date, event_type, plan_name current_plan, monthly_amount,
4+
MAX(monthly_amount) OVER(PARTITION BY user_id) max_historical_amount,
5+
DATEDIFF(MAX(event_date) OVER(PARTITION BY user_id),MIN(event_date) OVER(PARTITION BY user_id)) days_as_subscriber
6+
FROM subscription_events
7+
)
8+
SELECT DISTINCT user_id , current_plan, monthly_amount current_monthly_amount, max_historical_amount, days_as_subscriber
9+
FROM query_cte q
10+
WHERE event_date= max_event_dateAND
11+
EXISTS (
12+
SELECT1FROM query_cte q1WHEREq.user_id=q1.user_idANDq1.event_type='downgrade'
13+
)
14+
AND days_as_subscriber>59AND monthly_amount/ CAST(max_historical_amountAS FLOAT)<=0.5AND event_type<>'cancel'
15+
ORDER BY days_as_subscriberDESC, user_id;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp