목록Leetcode (26)
mjk study log
문제Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.You have the following three operations permitted on a word:Insert a characterDelete a characterReplace a character Example 1:Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') Example 2..
문제You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.Note that an empty array is considered to have distinct elements. Return the minimum number of op..
문제Given a binary array nums, you should delete one element from it.Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray. Example 1:Input: nums = [1,1,0,1] Output: 3 Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's. Example 2:Input: nums = [0,1,1,1,0,1,1,0,1] Output: 5 ..
문제Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2:Input: s = "cbbd" Output: "bb" Constraints:1 풀이palindrome의 시작이 되는 곳을 s의 처음부터 하나씩 구해준다.이때 s[i]==s[i+1], s[i-1]==s[i+1] 경우 둘다 고려해줘야 한다. 중요한점은 while loop 에서 인덱스를 먼저 condition으로 둬야 다음 s[left] == s[right] 비교할때 index out of bound 에러가 뜨지 않는..
문제Given an integer array nums, find a subarray that has the largest product, and return the product.The test cases are generated so that the answer will fit in a 32-bit integer. Example 1:Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2:Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray. Constraints:..
문제 Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. Example 1:Input: nums = [1,5,11,5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2:Input: nums = [1,2,3,5] Output: false Explanation: The array cannot be partitioned into equal sum subset..
문제Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1:Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2:Input: root = [1,2,3] Output: [1,3] Constraints:The number of nodes in the tree will be in the range [0, 104].-231 풀이 bfs를 사용하는 문제이다.각 row를 bfs 로 찾고, for loop안의 각 element의 최댓값을 찾아준다.코드/** * Definition for a binary tree ..
문제Given a string s, reverse only all the vowels in the string and return it.The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once. Example 1:Input: s = "IceCreAm"Output: "AceCreIm"Explanation:The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".Example 2:Input: s = "leetcode"Output: "leotcede" Constraints:..
문제Given an integer array nums, handle multiple queries of the following types:Update the value of an element in nums.Calculate the sum of the elements of nums between indices left and right inclusive where left Implement the NumArray class:NumArray(int[] nums) Initializes the object with the integer array nums.void update(int index, int val) Updates the value of nums[index] to be val.int sumRang..
문제Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string. Example 1:Input: arr = [..