본문 바로가기

coding41

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.
Leetcode. Find Numbers with Even Number of Digits Problem: Given an array nums of integers, return how many of them contain an even number of digits. -Summary- 1. Format change int to string for each number and count the length of the str format of the number. 2. Check if it is even or odd number of digits. class Solution: def findNumbers(self, nums: List[int]) -> int: count = 0 for num in nums: if len(str(num)) % 2 == 0: count += 1 return coun.. 2020. 7. 1.
Leetcode. Max Consecutive Ones Problem: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000 class Solution: def fi.. 2020. 6. 29.
LeetCode. Sum Root to Leaf Numbers Problem: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. -Summary- Used recursive DFS. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=Non.. 2020. 6. 27.
LeetCode. Min Stack Problem: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Example 1: -Summary- Use tuple to save the minimum value and the current value of the stack. class MinStack: def __ini.. 2020. 6. 26.