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

Commit5d4ebaa

Browse files
committed
add new files
1 parent1532204 commit5d4ebaa

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.
3+
4+
5+
6+
Example 1:
7+
8+
Input: nums = [3,6,5,1,8]
9+
Output: 18
10+
Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).
11+
Example 2:
12+
13+
Input: nums = [4]
14+
Output: 0
15+
Explanation: Since 4 is not divisible by 3, do not pick any number.
16+
17+
*/
18+
package main
19+
20+
funcmaxSumDivThree(nums []int)int {
21+
dp:=make([]int,3)
22+
for_,v:=rangenums {
23+
tmp:=make([]int,3)
24+
copy(tmp,dp)
25+
fori:=0;i<3;i++ {
26+
dp[(v+tmp[i])%3]=max(dp[(v+tmp[i])%3],v+tmp[i])
27+
}
28+
}
29+
returndp[0]
30+
}
31+
32+
funcmax(x,yint)int {
33+
ifx>y {
34+
returnx
35+
}
36+
returny
37+
}
38+
39+
/**
40+
dp:除以3的余数有3种情况,0,1,2
41+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
3+
4+
After doing so, return the array.
5+
6+
7+
8+
Example 1:
9+
10+
Input: arr = [17,18,5,4,6,1]
11+
Output: [18,6,6,6,1,-1]
12+
13+
*/
14+
package main
15+
16+
funcreplaceElements(arr []int) []int {
17+
varres []int=make([]int,len(arr))
18+
varmaxint=arr[len(arr)-1]
19+
res[len(arr)-1]=-1
20+
fori:=len(arr)-2;i>=0;i-- {
21+
res[i]=max
22+
ifarr[i]>max {
23+
max=arr[i]
24+
}
25+
}
26+
returnres
27+
}
28+
29+
/**
30+
trick:from right to left
31+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp