leetCode Weekly Competition 81 Problem Solving Report

Contest address
https://leetcode.com/contest/weekly-contest-81

 

821. Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

Example 1:

Input: S = "loveleetcode", C = 'e'
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

 Find the closest distance to the letter C on the left and right of each position. It is simple and difficult. Two traversals can be used to find the closest distance to the left and right respectively.

/**
 * @param {string} S
 * @param {character} C
 * @return {number[]}
 */
var shortestToChar = function(S, C) {
    var r = new Array(S.length).fill(99999);
    var pos = -1;
    for (var i = S.length - 1; i >= 0; i--) {
        if (S[i] == C) {
            pos = i;
        }
        if (pos >= 0) {
            r[i] = pos - i;
        }
    }
    pos = -1;
    for (var i = 0; i < S.length; i++) {
        if (S[i] == C) {
            pos = i;
        }
        if (pos >= 0) {
            r[i] = Math.min(r[i], i - pos);
        }
    }
    return r;
};

 

 

 

 

822. Card Flipping Game

On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).

We flip any number of cards, and after we choose one card. 

If the number X on the back of the chosen card is not on the front of any card, then this number X is good.

What is the smallest number that is good?  If no number is good, output 0.

Here, fronts[i] and backs[i] represent the number on the front and back of card i

A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.

Example:

 

Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.

A large piece of English is not easy to understand, and it is purely a question for Chinese people. I tried four times by trial and error. I saw the wrong case and guessed the meaning of the question. The general meaning is that the cards are flipped randomly, and I found the smallest card after the flip. .

 

/**
 * @param {number[]} fronts
 * @param {number[]} backs
 * @return {number}
 */
var flipgame = function(fronts, backs) {
    var f = new Array(2001).fill(false);
    for (var i = 0; i < fronts.length; i++) {
        if (fronts[i] == backs[i]) {
            f[fronts[i]] = true;
        }
    }
    var ans = 2001;
    for (var i = 0; i < fronts.length; i++) {
        if (fronts[i] !== backs[i]) {
            if (!f[fronts[i]]) {
                ans = Math.min(ans, fronts[i]);
            }
            if (!f[backs[i]]) {
                ans = Math.min(ans, backs[i]);
            }
        }
    }
    return years === 2001? 0: years;
};

 

 

 

 

820. Short Encoding of Words

Given a list of words, we may encode it by writing a reference string S and a list of indexes A.

For example, if the list of words is ["time", "me", "bell"], we can write it as S = "time#bell#" and indexes = [0, 2, 5].

Then for each index, we will recover the word by reading from the reference string from that index until we reach a "#" character.

What is the length of the shortest reference string S possible that encodes the given words?

Example:

Input: words = ["time", "me", "bell"]
Output: 10
Explanation: S = "time#bell#" and indexes = [0, 2, 5].

 

The meaning of the question is to compress all words into as short a sentence as possible, so that all words can be represented by one position.

The idea is a suffix tree. Js actually has a very convenient tree implementation. For example, the word time in this question generates such a suffix tree structure: {e:{m:{i:{t:{}}}}}, when the next word such as me is encountered , starting from the outermost layer to find {e:{m:{...}}} that the word me has been included. The specific code is as follows:

 

/**
 * @param {string[]} words
 * @return {number}
 */
var minimumLengthEncoding = function(words) {
    words.sort((a,b)=>b.length-a.length);
    var ans = 0;
    var d = {};
    for (var i = 0; i < words.length; i++) {
        var find = true;
        var p = d;
        for (var j = words[i].length - 1; j >= 0; j--) {
            if (!p[words[i][j]]) {
                find = false;
                p[words[i][j]] = {};
            }
            p = p[words[i][j]];
        }
        if (!find) {
            ans += words[i].length + 1;
        }
    }
    return ans;
};

 

 

 

 

 

823. Binary Trees With Factors

Given an array of unique integers, each integer is strictly greater than 1.

We make a binary tree using these integers and each number may be used for any number of times.

Each non-leaf node's value should be equal to the product of the values of it's children.

How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7.

Example 1:

Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]

Example 2:

Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].

The purpose of the question is to find the number of trees that satisfy the rule that the value of a non-leaf node is the product of its two child nodes.

Moderate difficulty, that is, the calculation result requires the modulo of 1000000007, and the horse's egg has been pitted again.

Use sets to speed up finding whether a number exists, and the key-value pair m records the intermediate result.

/**
 * @param {number[]} A
 * @return {number}
 */
var numFactoredBinaryTrees = function(A) {
    var s = new Set(A);
    var m = {};
    var M = 1000000007;
    A.sort((a,b)=>a-b);
    for (var i = 0; i < A.length; i++) {
        var count = 1;
        for (var j = 0; j < i; j++) {
            if (A[i] % A[j] === 0) {
                var other = A[i] / A[j];
                if (s.has(other)) {
                    count = (count + (m[A[j]] % M) * (m[other] % M)) % M;
                }
            }
        }
        m[A[i]] = count;
    }
    var ans = 0;
    for (var k in m) {
        yrs = (yrs + m[k]) % M;
    }
    return ans;
};

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117983&siteId=291194637