파이썬 브러시: 간단한 그룹 (5)

21.Max Consecutive Ones
    Given a binary array, find the maximum number of consecutive 1s in this array.
2진수 그룹을 정해서 이 그룹에서 연속적으로 나타나는 숫자 1의 최대 개수를 찾아라.
예:nums=[1,1,0,1,1], 출력 3
프로그램:
def fm(nums):
	current = 0
	max_n = 0
	for num in nums:
		if num == 1:
			current += 1
			max_n = max(max_n, current)
		else:
			current = 0
	return current

22.K-diff Pairs in an Array
    Given an array of integers and an integer K, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
정수 그룹과 정수 K를 정하려면 이 그룹에서 유일무이한 k의 차이를 찾아야 한다.K 차이 쌍은 정수 쌍(i, j)으로 구성되고 i와 j는 모두 수조의 숫자이며 절대값의 차이는 K와 같다.
예: [3,1,4,1,5]와 k=2를 정하고 출력 2(1,3)와(3,5)를 출력한다.
프로그램:
import collections
def ij(nums,k):
	if k>0:
		return len(set(nums) + set(num+k for num in nums))
	elif k=0:
		return sum(v>1 for v in collections.Counter(nums).values())/2
	else:
		return 0

23.Array Partition Ⅰ
    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say(a1, b1), (a2, b2),…, (an,bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
2n개의 정수로 구성된 그룹을 지정합니다. 당신의 임무는 이 정수들을 n쌍으로 나누는 것입니다. 예를 들어 (a1, b1), (a2, b2),..., (an,bn), 마지막으로min(ai,bi)의 총계를 가장 크게 하는 것입니다.
예:
[1,4,3,2]를 주고 출력 4
프로그램:
def partition(nums):
	return sum(sorted(nums)[::2])

24.Reshape the Matrix
    In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data. You’re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively. The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were. If the ‘reshape’ operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
MATLAB에서 자주 사용하는 함수는reshape입니다. 이 함수는 행렬을 크기가 다른 새로운 행렬로 재구성하고 원시 수치를 바꾸지 않습니다.너는 하나의 행렬을 2차원 행렬로 표시해야 한다. 두 개의 정수 r와 c는 각각 재구성 행렬의 행수와 열수를 대표한다.재구성 행렬은 원 행렬의 모든 요소를 같은 줄 순서로 옮겨야 한다.만약 재구성 매트릭스에 주어진 매개 변수가 사용 가능하고 합법적이라면 새로운 재구성 매트릭스를 출력합니다.그렇지 않으면 원본 행렬을 출력합니다.
예:
nums=[1,2],[3,4],r=1,c=4 입력
[[[1,2,3,4]] 출력
프로그램:
def reshape(nums, r, c):
	if(len(nums)*len(nums[0]) == r*c):
		mid = [nums[i][j] for i in range(len(nums)) for j in range(len(nums[0]))]
		return [mid[t*c:(t+1)*c] for t in range(r)]
	else:
		return nums

25.Shortest Unsorted Continuous Subarray
    Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shorted such subarray and output its length.
정수 그룹을 정하려면 연속적인 하위 그룹을 찾아야 한다. 만약 이 하위 그룹에 대한 정렬만 한다면 전체 하위 그룹도 정렬 상태를 보일 것이다.가장 짧은 하위 그룹을 찾아서 길이를 출력해야 한다.
예:
[2,6,4,8,10,9,15]를 입력하여 출력 5
프로그램:
def reshape(nums, r, c):
	if(len(nums)*len(nums[0]) == r*c):
		mid = [nums[i][j] for i in range(len(nums)) for j in range(len(nums[0]))]
		return [mid[t*c:(t+1)*c] for t in range(r)]
	else:
		return nums

좋은 웹페이지 즐겨찾기