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

Commit3e36216

Browse files
committed
add new files
1 parentb37d8dd commit3e36216

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
Given an integer array A, you partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray.
3+
4+
Return the largest sum of the given array after partitioning.
5+
6+
7+
8+
Example 1:
9+
10+
Input: A = [1,15,7,9,2,5,10], K = 3
11+
Output: 84
12+
Explanation: A becomes [15,15,15,9,10,10,10]
13+
14+
*/
15+
16+
package main
17+
18+
funcmaxSumAfterPartitioning(A []int,Kint)int {
19+
n:=len(A)
20+
dp:=make([]int,n+1)
21+
fori:=0;i<n;i++ {
22+
curMax:=A[i]
23+
forj:=1;j<=K&&i-j+1>=0;j++ {
24+
curMax=max(curMax,A[i-j+1])
25+
dp[i+1]=max(dp[i+1],dp[i-j+1]+curMax*j)
26+
}
27+
}
28+
returndp[n]
29+
}
30+
31+
funcmax(a,bint)int {
32+
ifa>b {
33+
returna
34+
}
35+
returnb
36+
}
37+
38+
/**
39+
note:DP
40+
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 or more times)
3+
4+
Return the largest string X such that X divides str1 and X divides str2.
5+
6+
7+
8+
Example 1:
9+
10+
Input: str1 = "ABCABC", str2 = "ABC"
11+
Output: "ABC"
12+
Example 2:
13+
14+
Input: str1 = "ABABAB", str2 = "ABAB"
15+
Output: "AB"
16+
Example 3:
17+
18+
Input: str1 = "LEET", str2 = "CODE"
19+
Output: ""
20+
21+
*/
22+
package main
23+
24+
import"golang.org/x/tools/go/ssa/interp/testdata/src/strings"
25+
26+
funcgcdOfStrings(str1string,str2string)string {
27+
28+
iflen(str1)==0||str1==str2 {
29+
returnstr1
30+
}
31+
iflen(str2)==0 {
32+
returnstr2
33+
}
34+
iflen(str1)>len(str2) {
35+
x:=strings.Index(str1,str2)
36+
ifx>-1 {
37+
returngcdOfStrings(str2,str1[x+len(str2):])
38+
}
39+
}else {
40+
y:=strings.Index(str2,str1)
41+
ify>-1 {
42+
returngcdOfStrings(str1,str2[y+len(str1):])
43+
}
44+
}
45+
return""
46+
}
47+
48+
/**
49+
need to know the function:Strings.Index
50+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
Given the root of a binary tree, consider all root to leaf paths: paths from the root to any leaf. (A leaf is a node with no children.)
3+
4+
A node is insufficient if every such root to leaf path intersecting this node has sum strictly less than limit.
5+
6+
Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree.
7+
8+
*/
9+
package main
10+
11+
typeTreeNodestruct {
12+
Valint
13+
Left*TreeNode
14+
Right*TreeNode
15+
}
16+
17+
funcsufficientSubset(root*TreeNode,limitint)*TreeNode {
18+
sufficientSubsetRec(&root,0,limit)
19+
returnroot
20+
}
21+
22+
funcsufficientSubsetRec(ptrToCurr**TreeNode,accint,limitint) {
23+
cur:=*ptrToCurr
24+
valueSoFar:=acc+cur.Val
25+
26+
hasLeftKid,hasRightKid:=cur.Left!=nil,cur.Right!=nil
27+
ifcur.Left!=nil {
28+
sufficientSubsetRec(&cur.Left,valueSoFar,limit)
29+
}
30+
ifcur.Right!=nil {
31+
sufficientSubsetRec(&cur.Right,valueSoFar,limit)
32+
}
33+
ifvalueSoFar<limit&&cur.Left==nil&&cur.Right==nil {
34+
*ptrToCurr=nil
35+
}
36+
ifcur.Left==nil&&cur.Right==nil&& (hasLeftKid||hasRightKid) {
37+
*ptrToCurr=nil
38+
}
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp