본문 바로가기

전체 글137

Leet Code 83. Remove Duplicates from Sorted List [Easy] Problem: Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 -Summary- 1. Set root as head first. 2. Move the head and compare the current value with the next value. If it is the same, point next to next.next. Otherwise, move head to the next. 3. Return root -Code- # Defi.. 2020. 9. 8.
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.