Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Real or Not? NLP with Disaster Tweets
- github
- 프로그래머스
- ubuntu
- 편스토랑 우승상품
- Kaggle
- 백준
- 데이콘
- gs25
- 맥북
- SW Expert Academy
- Docker
- 금융문자분석경진대회
- dacon
- programmers
- 캐치카페
- Baekjoon
- Git
- 편스토랑
- 우분투
- AI 경진대회
- PYTHON
- 프로그래머스 파이썬
- 파이썬
- ChatGPT
- 더현대서울 맛집
- hackerrank
- leetcode
- 자연어처리
- 코로나19
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 75. Sort Colors (Python) 본문
728x90
반응형
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Follow up:
- A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. - Could you come up with a one-pass algorithm using only constant space?
Solution
from collections import Counter
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
nums_dict = dict(Counter(nums))
keys = nums_dict.keys()
index = 0
for i in range(3):
if i in keys:
for j in range(nums_dict[i]):
nums[index] = i
index = index + 1
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 29. Divide Two Integers (Python) (2) | 2020.06.05 |
---|---|
[leetCode] 46. Permutations (Python) (0) | 2020.06.04 |
[leetCode] 50. Pow(x, n) (Python) (0) | 2020.06.02 |
[BaeKJoon] 9461번: 파도반 수열 (Python) (0) | 2020.06.01 |
[BaeKJoon] 1699번: 제곱수의 합 (Python) (0) | 2020.06.01 |
Comments