본문 바로가기

코딩92

LeetCode. Best Time to Buy and Sell Stock [Dynamic Programming] Problem: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sel.. 2020. 6. 10.
LeetCode. Power of Two Problem: Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: Input: 16 Output: true Explanation: 24 = 16 Example 3: Input: 218 Output: false -Summary- 1. Keep divide number by 2. In the iteration, if any number divide by 2 is not 0, then return False 2. After the loop, return True since the number is a power of .. 2020. 6. 9.
LeetCode 2. Add Two Numbers [Linked List] Problem: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation.. 2020. 6. 9.
LeetCode. First Bad Version [Sorting and Searching] Problem: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following o.. 2020. 6. 8.
LeetCode. Merge Sorted Array [Sorting and Searching] Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. Example: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5.. 2020. 6. 8.
LeetCode. Convert Sorted Array to Binary Search Tree [Trees] Problem: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: -Summary- Solve by recursive function call 1. First, find a root first by finding the midpoint of the given integer array. 2.. 2020. 6. 7.