
[LeetCode] 리트코드 1365번 How Many Numbers Are Smaller Than the Current Number
(Python)
(글쓴날 : 2020.03.04)
* LeetCode, 리트코드 1365번 문제 Python 언어 풀이입니다.
* 소스 코드의 저작권은 글쓴이에게 있습니다.
리트코드 1365번 How Many Numbers Are Smaller Than the Current Number
1) 문제
문제 링크 : https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
How Many Numbers Are Smaller Than the Current Number - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
2) 풀이 과정
여러 개의 숫자들이 배열(Python의 경우 리스트)로 입력되며, 입력받은 배열(리스트)에 속한 각각의 숫자들을 기준으로 작은 숫자들의 개수를 차례대로 배열(리스트)로 만들어 반환하는 문제입니다.
저의 경우, Python으로 해시 테이블을 만들어 문제를 풀었는데 먼저 각 입력받은 배열(리스트)에서 가장 큰 숫자 길이만큼의 해시 테이블을 생성하여 인덱스를 기준으로 인덱스에 해당하는 숫자가 몇 개가 있나 저장하였습니다.
그 후, 문제에서 요구하는 입력받은 배열(리스트)의 각 원소보다 작은 숫자의 개수를 구하기 위해 처음의 입력받은 배열(리스트)를 반복하여, 만들어 놓은 해시 테이블에서 해당 원소의 인덱스 앞까지 슬라이싱한 합을 구해 차례대로 배열(리스트)화하여 문제를 해결하였습니다.
3) 코드
* Python 코드
1
2
3
4
5
6
7
8
9
|
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
li = [0] * (max(nums) + 1)
for i in nums:
li[i] = (nums.count(i))
res = [sum(li[:x]) for x in nums]
return res
|
'Deprecated' 카테고리의 다른 글
[MySQL] 테이블(Table) 생성, 조회, 삭제하는 법 (0) | 2020.03.07 |
---|---|
[MySQL] 데이터베이스(스키마) 생성, 조회, 사용, 삭제하는 법 (0) | 2020.03.06 |
[LeetCode] 리트코드 1342번 Number of Steps to Reduce a Number to Zero(Python) (0) | 2020.03.03 |
[CSS] flexbox를 이용한 레이아웃(이미지, div 등) 가운데 정렬하는 법 (0) | 2020.03.02 |
[CSS] 레이아웃 height 100%로 동작하게 하는 법 (2) | 2020.03.01 |