본문 바로가기

정렬

(15)
[정렬] 백준 1715번 '카드 정렬하기' (Python) https://www.acmicpc.net/problem/1715 1715번: 카드 정렬하기 정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장 www.acmicpc.net 1. 항상 가장 작은 크기의 두 카드 묶음을 합치는 알고리즘 계산 2. heapq 사용 3. 결과 값은 두 개 더한 값을 다 더한 값 #이취코 답 import heapq n = int(input()) heap = [] for i in range(n): data = int(input()) heapq.heappush(heap, data) result = 0 while len(heap) ..
[순열과 조합] 프로그래머스 L1 '두 개 뽑아서 더하기' (Python) https://programmers.co.kr/learn/courses/30/lessons/68644 코딩테스트 연습 - 두 개 뽑아서 더하기 정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요. 제한 programmers.co.kr 1. combinations을 이용해서 조합 만들기 2. 중복되는 문자열 제거 3. 숫자를 더한 값 출력 from itertools import combinations, permutations def solution(numbers): s = set() num = set(combinations(numbers,2)) ..
[정렬] 프로그래머스 L1 '문자열 내 마음대로 정렬하기' (Python) https://programmers.co.kr/learn/courses/30/lessons/12915 코딩테스트 연습 - 문자열 내 마음대로 정렬하기 문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 ["sun", "bed", "car"]이고 n이 1이면 각 단어의 인덱 programmers.co.kr 1. 문자열의 n번째 숫자 기준으로 정렬 2. n번째 문자가 같은 경우 오름차순으로 정렬 def solution(strings, n): answer = [] hashs = {} for i in strings: hashs[i] = i[n] hashs = dict(sorted(hashs.items(..
[이분탐색] 릿코드 Medium 34 'Find First and Last Position of Element in Sorted Array' (Python) https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ Find First and Last Position of Element in Sorted Array - 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 1. 파이썬의 bisect 라이브러리를 활용 2. target이 nums에 있다면 정답을 반환하고 없으면 [-1,-1]을 반환함 from bisect impor..
[정렬] 릿코드 Easy 167 'Two Sum II - Input Array Is Sorted' (Python) https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Two Sum II - Input Array Is Sorted - 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 효율적인 알고리즘을 위해 bisect 라이브러리를 사용 양수가 포함될 때와 음수(0 포함)만 있을 때 나눠서 코딩 from bisect import bisect_right, bisect_left class Solution: def twoS..
[정렬] 릿코드 Easy 283 'Move Zeroes' (Python) https://leetcode.com/problems/move-zeroes/ Move Zeroes - 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 파이썬 배열의 내장 메소드를 사용 class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ for i in range(nums.count(0)): nu..
[정렬] 릿코드 Medium 189 'Rotate Array' (Python) https://leetcode.com/problems/rotate-array/ Rotate Array - 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 k번만큼 제일 오른쪽 값을 왼쪽으로 옮기는걸 반복하면 됨 class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ for i in range..
[정렬] 릿코드 Easy 977 'Squares of a Sorted Array' (Python) https://leetcode.com/problems/squares-of-a-sorted-array/ Squares of a Sorted Array - 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 리스트의 각 값에 연산을 해야 하므로 벡터연산이 가능한 numpy를 import 함 import numpy as np class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: answer = np...

LIST