이카's
반응형
[Algorithm] LeetCode #6 Maximum Product Subarray

Problems Maximum Product Subarray Medium Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer. A subarray is a contiguous subsequence of the array. 인접한 원소들과 곱샘을 했을 때, 가장 큰 결과값을 반환 Example 1: Input: nums = [2,3,-2,4] Output: 6 Explanation..

[Algorithm] LeetCode #5 Maximum Subarray

Problems Maximum Subarray Easy 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] ..

[Algorithm] LeetCode #4 Product of Array Except Self

Problem 238 Product of Array Except Self Medium 12160 740 Add to List Share Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Exa..

[Algorithm] LeetCode #3 Contains Duplicate

Problems Contains Duplicate Easy 4591 953 Add to List Share Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example 1: Input: nums = [1,2,3,1] Output: true Example 2: Input: nums = [1,2,3,4] Output: false Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true 문제 이해 굉장히 쉬운 문제다. 배열안에 중복되는 숫자가 있으면 True없으면..

[Algorithm] LeetCode #2 Best time to buy and sell stock

LeetCode - Best time to buy and sell stock Problems You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example..

[Algorithm] LeetCode #1 Two Sum solved

Brute Force 내 방식 def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i + 1 , len(nums)): if nums[i] + nums[j] == target: return [i, j] Other Method Two pass Hash Table def twoSum(self, nums: List[int], target: int) -> List[int]: talbe = {num: i for i, num in enumerate(nums)} for idx, num in enumerate(nums): if ((target - num) in table) and (idx ..

반응형