Solution of Weekly Contest 170
[Easy] 1309. Decrypt String from Alphabet to Integer Mapping
Given a string s
formed by digits ('0'
- '9'
) and '#'
. We want to map s
to English lowercase characters as follows:
- Characters (
'a'
to'i')
are represented by ('1'
to'9'
) respectively. - Characters (
'j'
to'z')
are represented by ('10#'
to'26#'
) respectively.
Return the string formed after mapping. It’s guaranteed that a unique mapping will always exist.
Example
1 | Input: s = "10#11#12" |
Solution
We only have to care about i + 2
and see if it is #
, if so, we can get the number and then 'j' + (number - 10)
, if not, we just use one character each time, and then 'a' + (number - 1)
.But the move would be different, it moves i + 3, i + 1 separately.
Code
1 | // time: O(n) space: O(n) |
[Medium] 1310. XOR Queries of a Subarray
Given the array arr
of positive integers and the array queries
where queries[i] = [Li, Ri]
, for each query i
compute the XOR of elements from Li
to Ri
(that is, arr[Li] xor arr[Li+1] xor ... xor arr[Ri]
). Return an array containing the result for the given queries
.
Example
1 | Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] |
Solution
Use prefix strategy and we maintain the arr to store the prefix XOR value. Also, we have to use the a ^ b ^ b = a
to solve this problem. For example, we have the arr [a, b, c, d, e, f]
. And if we have to calculate [4,5] in [0, 5]. And we can get the prefix[4] = abcd
and prefix[6] = abcdef
and we get ef
.
Code
1 | // time:O(n) space:O(n) |
[Medium] 1311. Get Watched Videos by Your Friends
There are n
people, each person has a unique id between 0
and n-1
. Given the arrays watchedVideos
and friends
, where watchedVideos[i]
and friends[i]
contain the list of watched videos and the list of friends respectively for the person with id = i
.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path equal to k with you. Given your id
and the level
of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example
1 | Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1 |
Solution
We add the id
into the queue and see it’s friends and iterate k
levels. At the end we get person we need and see the moveis they watched and sort it.
Code
1 | // time:O(nlogn) space:O(n) |
[Hard] 1312. Minimum Insertion Steps to Make a String Palindrome
Given a string s
. In one step you can insert any character at any index of the string.
Return the minimum number of steps to make s
palindrome.
A Palindrome String is one that reads the same backward as well as forward.
1 | Input: s = "mbadm" |
Solution
We set left as 0, right as n - 1
and compare these two, if it equals, we just need to focus on [left +1, right - 1]
and if doesn’t we have to ways to make it palindrome, we can add char in left side or right side. So, we have to get min([left, right-1], [left + 1, right]) + 1
. The following code uses top-down approach with memo.
Code
1 | // time:O(n^2) space:O(n^2) |
Reference
Rank: 2742 / 6833. Solved 2/4 in 1:30:00.