Coding test/LeetCode
[LeetCode] Find All Numbers Disappeared in an Array
🔍 문제 Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums. Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6] Input: nums = [1,1] Output: [2] Tip) The intuition behind using a hash map is pretty clear in this case 🖥 실행 1) Answer(Hash Table) class Solution(object): def findDisappearedNumbers(self, num..
[LeetCode] Third Maximum Number
🔍 문제 Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. Input: nums = [3,2,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1. 🖥 실행 1) Answer class Solution: def thirdMax(self, nums: List[int]) -> int: for i in nums: while num..
[LeetCode] Max Consecutive Ones II
🔍 문제 Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0. nput: nums = [1,0,1,1,0] Output: 4 Explanation: Flip the first zero will get the maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4. 🖥 실행 1) Answer class Solution(object): def findMaxConsecutiveOnes(self, nums): # previous and current l..
[LeetCode] Height Checker
🔍 문제 A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line. You are given an integer array heights representing the current order that the students are standing in. Eac..
[LeetCode] Move Zeroes
🔍 문제 Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Input: nums = [0] Output: [0] 🖥 실행 1) Answer class Solution: def moveZeroes(self, nums: List[int]) -> None: n = 0 while nums.count(0) > n: nums.remove(..
[LeetCode] Replace Elements with Greatest Element on Right Side
🔍 문제 Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array. Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is in..
[LeetCode] Valid Mountain Array
🔍 문제 Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if: arr.length >= 3 There exists some i with 0 ... > arr[arr.length - 1] Input: arr = [2,1] Output: false Input: arr = [3,5,5] Output: false Input: arr = [0,3,2,1] Output: true 🖥 실행 ..
[LeetCode] Merge Sorted Array
🔍 문제 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a ..