Skip to main content

Posts

Find Target Indices After Sorting Array

Find Target Indices After Sorting Array   You are given a 0-indexed integer array nums and a target element target. A target index is an index i such that nums[i] == target. Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order. Example 1: Input: nums = [1,2,5,2,3], target = 2 Output: [1,2] Explanation: After sorting, nums is [1,2,2,3,5]. The indices where nums[i] == 2 are 1 and 2. Example 2: Input: nums = [1,2,5,2,3], target = 3 Output: [3] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 3 is 3. Example 3: Input: nums = [1,2,5,2,3], target = 5 Output: [4] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 5 is 4. Example 4: Input: nums = [1,2,5,2,3], target = 4 Output: [] Explanation: There are no elements in nums with value 4.   Constraints: 1 <= nums.length <= 100 1 <= nums[...

Squares of a Sorted Array

Squares of a Sorted Array ( Easy) Link : https://leetcode.com/problems/squares-of-a-sorted-array/ Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.   Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121]   Constraints: 1 <= nums.length <= 104 -104 <= nums[i] <= 104 nums is sorted in non-decreasing order.   Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? Solution (C++) : vector < int > sortedSquares ( vector < int >& nums) { int n = nums.size(); int start= 0 ; int end=n -1 ; vector < int > res(n); int pos = n -1 ; ...

Decode Ways II

  Decode Ways II A message containing letters from A-Z can be encoded into numbers using the following mapping: 'A' -> "1" 'B' -> "2" ... 'Z' -> "26" To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into: "AAJF" with the grouping (1 1 10 6) "KJF" with the grouping (11 10 6) Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06". In addition to the mapping above, an encoded message may contain the '*' character, which can represent any digit from '1' to '9' ('0' is excluded). For example, the encoded message "1*" may represent any of the encoded messages "11", "12", "13", "14...

Longest Increasing Subsequence

Longest Increasing Subsequence Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7]. Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Example 2: Input: nums = [0,1,0,3,2,3] Output: 4 Example 3: Input: nums = [7,7,7,7,7,7,7] Output: 1   Constraints: 1 <= nums.length <= 2500 -104 <= nums[i] <= 104   Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?  Solution : C++ : class Solution { public : // There's a typical DP solution with O(N^2) Time and O(N) space // DP[i] means the result ends at i // So for dp[i], dp[i] is max(dp[j]+1), for all j < i and nums[j]...

Fair Elections January Challenge 2021 Division 3 FAIRELCT

Fair Elections  Problem Code:  FAIRELCT Elections are coming soon. This year, two candidates passed to the final stage. One candidate is John Jackson and his opponent is Jack Johnson. During the elections, everyone can vote for their favourite candidate, but no one can vote for both candidates. Then, packs of votes which went to the same candidate are formed. You know that for John Jackson, there are  N N  packs containing  A 1 , A 2 , … , A N A 1 , A 2 , … , A N  votes, and for Jack Johnson, there are  M M  packs containing  B 1 , B 2 , … , B M B 1 , B 2 , … , B M  votes. The winner is the candidate that has strictly more votes than the other candidate; if both have the same number of votes, there is no winner. You are a friend of John Jackson and you want to help him win. To do that, you may perform the following operation any number of times (including zero): choose two packs of votes that currently belong to different candidates and ...