파이썬 딕셔너리 11

[LeetCode] 딕셔너리 알파벳 빈도수: 1255. Maximum Score Words Formed by Letters

https://leetcode.com/problems/maximum-score-words-formed-by-letters/ Maximum Score Words Formed by Letters - 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 collections.Count()를 사용해서 letters에 있는 알파벳들의 개수를 파악한다. 이를 count라고 할 때 words의 문자열을 확인할 때마다 count의 개수를 줄여나가야 한다. words의 문자열을 취해서..

알고리즘 2022.04.14

[리트코드] Hash Table 문제: 347. Top K Frequent Elements (python 딕셔너리, JavaScript map)

https://leetcode.com/problems/top-k-frequent-elements/ Top K Frequent Elements - 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 풀이법은 간단하다. 각 빈도수를 체크해서 hash로 저장하고 이걸 빈도수 역순 정렬해서 k개만큼의 키를 반환해주면 된다. 자바스크립트는 map을 활용하고 파이썬은 딕셔너리를 활용하여 풀었다. 각 언어의 문법을 잘 활용할 수 있는 문제이다. var topKFrequent = ..

알고리즘 2022.03.30

[Python] 리트코드 Hash Table, Sorting 문제 (767. Reorganize String) 딕셔너리 정렬

https://leetcode.com/problems/reorganize-string/ Reorganize String - 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 주어진 s를 붙어있는 문자들이 겹치치 않도록 재 정렬하는 문제이다. ==풀이== 각 문자의 빈도수 구해서 딕셔너리를 내림차순 정렬 answer에 [0]부터 2씩 건너뛰며 단어 입력. 끝까지 돌았다면 이번엔 [1]부터 단어 입력 * 만약 최대 빈도수 값이 s의 총개수보다 크다면 한 바퀴를 돌고 맨..

알고리즘 2022.03.14

[Python] 리트코드 Hash Table, Sorting문제(791. Custom Sort String)

https://leetcode.com/problems/custom-sort-string/ Custom Sort String - 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 우선 s의 문자들 중 order에 존재하는 문자를 order에서의 정렬 순서대로 정렬한 후 나머지 문자를 s에 있던 순서대로 정렬한다. 여기서 주의해야 할 점은 order는 모든 문자가 유니크하지만 s는 중복이 허용되기 때문에 s에 있는 모든 중복 문자는 order에 있는 순서대로 정렬되어야..

알고리즘 2022.03.14

[Python] 프로그래머스: 베스트앨범(딕셔너리 활용)

https://programmers.co.kr/learn/courses/30/lessons/42579 코딩테스트 연습 - 베스트앨범 스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가 programmers.co.kr import collections def solution(genres, plays): answer = [] # 1 sumOfGenres = collections.defaultdict(int) # 장르별 총 재생 횟수 playsListDict = collections.defaultdict(list) # 장르별로 나눠서 재생횟수와 인덱스 리스트로 저장 # 2 fo..

알고리즘 2021.11.09

[Python] 리트코드 514. Freedom Trail

https://leetcode.com/problems/freedom-trail/ Freedom Trail - 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 리스트 ring은 [0]번 요소가 12시 방향에 위치하고 뒤따르는 요소들이 시계방향으로 위치한 다이얼 모양 원이다. 선택해야 하는 알파벳인 key가 주어진다. ring을 시계방향 혹은 반시계 방향으로 돌리면서 key에 해당하는 알파벳을 순서대로 고를 건데 이때 다이얼을 가장 적게 돌리는 경우의 횟수를 구하라...

알고리즘 2021.09.15

[Python] 리트코드 403. Frog Jump (Array, DP)

https://leetcode.com/problems/frog-jump/ Frog Jump - 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 개구리가 강을 건너는데 그 강은 몇 개의 유닛으로 쪼개져 있다. 각 유닛엔 돌이 있을 수도 있고 없을 수도 있다. 오름차순 리스트인 stones가 주어질 때 개구리가 맨 끝까지 갈 수 있다면 true를 반환한다. 맨 처음에는 1칸을 뛸 수 있으며 개구리가 마지막에 k유닛을 점프했다면 다음에 점프할 수 있는 유닛의 간격은 k..

알고리즘 2021.09.10

[Python] 리트코드 451. Sort Characters By Frequency (딕셔너리 정렬, 요소 길이별 정렬)

https://leetcode.com/problems/sort-characters-by-frequency/ Sort Characters By Frequency - 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 ==문제== 알파벳 문자열이 주어졌을 때 정렬하는 문제이다. 첫 번째 정렬 조건은 반복되는 횟수가 많은 순서이고 두 번째 정렬 조건은 알파벳 순서대로 정렬하면 된다. ==풀이== 방법은 아주 간단하다. 딕셔너리에 (key:알파벳, value:반복 횟수)로 ..

알고리즘 2021.08.08

[Python] 리트코드 560. Subarray Sum Equals K (딕셔너리 키에 해당하는 값 없으면)

https://leetcode.com/problems/subarray-sum-equals-k/ Subarray Sum Equals K - 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 ==문제== nums배열에서 더해서 k가 되는 부분 배열의 가짓수를 구하는 문제이다. ==풀이== 누적합계인 sums를 선언해서 합계에 어떤 숫자가 더해질 때마다 sums-k 값을 구한다. 이 값이 이 전에 구했던 sums의 이력 중 어떤 것이라도 일치하는 게 있다면 부분 배열 개..

알고리즘 2021.07.21

[Python] 리트코드 877 : Stone Game (DP)

https://leetcode.com/problems/stone-game/submissions/ Stone Game - 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 짝수개의 돌무더기가 있을 때 Alex와 Lee가 한 무더기씩 번갈아가면서 가져갈 것이다. 양쪽 끝에 있는 돌무더기만 가져갈 수 있을 때 Alex가 이길 수 있으면 True를 반환한다. piles의 첫 번째를 piles[i]라고 하고 끝을 piles[j]라고 할 때 piles[i, i+1, i+2, ..

알고리즘 2021.06.16