본문 바로가기

Python130

LeetCode 561. Array Partition I [Easy] Problem: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4). Note: n is a positive integer, which is in the range of [1, 10000.. 2020. 8. 26.
LeetCode 15. 3Sum [Medium] Problem: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Summary: Using a two-pointer move left and the right pointer to compare the sum of three-element. Code: class Solution: def threeSum(self, nums: List[int]) ->.. 2020. 8. 25.
LeetCode 42. Trapping Rain Water [Hard] Problem: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: .. 2020. 8. 24.
LeetCode 49. Group Anagrams Problem: Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Summary: 1. Create a defaultdict(list) to save the sorted word as a key in list format. 2. For each word, sort the word so that any anagram word would be saved in a list under same key. 3. return with all values saved in .. 2020. 8. 19.
LeetCode 819. Most Common Word Problem: Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique. Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase. Example: Input: para.. 2020. 8. 18.
LeetCode 937. Reorder Data in Log Files Problem: You have an array of logs. Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric identifier. Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word after the identifier will consist only of digits. We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that eac.. 2020. 8. 17.