---
description: 最重要到就是找出歸則
---

# # maxOfBiggestVal

```javascript
// numbers = ［5,2,5,3,1]
// q = [1,5,3,4,5]
// return [2,1,1,1,1]

//1 [5,2,5,3,1]  return 2 因為 5 出現兩次
//5 [1] 1
//3 [5,3,1] 1
//4 [3,1] 1
//5 [1] 1
function maxOfBiggestVal (numbers, q) {}
```

### 怎麼解

先不要管 q，先從 numbers 開始找規則，發現從後面算的話，算到前面可以繼續利用．一開始一定是 return 1 因為最後一個只有有一個值，然後創一個 lookup 把寄過得值存起來

```javascript
function maxOfBiggestVal (numbers, q) {
  const len = numbers.length
  let maxNum = numbers[len-1]
  let lookup = new Map()
  let result= new Array(len)
   // [2,1,1,1,1]
  
  for(let i=len-1; i>=0; i--){
      if(!lookup.has(numbers[i])){
        lookup.set(numbers[i], 1)
      } else {
        lookup.set(numbers[i], lookup.get(numbers[i]) + 1)
      }
      maxNum = Math.max(numbers[i], maxNum)
      result[i] = lookup.get(maxNum)
  }
  

  return  q.map((query) => result[query-1] )
}
```

8/13

```javascript
function getBiggestValueCount(nums){
  if(nums.length == 0 ) return null
  if(nums.length < 2) return 1
  
  let map = new Map()
  let maxVal = nums[0]
  
  for(let num of nums){
    map.set(num, (map.get(num) || 0) + 1 )
    maxVal = Math.max(maxVal, num)
  }

  return map.get(maxVal)
}

function maxOfBiggestVal (numbers, q) {
  return q.map(query => 
    getBiggestValueCount(numbers.slice(query - 1))
  )
}
```
