[2261] K Divisible Elements Subarrays | Medium | contest 291

yoongyum·2022년 5월 1일
0

코딩테스트 🧩

목록 보기
29/47
post-thumbnail

🔎 문제설명

Given an integer array nums and two integers k and p, return the number of distinct subarrays which have at most k elements divisible by p.

Two arrays nums1 and nums2 are said to be distinct if:

They are of different lengths, or
There exists at least one index i where nums1[i] != nums2[i].
A subarray is defined as a non-empty contiguous sequence of elements in an array.

p로 나눌수 있는 요소의 갯수가 k개 이하인 집합의 갯수를 구하는 문제입니다.

Example 1

Input: nums = [2,3,3,2,2], k = 2, p = 2
Output: 11

Exampl2 2

Input: nums = [1,2,3,4], k = 4, p = 1
Output: 10

제한사항

  • 1 <= nums.length <= 200
  • 1 <= nums[i], p <= 200
  • 1 <= k <= nums.length


저는 이문제를 잘못이해하고 풀어서 결국 풀지 못했습니다.
끝나고 나서 왜 안돼나 하면서 다시 읽어보면서 알게되었습니다. (언어의 장벽 ㅜㅜ)

이문제 👎싫어요수도 높긴합니다..
(나만 이상하다고 느낀 게 아닐지도..)

🧊 파이썬 코드

class Solution:
    def countDistinct(self, nums: List[int], k: int, p: int) -> int:
        n = len(nums)                        
        arr = set()
        
        for start in range(n):
            cnt = 0
            tmp = ''
            for i in range(start, n):
                if nums[i]%p == 0:
                    cnt+=1                 
                tmp+=str(nums[i]) + ','        
                if cnt>k: 
                    break
                arr.add(tmp)                                    
                
        return len(arr)

문제만 처음부터 잘 이해했더라면 좋았을텐데 아쉽네요..

0개의 댓글