LeetCode) 491. Non-decreasing Subsequences

Zion·2023년 1월 24일
1

문제 요약) 감소하지 않은 부분집합을 출력하되, 최소 원소 갯수 2개 이상인 집합을 출력하라. 순서 상관 없음.

사용한 개념 - bitmask

Sol )

print

Code )

func findSubsequences(_ nums: [Int]) -> [[Int]] {
        return Set(
            (1 ..< (1 << nums.count))
            .map { bits in
                nums.indices
                .filter { index in bits & 1 << index > 0 }
                .reduce(into: [Int]()) { partial, idx in
                    if partial.isEmpty || nums[idx] >= partial.last! {
                        partial.append(nums[idx])
                    }
                }
            }
        ).filter { $0.count > 1 }
    }
}

참고 )

bitmask?


위키백과 - bitmask

bitmask subarray

https://mygumi.tistory.com/361

profile
어제보다만 나아지는

0개의 댓글