Skip to main content

Posts

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 ...