saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 딕셔너리
  • 리트코드
  • DFS
  • BFS
  • 딥러닝
  • 온라인저지
  • 개발자
  • Python
  • 개발자 취업준비
  • 알고리즘
  • 알고리즘문제해결
  • 문제해결능력
  • c++
  • 파이썬
  • 릿코드
  • 백준
  • 취업준비
  • LeetCode
  • 취준
  • two pointer

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode]1338. Reduce Array Size to The Half

2022. 8. 20. 11:03

1338. Reduce Array Size to The Half

Medium

You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.

Return the minimum size of the set so that at least half of the integers of the array are removed.

 

Example 1:

Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array.

Example 2:

Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.

 

Constraints:

  • 2 <= arr.length <= 105
  • arr.length is even.
  • 1 <= arr[i] <= 105

문제 풀이

  1. 문제 접근
  2. 리스트의 최대 길이는 10^5 이다.
  3. 시간복잡도 O(N^2) 이하의 속도로 풀어야한다.

풀이

  1. 숫자 갯수를 세는 것은 for 문을 한번 진행해서 N 시간만에 구할 수 있다. 
  2. 편하게 Counter 를 사용해서 각 숫자의 갯수를 저장한다.
  3. 가장 갯수가 많은 것부터 더 해가면서 그 값이 리스트 전체 길이의 반 이상이 되면 정답이 된다. 
  4. 최소의 숫자 세트를 빼서 총 리스트 길이를 반 이상 줄여야 하기 때문에 정렬을 하여 큰 숫자부터 빼며 답을 찾는다.

소스 코드

class Solution:
    def minSetSize(self, arr: List[int]) -> int:
        cnt = Counter(arr)
        total_len = 0
        ans = 0
        n = len(arr)
        cnt = sorted(cnt.values(), reverse=True)
        for num in cnt:
            print(num)
            total_len += num
            ans += 1
            if total_len >= n - total_len:
                return ans
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 1272. Remove Interval  (1) 2022.09.23
[LeetCode] 557. Reverse Words in a String III  (1) 2022.09.23
[LeetCode] 1570. Dot Product of Two Sparse Vectors  (0) 2022.08.18
[LeetCode] 28. Implement strStr()  (0) 2022.08.17
[LeetCode] 49. Group Anagrams  (0) 2022.08.17
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 1272. Remove Interval
    • [LeetCode] 557. Reverse Words in a String III
    • [LeetCode] 1570. Dot Product of Two Sparse Vectors
    • [LeetCode] 28. Implement strStr()
    saurus2
    saurus2
    Simple is Best

    티스토리툴바