이경로 2022. 10. 27. 16:17

Difficulty : Gold V

Problem Description

There is an infinite sequence.

The rule of the sequence is like this.

The input is N, P, Q. The output should be A(N).

 

Solution

The range of N is too large to use array, so I used hash(in python, dictionary) to solve the problem.

 

I made a dictionary and setted the initial value (0, 1), (1, 2).

 

If the value is calculated before, return the value.

Otherwise, calculate A(i/P) + A(i/Q), store and return it.

 

 

Today's English

initial value : 초기값

 

Today's Code

x = dict() : 딕셔너리 선언

x.get(i, a) : 딕셔너리에서 i값 탐색, 없으면 a 반환

 

여담

배열을 선언하니 크기가 초과되어서 질문 리스트를 보다가 해시를 사용한다는 것을 알았다.

해시의 이론적인 정의는 알고 있었지만 사용해본 적은 없었는데

파이썬은 해시가 이미 구현되어 있어 간단히 사용이 가능했다.

기본적인 자료구조 중 하나인데 아직까지 써본 적이 없다는게 말이 되나 싶기도 하고...반성해야겠다.

댓글수0