168 Course Race Week

1. The maximum number of occurrences of the substring

Give you a string s, please return to the following conditions and the maximum number of occurrences of an arbitrary substring occurrences:

The number of substrings different letters must be less than or equal maxLetters.
Substring length must be greater than or less minSize maxSize.
 

Example 1:

Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: substring "aab" appears twice in the original string.
It meets all the requirements: 2 different letters, a length of 3 (and in minSize maxSize range).
Example 2:

Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: substring "aaa" appears twice in the original string, and they have overlapping portions.
Example 3:

Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:

Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
 

prompt:

. 1 <= s.length <^ = 10. 5
. 1 <= maxLetters <= 26 is
. 1 <= the minSize <= the maxSize <= min (26 is, s.length)
S contains only lowercase letters.

/**
 * @param {string} s
 * @param {number} maxLetters
 * @param {number} minSize
 * @param {number} maxSize
 * @return {number}
 */
// maxSize 根本没有用
var maxFreq = function(s, maxLetters, minSize, maxSize) {
  let lenMap = {};
  let ans = 0;
  for (let i = 0; i < s.length - minSize + 1; i++) {
    const str = s.substr(i, minSize);
    if (new Set(str.split('')).size <= maxLetters) {
      if (lenMap[str]) {
        lenMap[str]++; 
      } Else { 
        lenMap [str] = 1 ; 
      } 
      If (lenMap [str]> years) { 
        years = lenMap [str]; 
      } 
    } 
  } 
  Return year; 
};
/**
 * @param {string} s
 * @param {number} maxLetters
 * @param {number} minSize
 * @param {number} maxSize
 * @return {number}
 */
var maxFreq = function(s, maxLetters, minSize, maxSize) {
  function check(ss) {
      const sss = new Set(ss.split(''))
      if (sss.size <= maxLetters) {
          return true
      }
      return false
  }
  function count(ss) {
      let cnt = 0
      let temps = s
      let index = temps.indexOf(ss)
      while(index !== -1) {
          cnt++
          temps = temps.slice(index + 1)
          index = temps.indexOf(ss)
      }
      return cnt
  }
  let ans = 0
  const arr = []
  for(let i = 0; i < s.length - minSize + 1; i++) {
      let ss = ''
      for(let j = 0; j < minSize; j++) {
          ss += s[i + j]
      }
      let x = minSize + i
      while(check(ss) && ss.length <= maxSize) {
          // console.log(ss)
          arr.push(ss)
          if (x >= s.length) {
              break
          }
          ss += s[x++]
      }
  }
  arr.sort()
  const map = {}
  for(let i = 0; i < arr.length ;i++) {
    if (map[arr[i]]) map[arr[i]] ++
    else map[arr[i]]  = 1
  }
  const unq = [...new Set(arr)]
  for(let i = 0; i < unq.length; i++) { 
    Years = Math.max (year map [UNQ [i]]) 
  } 
  // console.log (years) 
  return years 
};

 

 

 

 

2. Fractional numbers set successive group

Nums give you an array of integers and a positive integer k, determine whether you can put some of this array is divided into a collection of k consecutive digits.
If you can, please return True; otherwise, it returns False.

Example 1:

Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
explanation: the array can be divided into [1,2,3,4] and [3,4,5, 6].
Example 2:

Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
explanation: the array can be divided into [2,3], [ 2,3,4], [3,4,5] and [9,10,11].
Example 3:

Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:

Input: nums = [1,2,3,4], k = 3
Output: false
interpretation: the size of the array is not divided into several sub-array 3.
 

prompt:

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var isPossibleDivide = function(nums, k) {
    let judge =(nums, k)=>{
       if(nums.length % k ===0 && nums.length!==0){
           let min = Math.min(...nums);
           let k1 = k;
           while(k1--){
               let index = nums.indexOf(min);
               if(index!==-1){
                   // splice(index, replace nums, replace params)
                   nums.splice(index, 1)
               } else {
                   return;
               }
               min++;
           }
           judge(nums, k)
       } else {
           return
       }     
    };
      
    judge(nums, k);
    
    return !nums.length;
};

 

 

3. The maximum number of candy from the box you get the

You n boxes, each box format [status, candies, keys, containedBoxes], wherein:

