avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1664. Number of schemes to generate balance numbers One question daily

  2024-01-01        
字数统计: 235字   |   阅读时长: 1min

1674887518406.png
1664. Number of schemes to generate balance numbers

Thought:

See when you read the question medium I know that it is definitely not really going to delete an element。Otherwise it will time out,So I tried to try polepythonFeature code:Use slice to process all data;
But it’s timeout。。

然后看官方answer,用的Dynamic planning。中心Thought是:

General nature,Now we will settle down i Delete elements,
Obviously the bidding i The previous element bidding will not change from this,Bidding i
The original was originally j,j>iThe array elements of the bid will move to the bidding j−1,
Immediately bidding i The subsequent bidding elements will become the rated element,
The even bidding element will become a strange number of bidding elements。

Code

slice
1
2
3
4
5
6
7
8
class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
flag = 0
for i in range(len(nums)):
temp_nums = nums[:i] + nums[i+1:]
if sum(temp_nums[::2])==sum(temp_nums[1::2]):
flag += 1
return flag
官方answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
res = odd1 = even1 = odd2 = even2 = 0
for i, num in enumerate(nums):
if i & 1:
odd2 += num
else:
even2 += num
for i, num in enumerate(nums):
if i & 1:
odd2 -= num
else:
even2 -= num
if odd1 + even2 == odd2 + even1:
res += 1
if i & 1:
odd1 += num
else:
even1 += num
return res
  • Python
  • answer
  • One question daily
  • Dynamic planning

扫一扫,分享到微信

微信分享二维码
1663. The smallest string with a given value One question daily
1669. Merge two linked watches One question daily
目录
  1. 1. Thought:
  2. 2. Code

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号