본문 바로가기
반응형

Algorithm41

입실 퇴실 [프로그래머스] 프로그래머스 7주차 문제이다. 입실과 퇴실이 반복되는 방에서 어떤 경우에도 반드시 만날 수 밖에 없는 손님을 카운트 하는 문제였다. 어떤 경우에는 만나고, 어떤 경우에는 안만나는 상황은 알고리즘 적인 상황이 아니니 분명 무슨 경우의 수가 있을 것이라 가정하고 풀었다. 공책에 몇번 써가면서 내가 세운 규칙은 입실과 퇴실중 입실먼저 수행한다. 방이 비어있는데 입실과 퇴실이 같다면 아무도 만나지 않고 나간다. 퇴실[0]과 입실[last]가 다르다면 입실 먼저! 퇴실[0]와 입실[last]가 같다면 입실 수행후 퇴실 번호가 없을때까지 퇴실만 수행 입실이 비어있다면 반복을 종료! 로 세우고 문제를 풀었다. 손님들 끼리 만나는 경우 meet_matrix 라는 행렬을 만들어 "O"로 표시, 자기 자신은 만남의 카운트.. 2021. 9. 15.
복서 정렬하기 [프로그래머스] 저번 4주차 때와 마찬가지로 딕셔너리 소팅 문제로 접근해 풀었다. python dictionary sort 정리 (sort by key & value) python dictionary sort 정리 (sort by key & value) 매번 헷갈리고 알고리즘 문제 풀때마다 찾아보길래 이번 기회에 블로그에 적음으로 찾는 수고를 덜고자 한다. 프로그래머스에서 이번에 위클리 챌린지라고 leetcode에서 하는것 처럼 매주 문제 hoonzi-text.tistory.com 각 복서 마다 속성들 몇번 이겼는지 ⇒ win_count 자기보다 무거운 사람과 싸워서 이겼는지 ⇒ weight_count 몇번 선수인지 ⇒ index 자기 몸무게는 몇인지 ⇒ weights[index] 등을 dictionary로 저장하고 해.. 2021. 9. 7.
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.
반응형