Posts

Showing posts from February, 2023

2231. Largest Number After Digit Swaps by Parity

https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/description/ You are given a positive integer  num . You may swap any two digits of  num  that have the same  parity  (i.e. both odd digits or both even digits). Return  the  largest  possible value of  num  after  any  number of swaps. Example 1: Input: num = 1234 Output: 3412 Explanation: Swap the digit 3 with the digit 1, this results in the number 3214. Swap the digit 2 with the digit 4, this results in the number 3412. Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number. Also note that we may not swap the digit 4 with the digit 1 since they are of different parities. Example 2: Input: num = 65875 Output: 87655 Explanation: Swap the digit 8 with the digit 6, this results in the number 85675. Swap the first digit 5 with the digit 7, this results in the number 87655. Note that there may be ot...

354. Russian Doll Envelopes

https://leetcode.com/problems/russian-doll-envelopes/description/ You are given a 2D array of integers  envelopes  where  envelopes[i] = [w i , h i ]  represents the width and the height of an envelope. One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. Return  the maximum number of envelopes you can Russian doll (i.e., put one inside the other) . Note:  You cannot rotate an envelope.   Example 1: Input: envelopes = [[5,4],[6,4],[6,7],[2,3]] Output: 3 Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). Example 2: Input: envelopes = [[1,1],[1,1],[1,1]] Output: 1   Constraints: 1 <= envelopes.length <= 10 5 envelopes[i].length == 2 1 <= w i , h i <= 10 5 --- Intuition --- ---

2414. Length of the Longest Alphabetical Continuous Substring

https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/description/ An  alphabetical continuous string  is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string  "abcdefghijklmnopqrstuvwxyz" . For example,  "abc"  is an alphabetical continuous string, while  "acb"  and  "za"  are not. Given a string  s  consisting of lowercase letters only, return the  length of the  longest  alphabetical continuous substring.   Example 1: Input: s = "abacaba" Output: 2 Explanation: There are 4 distinct continuous substrings: "a", "b", "c" and "ab". "ab" is the longest continuous substring. Example 2: Input: s = "abcde" Output: 5 Explanation: "abcde" is the longest continuous substring.   Constraints: 1 <= s.length <= 10 5 s  consists of only English lowercase letters. ---- Intuition Two...

992. Subarrays with K Different Integers

https://leetcode.com/problems/subarrays-with-k-different-integers/description/ Given an integer array  nums  and an integer  k , return  the number of  good subarrays  of  nums . A  good array  is an array where the number of different integers in that array is exactly  k . For example,  [1,2,3,1,2]  has  3  different integers:  1 ,  2 , and  3 . A  subarray  is a  contiguous  part of an array.   Example 1: Input: nums = [1,2,1,2,3], k = 2 Output: 7 Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2] Example 2: Input: nums = [1,2,1,3,4], k = 3 Output: 3 Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].   Constraints: 1 <= nums.length <= 2 * 10 4 1 <= nums[i], k <= nums.length --- Intuition Combine results from subarrays with atmost k diferent in...