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

6275. Make all the minimum operations of all elements in the array II

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

2023-01-22 (1).png
6275. Make all the minimum operations of all elements in the array II

Thought:

Weekly Two Questions!!!

·First use the list derivative to calculate the difference between each element。
·Then,Initialize a counter to track the required operations required。
·The difference between traversing,And check whether each difference can be removed。
·if not,Then return -1,Because the array cannot be equal to the given operation。
·If it is eliminated,It removes the absolute value of the difference to add to the counter。
·at last,It returns the required number of operations。

for example:[3,0,-6,3]It is every positive number divided by3In harmony,Tested many times,是因为at last一种特殊情况(When two list items, etc.)No consideration。

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int], k: int) -> int:
if k <= 0:
if nums1 != nums2:
return -1
else:
return 0
diff = [nums1[i] - nums2[i] for i in range(len(nums1))]
if sum(diff) != 0:
return -1
if sum(diff) % k != 0:
return -1
operations = 0
for index, d in enumerate(diff):
if d % k != 0:
return -1
if d > 0:
operations += d // k
return operations
  • Python
  • answer
  • greedy

扫一扫,分享到微信

微信分享二维码
6293. Count the number of good array
6323. Child that divides money the most
目录
  1. 1. Thought:
  2. 2. Code:

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