Algorithm/LeetCode
-
[LeetCode] [DP] 118. Pascal's TriangleAlgorithm/LeetCode 2021. 10. 25. 13:09
118. Pascal's Triangle Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: Example 1: Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] Example 2: Input: numRows = 1 Output: [[1]] class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: ..
-
[LeetCode] [DP] 746. Min Cost Climbing StairsAlgorithm/LeetCode 2021. 10. 8. 10:11
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1. Return the minimum cost to reach the top of the floor. Example 1: Input: cost = [10,15,20] Output: 15 Explanation: Cheapest is: start on cost[1], pay that cost, and go to ..
-
[LeetCode] [DP] 53. Maximum SubarrayAlgorithm/LeetCode 2021. 10. 5. 08:56
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. A subarray is a contiguous part of an array. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Example 2: Input: nums = [1] Output: 1 Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Constraints: 1
-
[LeetCode]229. Majority Element IIAlgorithm/LeetCode 2021. 9. 29. 10:58
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Follow-up: Could you solve the problem in linear time and in O(1) space? Example 1: Input: nums = [3,2,3] Output: [3] Example 2: Input: nums = [1] Output: [1] Example 3: Input: nums = [1,2] Output: [1,2] Constraints: 1