算法题记录【华为od】最多提取字串数目

题目描述

思路分析

        我的想法是 先求出所有可能的连续数组和,记录当前数组和所对应的数组首尾位置,

使用map进行记录,之后将每个数组和对应的数组首尾位置进行比较,从而求出满足题目要求的最大值

代码解析

let input1 = [-1, 0, 4, -3, 6, 5, -6, 5, -7, -3], input2 = "bd", res = [], count = 0;
let m = new Map(), arr = [], cache = [], value = [], max = 0, maxValue = 0;
for (let i = 0; i < input1.length; i++) {
  let sum = input1[i];
  for (let j = i; j < input1.length; j++) {
    if (i != j) {
      sum += input1[j];
    }
    const c = sum;
    m.set(c, m.has(c) ? m.get(c) + 1 : 1);
    if (cache.indexOf(sum) != -1) {
      arr.map(item => {
        if (item[0] == sum) value = item[1]
      })
      value.push([i, j])
    } else {
      value = []
      value.push([i, j])
      arr.push([sum, value])
      cache.push(sum)
    }
  }
}
for (let i = 0; i < arr.length; i++) {
  let cc = arr[i][1]
  console.log('arr[i]=>', cc);
  if (cc.length > max) {
    var list = Array(cc.length).fill(-1)
    func(cc, 0)
    max = Math.max(max, maxValue);
    maxValue = 0;
    console.log('list=>', list);
  }
}
function func (item, index) {
  if (list[index] != -1) return list[index];
  let count = 0, max = 0;
  for (let i = index; i < item.length; i++) {
    let start = item[index][1];
    count++;
    if (start < item[i][0]) {
      for (let j = i; j < item.length; j++) {
        if (start < item[j][0]) {
          start = item[j][1];
          count++
        }
      }
    }
    max = Math.max(count, max)
    count = 0
  }
  console.log('max=>', max);
  let nextIndex = index + 1;
  while (nextIndex < item.length) {
    let rres = func(item, nextIndex)
    if (item[index][1] < item[nextIndex][0]) {
      max = Math.max(max, rres + 1);
    }
    nextIndex++;
  }
  list[index] = max
  maxValue = Math.max(max, maxValue);
  return max;
}
console.log('max=>', max);

后话

只是记录,正在找工作中,各位老板缺前端的可以私信,会vue,react,ts,js,22届本科毕业,一年工作经验

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值