코딩 테스트/99클럽

[99클럽] 코테 스터디 5일차 TIL - HashMap

토자맨 2024. 5. 25. 23:20

문제

You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].

Implement the SmallestInfiniteSet class:

  • SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  • int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  • void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.제한사항
  • 1 <= num <= 1000
  • At most 1000 calls will be made in total to popSmallest and addBack.입출력 예
  • Input*
    ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
    [[], [2], [], [], [], [1], [], [], []]
  • Output*
    [null, null, 1, 2, 3, null, 1, 4, 5]

Explanation
SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.
smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
smallestInfiniteSet.addBack(1); // 1 is added back to the set.
smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
// is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.

HashMap

HashMap이란?

Map 인터페이스를 상속 받아 구현한 Map 컬렉션으로써 Key와 Value 쌍으로 값을 저장하고 관리할 수 있는 자료구조이다.

HashMap 메소드

  • get(key)
    • key에 해당하는 value 반환
  • put(key, value)
    • key 값에 value 삽입
  • remove(key)
    • key 값을 갖는 value를 삭제
  • containsKey(key)
    • key값에 해당하는 value가 있다면 True, 없다면 False 반환

오늘 회고

발생한 문제

딱히 문제는 없었고 처음엔 배열을 이용하려고 했는데 문제 의도에 맞게 HashMap을 이용해서 풀었다.

새롭게 깨달은 내용

배열이 익숙하다 보니 평소에 배열을 많이 이용하는데 앞으론 다양한 자료구조를 이용해 봐야겠다.

내일 공부할 것

  • 프레임워크 프로그래밍 백엔드 구현 및 JSP 구현
  • 99클럽 6일차 코테 문제 풀이 및 TIL 작성