노트/Algorithm : 알고리즘(63)
-
[leetcode] 238. Product of Array Except Self
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. Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums =..
2022.11.08 -
[leetcode] 121. Best Time to Buy and Sell Stock
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 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanat..
2022.11.08 -
[leetcode] 561. Array Partition
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. Example 1: Input: nums = [1,4,3,2] Output: 4 Explanation: All possible pairings (ignoring the ordering of elements) are: 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 2. (1, 3), (2, 4) -> min(1,..
2022.11.04 -
[leetcode] 15. 3Sum
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] for i in range(len(nums)-2): for j in range(i+1, len(nums)-1): for k in range(j+1, len(nums)): if nums[i] + nums[j] + nums[k] == 0: res.append(sorted([nums[i], nums[j] , nums[k]])) return list(set(map(tuple, res))) => 브루트포스 방식 : Timeout class Solution(object): def threeSum(self, nums)..
2022.11.03 -
[leetcode] 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) 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. Example 2: Input:..
2022.11.02 -
리스트에서 최솟값 찾기 구현
candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500] # Write loop to find the ideal tree size from candidate_max_leaf_nodes compare = [] for max_leaf_nodes in candidate_max_leaf_nodes: mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y) print('max_leaf_nodes: %d , mae: %s' %(max_leaf_nodes, mae)) if not compare: # 빈 리스트 라면 compare.append((max_leaf_nodes,mae)) if compare: # 빈리스트가 아니면 (..
2021.08.11