본문 바로가기

알고리즘

(113)
[정렬] 릿코드 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...
[이분탐색] 릿코드 Easy 278 'First Bad Version' (Python) https://leetcode.com/problems/first-bad-version/ First Bad Version - 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 원소의 값이 크기 때문에 이분 탐색으로 정답을 찾아야 함 # The isBadVersion API is already defined for you. # @param version, an integer # @return an integer # def isBadVersion(version): cla..
[이분탐색] 릿코드 Easy 704 'Binary Search' (Python) https://leetcode.com/problems/binary-search/ Binary Search - 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 search(self, nums: List[int], target: int) -> int: def binary_search(array,target,start,end): if start>end: return None mid = (start+e..
[알고리즘] BFS(넓이 우선 탐색) / DFS(깊이 우선 탐색) 인접 행렬 : 2차원 배열로 그래프의 연결 관계를 표현하는 방식 간선 정보를 저장하기 위해 O(V^2)만큼의 메모리 공간 필요 특정한 노드 A에서 다른 특정한 노드 B로 이어진 간선의 비용 : O(1) V : 노드 0 1 2 #노드 번호 0 0 7 5 #거리 1 7 0 ~ 2 5 ~ 0 인접 리스트 : 리스트로 그래프의 연결 관계를 표현하는 방식 간선 정보를 저장하기 위해 O(E)만큼의 메모리 공간 필요 특정한 노드 A에서 다른 특정한 노드 B로 이어진 간선의 비용 : O(V) E : 간선 graph = [[] for _ in range(노드개수)] graph[0].append((노드번호, 노드거리)) graph[0].append((노드번호, 노드거리)) graph[1].append((노드번호, 노드거리..
[알고리즘] 소수의 판별 / 투 포인터 / 구간 합 계산 / 순열과 조합 소수의 판별 [특정 숫자 소수 판별] 제곱근 사용. 시간 복잡도 : O(X^1/2) import math def is_prime_number(x): for i in range(2,int(math.sqrt(x))+1): if x%i==0: return False #소수 아님 return True #소수 is_prime_number(숫자) [특정 범위 소수 판별 (에라토스테네스의 체)] 시간 복잡도 : O(NloglogN). 메모리를 많이 필요하기 때문에 N이 1,000,000 이내여야 함 2부터 N까지의 모든 자연수를 나열한다. 남은 수 중에서 아직 처리하지 않은 가장 작은 수 i를 찾는다 남은 수 중에서 i의 배수를 모두 제거한다(i는 제거하지 않는다) 더 이상 반복할 수 없을 때까지 2번과 3번의 과정..
[순열과 조합] 백준 1759번 '암호 만들기' (Python) https://www.acmicpc.net/problem/1759 1759번: 암호 만들기 첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다. www.acmicpc.net 암호가 순서대로 정렬되어 있어야 하므로 combinations 사용 최소 모음 1개 이상, 자음 2개 이상 있어야 함 '''입력 예시 4 6 a t c i s w ''' from itertools import combinations l,c = map(int, input().split()) alpha = input().split() alpha.sort() result = list(combinations(..

LIST