본문 바로가기
반응형

분류 전체보기156

python dictionary sort 정리 (sort by key & value) 매번 헷갈리고 알고리즘 문제 풀때마다 찾아보길래 이번 기회에 블로그에 적음으로 찾는 수고를 덜고자 한다. 프로그래머스에서 이번에 위클리 챌린지라고 leetcode에서 하는것 처럼 매주 문제 하나씩 내는데 4주차 문제에서 dictionary를 sort해야하는 문제가 나왔다. 3주차는 어려워서 건너뛰고 4주차 풀었다. 내가 생각하는 이 문제의 제일 중요한 부분은 dictionary 자료형의 sort 부분이다. 2번에 대해 자세히 서술해보면 구글에 "python dictionary sort" 를 검색했을때 가장 많이 나오는 답으로는 sort by value 다. value값 크기 대소를 통해 sort 하는 방법이다. # sort by value Ascending result = sorted(dictionary.. 2021. 8. 24.
Reverse Nodes in k-Group [LeetCode] 문제 풀이 정리 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed. Example 1: Input.. 2021. 7. 18.
Find Peak Element [LeetCode] 문제 풀이 정리 Find Peak Element A peak element is an element that is strictly greater than its neighbors. Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] = -∞. You must write an algorithm that runs in O(log n) time. Example 1: Input: nums = [1,2,3,1] Output: 2 Ex.. 2021. 7. 14.
Isomorphic Strings [LeetCode] 문제 풀이 정리 문제 Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself. Example 1: Input: s = "egg", t = "add" Outpu.. 2021. 7. 12.
Find Median from Data Stream [LeetCode] 문제 풀이 정리 문제 The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. For example, for arr = [2,3,4], the median is 3. For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5. Implement the MedianFinder class: MedianFinder() initializes the MedianFinder object. void addNum(int nu.. 2021. 7. 12.
Reduce Array Size to The Half [LeetCode] 문제 풀이 정리 Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array. Return the minimum size of the set so that at least half of the integers of the array are removed. Example 1: Input: arr = [3,3,3,3,5,5,5,2,2,7] Output: 2 Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the.. 2021. 7. 7.
Reshape the Matrix [LeetCode] 문제 풀이 정리 In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data. You are given an m x n matrix mat and two integers r and c representing the row number and column number of the wanted reshaped matrix. The reshaped matrix should be filled with all the elements of the original matrix in the same row.. 2021. 7. 6.
H-index [프로그래머스] 문제 풀이 정리 문제 설명 H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다. 어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값이 이 과학자의 H-Index입니다. 어떤 과학자가 발표한 논문의 인용 횟수를 담은 배열 citations가 매개변수로 주어질 때, 이 과학자의 H-Index를 return 하도록 solution 함수를 작성해주세요. 제한사항 과학자가 발표한 논문의 수는 1편 이상 1,000편 이하입니다. 논문별 인용 횟수는 0회 이상 10,000회 이하입니다. 입출력 예 cita.. 2021. 7. 6.
소수 찾기 [프로그래머스] 문제 풀이 정리 문제 설명 한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다. 각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이 조각으로 만들 수 있는 소수가 몇 개인지 return 하도록 solution 함수를 완성해주세요. 제한사항 numbers는 길이 1 이상 7 이하인 문자열입니다. numbers는 0~9까지 숫자만으로 이루어져 있습니다. "013"은 0, 1, 3 숫자가 적힌 종이 조각이 흩어져있다는 의미입니다. 입출력 예 numbers / return "17" 3 "011" 2 입출력 예 설명 예제 #1 [1, 7]으로는 소수 [7, 17, 71]를 만들 수 있습니다. 예제 #2 [0, 1, 1]으.. 2021. 7. 5.
반응형