Leetcode - 121. Best Time to Buy and Sell Stock
리트코드 Best time to buy and sell stock 문제를 풀었다. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. Example 2: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0. 배열을 입력으로 받고 언제 사서 언제 팔면 이익이 가장 클지 계산하는 문제다...
2020. 8. 3.
Leetcode - 121. Best Time to Buy and Sell Stock
리트코드 Best time to buy and sell stock 문제를 풀었다. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. Example 2: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0. 배열을 입력으로 받고 언제 사서 언제 팔면 이익이 가장 클지 계산하는 문제다...
2020. 7. 31.
병합 정렬 javascript / merge sort
병합 정렬은 배열을 일정 크기로 분할한 다음 정렬하고 합치는 방식이다. [8,7,6,5,4,3,2,1] 이렇게 숫자가 있다면 [8,7,6,5] [4,3,2,1] [8,7] [6,5] [4,3] [2,1] 이렇게 나눈다. 그리고 나눠진 배열을 정렬한다. [7,8] [5,6] [3,4] [1,2] 앞에서부터 하나씩 비교하고 빼내서 결과를 만든다. [7,8] [5,6] 5가 더 작다. 5를 빼낸다. [5] [7,8] [6] 6이 더 작다. 6을 빼낸다. [5,6] [7,8] [] 배열에 7,8을 넣는다. [5,6,7,8] 이렇게 분할 된 두 배열을 합친다. 앞쪽의 5,6,7,8이 정렬되었다. 뒤쪽의 4,3,2,1도 마찬가지로 해본다. [3,4] [1,2] 1이 더 작다. 1을 빼낸다. [1] [3,4] [2]..
2020. 7. 26.