Skip to content

腾讯算法题 #124

Open
Open

Description

@sisterAn
const arr = [101,19,12,51,32,7,103,8];

问题一: 找出连续最大升序的数量

问题二: 找出不连续最大升序的数量

Activity

  1. syc666 commented on Nov 4, 2020

    @syc666

    问题一

    function queryFn(arr) {
        if(!arr.length) return 0
        let res = 1
        let start = 0
        for (let i = 1, len = arr.length; i < len; i++){
            if (arr[i] < arr[i - 1]) {
                res = Math.max(res,i-start)
                start = i
            }
        }
        return res
    }

    问题二

    function queryFn(arr) {
        if(!arr.length) return 0
        let res = 1
        let num = 0
        let lastVal = 0
        for (let i = 0, len = arr.length-1; i < len; i++){
            for (let j = i + 1; j < len; j++){
                if (arr[j] > lastVal) {
                    num++
                    lastVal = arr[j]
                }
            }
            res = Math.max(res,num)
        }
        return res
    }
  2. sisterAn commented on Nov 4, 2020

    @sisterAn
    OwnerAuthor

    解答问题一:

    const findLengthOfLCIS = (nums) => {
        if(nums.length <= 1) return nums.length
        let max = 1, count = 1
        for(let i = 1; i < nums.length; i++){
            if(nums[i] > nums[i-1]) {
                count += 1
            } else {
                count = 1
            }
            max = Math.max(max, count)
        }
        return max
    }

    leetcode

    解答问题二:

    var maxLength = function(nums) {
      if(nums.length <= 1) return nums.length
      let count = 1, max = 1, start = 0
      for(let i = 0; i < nums.length-1; i++) {
        start = nums[i]
        for(let j=i+1; j < nums.length; j++) {
          if(nums[j]>start) {
            count ++
            start = nums[j]
          }
        }
        max = Math.max(max, count)
        count = 1
      }
      return max
    }
  3. teal-front commented on Nov 5, 2020

    @teal-front

    问题一: 找出连续最大升序的数量

    var findLengthOfLCIS = function(nums) {
      if (nums.length <= 1) return nums.length
      let i = 0, j = 1
      let max = 1
      while (j < nums.length) {
        if (nums[j] > nums[j - 1]) {
          max = Math.max(max, j - i + 1)
          j++
        } else {
          i = j
          j = j + 1
        }
      } 
      return max
    }
  4. wenfeihuazha commented on Nov 6, 2020

    @wenfeihuazha
    var findLengthOfLCIS = function(nums) {
        if(nums.length <= 1) return nums.length
        let max = 1,count = 1,len = nums.length
        for(let i = 1; i < len; i++){
            if(nums[i] > nums[i-1]){
                count += 1
            }else{
                count = 1
            }
            max = Math.max(max,count)
        }
        return max
    };
    var findLengthOfLCIS = function(nums) {
      if(nums.length <= 1) return nums.length
      let max = 1,count = 1,len = nums.length
      for(let i = 1; i < len; i++){
          if(nums[i] !== (nums[i-1] + 1) && nums[i] > nums[i-1]){
              count += 1
          }else{
              count = 1
          }
          max = Math.max(max,count)
      }
      return max
    };
  5. mingju0421 commented on Nov 6, 2020

    @mingju0421
    const 连续最大升序 = arr => {
        let sum = 1, _sum = 1
        for (let i = 0; i < arr.length - 1; i++) {
            if (arr[i] < arr[i + 1]) _sum += 1
            else _sum = 1
            if (_sum > sum) sum = _sum
        }
        console.log(sum)
    }
    const 不连续最大升序 = arr => {
        // 保存每个下标对应的最大升序数量
        let 数组下标对应的非连续最大升序 = {}
        // 从最后一个算
        for (let i = arr.length - 1; i >= 0; i--) {
            let 当前下标对应的所有非连续升序集 = [1]
            for (let j = i + 1; j < arr.length - 1; j++) {
                // 如果后面的值大于当前值 将累计值存入数据
                if (arr[i] < arr[j]) 当前下标对应的所有非连续升序集.push(数组下标对应的非连续最大升序[j] + 1)
            }
            // 过滤出每个下标的最大值
            数组下标对应的非连续最大升序[i] = Math.max(...当前下标对应的所有非连续升序集)
        }
        // 过滤出对象中最大的值
        console.log(Math.max(...Object.values(数组下标对应的非连续最大升序)))
    }
  6. ohion commented on Nov 10, 2020

    @ohion
    1. 找出连续最大升序的数量
    (arr)=>{
        let index = 1
        let temp = []
        for(let i=0;i<arr.length;i++){
            temp[i] = 1
            for(let j = i ;j<arr.length;j++){
                if(arr[j+1] > arr[j]){
                    temp[i] = ++temp[i]
                    if(temp[i]>index){
                        index++
                    }
                }else{
                    break
                }
            }
        }
    }
    
  7. ohion commented on Nov 10, 2020

    @ohion
    1. 找出不连续最大升序的数量
    let num = 1
    let temp2 = []
    for(let i=1;i<arr.length;i++){
        temp2[i] = 1
        for(let j = i ;j<arr.length;j++){
            if(arr[j+1] > arr[j]){
                temp2[i] = ++temp2[i]
                if(temp2[i]>num){
                    num++
                }
            }else{
                continue
            }
        }
    }
    
  8. xllpiupiu commented on Oct 5, 2021

    @xllpiupiu

    解答问题一:

    const findLengthOfLCIS = (nums) => {
        if(nums.length <= 1) return nums.length
        let max = 1, count = 1
        for(let i = 1; i < nums.length; i++){
            if(nums[i] > nums[i-1]) {
                count += 1
            } else {
                count = 1
            }
            max = Math.max(max, count)
        }
        return max
    }

    leetcode

    解答问题二:@syc666

    测试数据let arr = [101,19,12,34,51,32,7,103,8,104,1,105,2,106]
    结果是6 实际结果应该是7吧 12 34 51 103 104 105 106

  9. xllpiupiu commented on Oct 5, 2021

    @xllpiupiu

    问题一

    function queryFn(arr) {
        if(!arr.length) return 0
        let res = 1
        let start = 0
        for (let i = 1, len = arr.length; i < len; i++){
            if (arr[i] < arr[i - 1]) {
                res = Math.max(res,i-start)
                start = i
            }
        }
        return res
    }

    问题二

    function queryFn(arr) {
        if(!arr.length) return 0
        let res = 1
        let num = 0
        let lastVal = 0
        for (let i = 0, len = arr.length-1; i < len; i++){
            for (let j = i + 1; j < len; j++){
                if (arr[j] > lastVal) {
                    num++
                    lastVal = arr[j]
                }
            }
            res = Math.max(res,num)
        }
        return res
    }

    问题1 如果arr=[1,2,3]好像不正确呢

  10. xllpiupiu commented on Oct 5, 2021

    @xllpiupiu
    /**
     * 连续不连续最大升序数量
     * 连续可以使用双指针  
     * let arr = [101,19,12,34,51,32,7,103,8]
     */
    //1. 连续升序最大数量
    function findMaxAesc(arr) {
        if (arr.length === 0) return 0
        let res = 1
        let slow = 0
        for (let fast = 1, len = arr.length; fast < len; fast++) {
            if (arr[fast] < arr[fast - 1]) {
                slow = fast
            }
            res = Math.max(res, fast - slow + 1)
        }
        return res
    }
    
    function findMaxAesc3(arr) {
        if (arr.length <= 1) return arr.length
        let max = 1
        let count = 1
        for (let i = 1; i < arr.length; i++) {
            if (arr[i] > arr[i - 1]) {
                count++
            } else {
                count = 1
            }
            max = Math.max(max, count)
        }
        return max
    }
    // let arr = [101,19,12,34,51,32,7,103,8,104,1,105,2,106]
    let arr = [2, 1, 3, 4, 5]
    console.log(findMaxAesc(arr))
    console.log(findMaxAesc3(arr))
    //不连续最大升序数量
    function findMaxAesc2(arr) {
        if (arr.length === 0) return 0
        let res = 1
        for (let i = 0, leng = arr.length; i < leng; i++) {
            let slow = arr[i]
            let num = 1
            for (let j = i + 1; j < leng; j++) {
                if (arr[j] > slow) {
                    num++
                    slow = arr[j]
                }
            }
            res = Math.max(res, num)
        }
        return res
    }
    
    
    console.log(findMaxAesc2(arr))
  11. AlexZhang11 commented on May 24, 2024

    @AlexZhang11

    function fn(nums){
    let maxcontinueUpCount=1,maxcontinueUpCountValue = 1;
    let maxBreakUpCount= 1,maxBreakUpCountValue =1
    let i = 1
    while(i<nums.length){
    if(nums[i]>nums[i-1]){
    if(maxBreakUpCountValue<maxBreakUpCount){
    maxBreakUpCountValue = maxBreakUpCount
    }
    maxBreakUpCount=1
    maxcontinueUpCount++
    }else{
    if(maxcontinueUpCountValue<maxcontinueUpCount){
    maxcontinueUpCountValue = maxcontinueUpCount
    }
    maxcontinueUpCount = 1
    maxBreakUpCount++
    }
    i++
    }
    return {
    maxBreakUpCountValue,
    maxcontinueUpCountValue
    }
    }

    console.log(fn([101,19,12,51,32,7,103,8]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions