Skip to content

Commit b72e16a

Browse files
committed
add Python 23,24,25,26 solutions
1 parent d9db13f commit b72e16a

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def maxSumAfterPartitioning(self, arr, k):
3+
"""
4+
:type arr: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
n = len(arr)
9+
res = [0] * (n+1)
10+
11+
for i in range(1, n+1):
12+
max_val = 0
13+
for j in range(1, k+1):
14+
pos = i - j
15+
if pos < 0:
16+
break
17+
if arr[pos] > max_val:
18+
max_val = arr[pos]
19+
res[i] = max(res[i], max_val * j + res[pos])
20+
return res[-1]
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution(object):
2+
def findBall(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: List[int]
6+
"""
7+
n = len(grid[0])
8+
ans = [-1] * n
9+
for i in range(n):
10+
col = i
11+
print(col)
12+
for row in grid:
13+
dir = row[col]
14+
col += dir
15+
if col < 0 or col == n or row[col] != dir:
16+
break
17+
else:
18+
ans[i] = col
19+
return ans
20+
21+
22+
23+
24+
25+
grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
26+
print(Solution().findBall(grid))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def minFlipsMonoIncr(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
8+
#使得该分界点之前1的个数和分界点之后0的个数之和最小,把分界点之前的1变成0,之后的0变成1
9+
m=s.count('0')
10+
res=[m]
11+
for x in s:
12+
if x=='1':
13+
m+=1
14+
else:
15+
m-=1
16+
res.append(m)
17+
return min(res)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def knightDialer(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
if n==1:
8+
return 10
9+
#A:{1,3,7,9}, B:{2,8}, C:{4,6}, D:{0}
10+
# 处于状态A中的数字(1,3,7,9)通过一次跳跃要么变成状态B(2,8),要么变成状态C(4,6)
11+
# 处于状态B中的数字(2,8)通过一次跳跃有两种方式变成状态A(1,3,7,9)
12+
# 处于状态C中的数字(4,6)通过一次跳跃有两种方式变成状态A(1,3,7,9),还有一种方式变成状态D(0)
13+
# 处于状态D中的数字(0)通过一次跳跃有两种方式变成状态C(4,6)
14+
15+
nums=[1,1,1,1]
16+
for _ in range(n-1):
17+
nums=[nums[1]+nums[2], 2*nums[0], 2*nums[0]+nums[3], 2*nums[2]]
18+
return (4*nums[0]+2*nums[1]+2*nums[2]+nums[3])%1000000007

0 commit comments

Comments
 (0)