본문 바로가기

LeetCode84

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 628. Maximum Product of Three Numbers Problem: Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer. -Summa.. 2020. 7. 9.
Leetcode. Squares of a Sorted Array Problem: Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: Input: [-7,-3,2,3,11] Output: [4,9,9,49,121] -Summary- Squares of each element in Array, then sort them using sort() function. class Solution: def sortedSquares(self, A: List[in.. 2020. 7. 4.