본문 바로가기

leetcode6

[Python-Leetcode] 509. Fibonacci Number (difficulty : Easy - ☆) 문제 설명 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given n, calculate F(n). Example 1: Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: n = 3 Output: 2 Explanation.. 2022. 5. 6.
[Python-Leetcode] 2. Add Two Numbers (difficulty : Medium - ☆☆) 문제 설명 2. Add Two Numbers 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 contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [.. 2022. 4. 30.
[Python-Leetcode] 4. Median of Two Sorted Arrays (difficulty : Hard - ☆☆☆) 문제 설명 Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and med.. 2022. 4. 27.
[Python-Leetcode] 232. Implement Queue using Stacks (difficulty : Easy - ☆) 문제 설명 Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. b.. 2021. 8. 12.
[Python-Study Leetcode 문제풀이] 665. Non-decreasing Array Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decreasing if nums[i] [4, 2, 1]의 경우, 2개 이상의 수를 변경해야 하므로, Fail [Solution Code] [1, 2, 3, 4, 5, 6, 4] 라는 배열이 있다고 가정하자 그러면, 각 숫자의 차이값을 계산한다면 [1, 2, 3, 4, 5, 6, 5] ↓ [1, 1, 1, 1, 1, -1] 이라는 배열을 얻을 수 있다. 1) 이때, 모든 배열의 값이 0보다 크거나 같다면, 해당 배열은 모두 증가하는 추세이.. 2021. 3. 17.
[Python-Study Leetcode 문제풀이] 121. Best Time to Buy and Sell Stock (Easy) 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. (Easy) Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 E.. 2021. 3. 14.