Algorithm
-
[🥰프로그래머스] 압축 (2018 KAKAO BLIND RECRUITMENT)Algorithm/1일 1코테 2022. 10. 8. 17:38
나의 풀이 def solution(msg): # 알파벳 사전을 만들기 위한 스트링 alphabet_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # 알파벳 사전 만들기... enumerate는 0부터 시작해서 1씩 더해줌 _dict = {} for i, a in enumerate(alphabet_string): _dict[a] = i+1 answer = [] l = 0 r = l+1 # msg[l:r]이 이미 사전에 있으면 하나 더 해줌 (K->KA) # 없으면 (KA) 사전에 넣어주고 이전 글자 (K)를 answer에 더해줌 # 다만 이미 사전에 있어도 마지막인 경우 (KAKAO 중 O) answer에 더해주지 않아서 따로 처리해줌 while r
-
[🥰프로그래머스] 신고 결과 받기 [2022 KAKAO BLIND RECRUITMENT]Algorithm/1일 1코테 2022. 10. 6. 16:56
나의 풀이 from collections import defaultdict def solution(id_list, report, k): count_dict = defaultdict(int) people_dict = defaultdict(list) for p in report: g, r = p.split(' ') if r not in people_dict[g]: count_dict[r] += 1 people_dict[g].append(r) count_list = [p for p in count_dict.keys() if count_dict[p] >= k] answer = [] for i in id_list: count = 0 for t in people_dict[i]: if t in count_list: ..
-
[😭 leetcode] 15. 3SumAlgorithm/1일 1코테 2022. 7. 25. 20:48
리트코드 13번째 문제 [3Sum] https://leetcode.com/problems/3sum/ 3Sum - 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 문제 배열을 입력받아 합으로 0을 만들 수 있는 3개의 엘리먼트를 출력하라 (중복X) 정답 class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: result = [] # 편의상 정렬 수행 nums.sort() # i를 기준으로 투 ..
-
[😸코테] 시간복잡도 VS 공간복잡도Algorithm/알고리즘 2022. 7. 25. 18:48
시간 복잡도 특정한 크기의 입력에 대하여 알고리즘이 얼마나 오래 걸리나? 빅오 표기법 : 가장 빠르게 증가하는 항만을 고려하는 표기법 예제) 5개의 데이터를 받아 차례로 5회 더해준다 -> 연산횟수가 N에 비례함 -> O(N)이라 표기 array = [3, 5, 1, 2, 4] # 5개의 데이터(N=5) summary = 0 # 합계를 저장할 변수 # 모든 데이터를 하나씩 확인하며 합계를 계산 for x in array: summary += x # 결과를 출력 print(summary) 예제) 2중 반복문 -> N*N만큼의 연산 사용 -> O(N^2) array = [3, 5, 1, 2,4] for i in array: for j in array: temp = i*j print(temp) 코딩테스트에서는..
-
🌼알고리즘🌼 그리디 알고리즘Algorithm/알고리즘 2022. 6. 5. 19:13
그리디 알고리즘 : 현재 상황에서 지금 당장 좋은 것만 고르는 방법 그리디 알고리즘 문제 유형 : 가장 큰 순서대로, 가장 작은 순서대로와 같은 기준이 나타난다. 이런 기준은 정렬 알고리즘과 함께 짝을 이뤄 출제된다 그리디 알고리즘 예제 당신은 음식점의 계산을 도와주는 점원이다. 카운터에는 거스름돈으로 사용할 500원, 100원, 50원, 10원짜리 동전히 무한히 존재한다. 손님에게 거슬러 줘야 할 돈이 N원일 때 거슬러 줘야 할 동전의 최소 개수를 구하라. 풀이 idea : 가장 큰 화폐 단위부터 돈을 거슬러 줘야함 가장 먼저 500원으로 거슬러 줄 수 있을 만큼 거슬러 준다. 그 다음 100원, 50원, 10원... 으로 거슬러 준다. n = 1260 count = 0 # 동전의 유형 coin_typ..
-
[🥰프로그래머스] 폰켓몬 (찾아라 프로그래밍 마에스터)Algorithm/1일 1코테 2022. 2. 7. 12:36
https://programmers.co.kr/learn/courses/30/lessons/1845 코딩테스트 연습 - 폰켓몬 당신은 폰켓몬을 잡기 위한 오랜 여행 끝에, 홍 박사님의 연구실에 도착했습니다. 홍 박사님은 당신에게 자신의 연구실에 있는 총 N 마리의 폰켓몬 중에서 N/2마리를 가져가도 좋다고 했습니다. programmers.co.kr 내 풀이 def solution(nums): n_set = set(nums) if len(n_set) < len(nums)/2: return len(n_set) return len(nums)/2 다른 사람 풀이 def solution(ls): return min(len(ls)/2, len(set(ls)))
-
[🥰프로그래머스] 신규 아이디 추천 (2021 KAKAO BLIND RECRUITMENT)Algorithm/1일 1코테 2022. 2. 4. 09:28
신규 아이디 추천 https://programmers.co.kr/learn/courses/30/lessons/72410 코딩테스트 연습 - 신규 아이디 추천 카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로 programmers.co.kr 내 풀이 import re def solution(new_id): new_id = new_id.lower() # 소문자 변경 new_id = re.sub(r'[^a-z0-9-_.]', '', new_id) # 특정 특문, 영어, 숫자외 제거 new_id_list = list(new_id) # 리스트로 변경 for i in range(le..
-
[🥰프로그래머스] 로또의 최고 순위와 최저 순위Algorithm/1일 1코테 2022. 2. 3. 17:36
로또의 최고순위와 최저순위 https://programmers.co.kr/learn/courses/30/lessons/77484 코딩테스트 연습 - 로또의 최고 순위와 최저 순위 로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호 programmers.co.kr 내 풀이 def solution(lottos, win_nums): _zero = lottos.count(0) # 0의 갯수 _coincide = 0 # 일치하는 숫자 갯수 rank = [0,6,5,4,3,2,1] # 로또 순위 -> 6개(index) 다 맞으면 1,,, # 일치하는 갯수 세..