Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

腾讯算法题 #124

Open
sisterAn opened this issue Nov 3, 2020 · 11 comments
Open

腾讯算法题 #124

sisterAn opened this issue Nov 3, 2020 · 11 comments

Comments

@sisterAn
Copy link
Owner

sisterAn commented Nov 3, 2020

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

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

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

@sisterAn sisterAn added the 腾讯 label Nov 3, 2020
@syc666
Copy link

syc666 commented Nov 4, 2020

问题一

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
}

@sisterAn
Copy link
Owner Author

sisterAn commented Nov 4, 2020

解答问题一:

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
}

@teal-front
Copy link

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

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
}

@wenfeihuazha
Copy link

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
};

@mingju0421
Copy link

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(数组下标对应的非连续最大升序)))
}

@ohion
Copy link

ohion commented Nov 10, 2020

  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
            }
        }
    }
}

@ohion
Copy link

ohion commented Nov 10, 2020

  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
        }
    }
}

@xllpiupiu
Copy link

解答问题一:

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

@xllpiupiu
Copy link

问题一

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]好像不正确呢

@xllpiupiu
Copy link

/**
 * 连续不连续最大升序数量
 * 连续可以使用双指针  
 * 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))

@AlexZhang11
Copy link

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
Projects
None yet
Development

No branches or pull requests

8 participants