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

Commite38bc7a

Browse files
authored
Create 1799-maximize-score-after-n-operations.kt
1 parent1939100 commite38bc7a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
classSolution {
2+
funmaxScore(nums:IntArray):Int {
3+
val cache=IntArray(1 shl nums.size) {-1 }
4+
5+
fundfs(mask:Int,op:Int):Int {
6+
if (cache[mask]!=-1)return cache[mask]
7+
8+
for (iin0 until nums.size) {
9+
for (jin i+1 until nums.size) {
10+
if ((1 shl i)and mask>0|| (1 shl j)and mask>0)
11+
continue
12+
val newMask= maskor (1 shl i)or (1 shl j)
13+
val score= op* gcd(nums[i], nums[j])
14+
cache[mask]= maxOf(
15+
if (cache[mask]!=-1) cache[mask]else0,
16+
score+ dfs(newMask, op+1)
17+
)
18+
}
19+
}
20+
21+
returnif (cache[mask]!=-1) cache[mask]else0
22+
}
23+
24+
return dfs(0,1)
25+
}
26+
27+
fungcd(_x:Int,_y:Int):Int {
28+
var x=_x
29+
var y=_y
30+
31+
while (x!= y) {
32+
if (x> y)
33+
x-= y
34+
else
35+
y-= x
36+
}
37+
38+
return x
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp