In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of dividing an array into a continuous set of numbers by Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Problem description
Give you an array of integers nums and a positive integer k, please determine whether this array can be divided into a set of k consecutive numbers.
Return True; if you can. Otherwise, return False.
Example 1:
Input: nums = [1, 2, 3, 3, 4, 4, 5, 5, 6], k = 4
Output: true
Explanation: arrays can be divided into [1] and [3] and [3] and [4].
Example 2:
Input: nums = [3, 2, 1, 1, 2, 3, 4, 4, 4, 5, 9, 10, 11], k = 3
Output: true
Explanation: the array can be divided into [1], [2], [3], [3, 4] and [9, 10, 11].
Example 3:
Input: nums = [3, 3, 3, 2, 2, 1, 1], k = 3
Output: true
Example 4:
Input: nums = [1, 2, 3, 4], k = 3
Output: false
Explanation: arrays cannot be divided into subarrays of size 3.
2. Solution
Just got this problem, the author wants to first find the smallest number in the array, and then delete the corresponding elements from the array according to the value of k, for example, k is equal to 3, the smallest number in the array is 1, then delete the three elements of 1Magne2Power3 from the list. If there are no corresponding elements in the array, you should return False.
The answers are as follows:
Def isPossibleDivide (nums, k): nums = sorted (nums) for _ in range (len (nums) / / k): minv = nums [0] for _ in range (k): if minv in nums: nums.remove (a) minv + = 1 return len (nums) = 0
But there are too many operations in the second for loop, and if the value of k is too large, the code will have a lot of memory to run, and running within the specified memory will time out. So the author thought of the second method, although the amount of code is a little larger, but compared with the first kind, the time complexity is smaller, and it is not easy to time-out. Use the set to find out the numbers that have appeared in the array, and then use the dictionary to count the number of times each number appears. Set the decision condition, and then return the corresponding Boolean according to the continuous decision condition.
Python Code:
Def isPossibleDivide (nums K): n = len (nums) if n% k! = 0: return False # record possible numbers in a collection s = set (nums) minList = list (s) minList.sort () # store the number of times each number appears in a dictionary d = dict () for num in nums: if num not in d: d [num] = 0 D [num] + = 1 # judge whether each group can be composed of k consecutive digits m = n / / k # m group start = 0 # starting position for mi in range (m): if start > = len (minList): return False minv = minList [start] flag = True t = start for key in range (minv Minv + k): if key not in d: return False if d [key]
< 1: return False elif d[key] == 1: d[key] -= 1 t += 1 elif d[key] >1: d [key]-= 1 if flag: start = t flag = False if flag: start = t return True "what is the method of dividing an array into a contiguous set of numbers by Python", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.