Status word status [i]: integer, if the box [i] is ON, then 1 is 0 otherwise.
Number confectionery candies [i]: integer representing the number box [i] in the candy.
Key keys [i]: array, that you open the box [i], the keys can get some boxes, each box element respectively for the index key.
Containing box containedBoxes [i]: integer, indicates on the box [i] in the box corresponding to the index.
Give you a initialBoxes array represents the box you get now, you can get inside the candy, you can also open a new box with a key to the box, you can continue to explore other boxes found from this box.

Please follow the above rules, the maximum number of returns can get candy.

 

Example 1:

Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[], [], [1], []], containedBoxes = [[1,2], [3], [], [ ]], initialBoxes = [0]
output: 16
explanation:
first you have a box 0. You will get it inside of seven candy and boxes 1 and 2.
Box 1 status is currently closed, but you do not have the corresponding key to it. So you'll open the box 2, and get the key inside four candy and boxes 1.
In box 1, you will get 5 boxes of candy and 3, but you can not get the key to the box so the box 3 of 3 will remain closed.
The number of candy you can get a total of = 7 + 4 + 5 = 16.
Example 2:

Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5], [ ], [], [], [], []], containedBoxes = [[1,2,3,4,5], [], [], [], [], []], initialBoxes = [0 ]
output: 6
explanation:
you have a box to start 0. Open the box which you can find four, five and their corresponding keys.
Open the box, you will get all the candy box, so the total number of candies six.
Example 3:

Input: status = [1,1,1], candies = [100,1,100], keys = [[], [0,2], []], containedBoxes = [[], [], []], initialBoxes = [1]
output: 1
example 4:

Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = []
Output: 0
Example 5:

Input: status = [1,1,1], candies = [2,3,2], keys = [[], [], []], containedBoxes = [[], [], []], initialBoxes = [2,1,0]
output: 7
 

prompt:

1 <= status.length <= 1000
status.length keys.length == == == candies.length containedBoxes.length == n-
Status [I] is either 0 or 1.
. 1 <= Candies [i] <1000 =
0 <= Keys [i] .length <status.length =
0 <= Keys [i] [J] <status.length
Keys value [i] are different from each other in of.
0 <= containedBoxes [i] .length <status.length =
0 <= containedBoxes [i] [J] <status.length
containedBoxes value [i] are different from each other in.
Each box contained a maximum of a box.
0 <= initialBoxes.length <status.length =
0 <= initialBoxes [I] <status.length

/ * * 
 * @Param {Number []} Status 
 * @param {Number []} Candies 
 * @param {Number [] []} Keys 
 * @param {Number [] []} containedBoxes 
 * @param {Number [] initialBoxes} 
 * {@return Number} 
 * / 
// in status sublime somehow become keywords, the default type is a string, not an array 
var maxCandies = function (Status, Candies, Keys, containedBoxes, initialBoxes) { 
    the let boxs = []; 
    the let mykeys = []; 
   
    initialBoxes.forEach (B => { 
        boxs.push (B); 
        
       the let getboxs = (cboxes) => { 
            cboxes.length&& cboxes.forEach(b1=>{
            boxs.push(b1);
            getboxs(containedBoxes[b1])
        })
      }
      getboxs(containedBoxes[b])
        
    });
   
    boxs.forEach(b=>{
        mykeys.push(...keys[b])
    })
    let res = candies.reduce((init, val, i)=>{
        if(boxs.indexOf(i)!==-1 && (Number(status[i]) || (!status[i] && mykeys.indexOf(i) !==-1))){
          return init + val;
        } else {
            return init; 
        }
    }, 0)
    return res
};

Replay:

  2 is a moderate problem I wrote to spend 20 minutes, the reason why it takes so long, another reason is the splice of api due time, unused and forgotten, wondering a little while, if you do not have this problem, should be able to improve one minute of it.

3 is a difficult question I wrote, but only to submit in 12:08 minutes, overtime, before two particularly successful this time, I do not know rest well, good status or ability to enhance light with 2 title before reached more than 300 If the regular filing can reach more than 200.
This question itself is not difficult, long-winded narrative subject, suggesting that too much, and feel there are problems, and in the sublime debugging, there is a problem, wondering for some time, that status seems to be a key, I wrote an array, or to be prompted string, and inside 
the code also runs a string, it is embarrassing.

 Finally, there

To do so successfully is not recommended at the time, it is easy to think of something directly to do just fine down, look at the time but will have slack psychology. Anyway, to aim to do four questions.

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/maximum-candies-you-can-get-from-boxes
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

Guess you like

Origin www.cnblogs.com/zhangzs000/p/12079465.html