李恒道 发表于 2025-1-30 16:32:23

https://leetcode.cn/problems/sfvd7V/
相同题
```js
/**
* @param {string[]} strs
* @Return {string[][]}
*/
var groupAnagrams = function (strs) {
const obj = strs.reduce((obj, value) => {
    const name = value.split("").sort().join("");
    (obj = obj ?? []), obj.push(value);
    return obj;
}, {});
const arr = [];
for (const key in obj) {
    arr.push(obj);
}
return arr;
};
```

李恒道 发表于 2025-1-30 19:55:02

https://leetcode.cn/problems/not-boring-movies/submissions/595969135/?envType=study-plan-v2&envId=sql-free-50
秒了
```go
# Write your MySQL query statement below
SELECT cinema.* FROM cinema WHERE   id%2!=0&&description!="boring" ORDER BY rating DESC
```

李恒道 发表于 2025-1-30 20:26:01

https://leetcode.cn/problems/ugly-number-ii/submissions/595972540/
最小堆,这题没想出来,真妙
```js
/**
* @param {number} n
* @Return {number}
*/
class Heap {
constructor(comparator = (a, b) => a - b) {
    this.heap = [];
    this.comparator = comparator;
}

getParentIndex(index) {
    return Math.floor((index - 1) / 2);
}

getLeftChildIndex(index) {
    return 2 * index + 1;
}

getRightChildIndex(index) {
    return 2 * index + 2;
}

swap(index1, index2) {
    const temp = this.heap;
    this.heap = this.heap;
    this.heap = temp;
}

insert(value) {
    this.heap.push(value);
    this.heapifyUp();
}

heapifyUp() {
    let index = this.heap.length - 1;
    while (index > 0) {
      const parentIndex = this.getParentIndex(index);
      if (this.comparator(this.heap, this.heap) < 0) {
      this.swap(index, parentIndex);
      index = parentIndex;
      } else {
      break;
      }
    }
}

extractMin() {
    if (this.heap.length === 0) {
      return null;
    }
    if (this.heap.length === 1) {
      return this.heap.pop();
    }
    const min = this.heap;
    this.heap = this.heap.pop();
    this.heapifyDown(0);
    return min;
}

heapifyDown(index) {
    let smallest = index;
    const leftChildIndex = this.getLeftChildIndex(index);
    const rightChildIndex = this.getRightChildIndex(index);

    if (
      leftChildIndex < this.heap.length &&
      this.comparator(this.heap, this.heap) < 0
    ) {
      smallest = leftChildIndex;
    }

    if (
      rightChildIndex < this.heap.length &&
      this.comparator(this.heap, this.heap) < 0
    ) {
      smallest = rightChildIndex;
    }

    if (smallest !== index) {
      this.swap(index, smallest);
      this.heapifyDown(smallest);
    }
}

peek() {
    return this.heap.length > 0 ? this.heap : null;
}

size() {
    return this.heap.length;
}
}
var nthUglyNumber = function (n) {
const minHeap = new Heap((a, b) => a - b);
minHeap.insert(1);
const visit = new Set();
let ugly = 0;
let list = ;
for (let index = 0; index < n; index++) {
    ugly = minHeap.extractMin();
    for (const num of list) {
      const mul = num * ugly;
      if (!visit.has(mul)) {
      visit.add(mul);
      minHeap.insert(mul);
      }
    }
}
return ugly;
};


```

李恒道 发表于 2025-1-30 20:29:49

https://leetcode.cn/problems/chou-shu-lcof/submissions/595973028/
lru,一个题
```js
/**
* @param {number} n
* @Return {number}
*/
class Heap {
constructor(comparator = (a, b) => a - b) {
    this.heap = [];
    this.comparator = comparator;
}

getParentIndex(index) {
    return Math.floor((index - 1) / 2);
}

getLeftChildIndex(index) {
    return 2 * index + 1;
}

getRightChildIndex(index) {
    return 2 * index + 2;
}

swap(index1, index2) {
    const temp = this.heap;
    this.heap = this.heap;
    this.heap = temp;
}

insert(value) {
    this.heap.push(value);
    this.heapifyUp();
}

heapifyUp() {
    let index = this.heap.length - 1;
    while (index > 0) {
      const parentIndex = this.getParentIndex(index);
      if (this.comparator(this.heap, this.heap) < 0) {
      this.swap(index, parentIndex);
      index = parentIndex;
      } else {
      break;
      }
    }
}

extractMin() {
    if (this.heap.length === 0) {
      return null;
    }
    if (this.heap.length === 1) {
      return this.heap.pop();
    }
    const min = this.heap;
    this.heap = this.heap.pop();
    this.heapifyDown(0);
    return min;
}

heapifyDown(index) {
    let smallest = index;
    const leftChildIndex = this.getLeftChildIndex(index);
    const rightChildIndex = this.getRightChildIndex(index);

    if (
      leftChildIndex < this.heap.length &&
      this.comparator(this.heap, this.heap) < 0
    ) {
      smallest = leftChildIndex;
    }

    if (
      rightChildIndex < this.heap.length &&
      this.comparator(this.heap, this.heap) < 0
    ) {
      smallest = rightChildIndex;
    }

    if (smallest !== index) {
      this.swap(index, smallest);
      this.heapifyDown(smallest);
    }
}

peek() {
    return this.heap.length > 0 ? this.heap : null;
}

size() {
    return this.heap.length;
}
}
var nthUglyNumber = function (n) {
const minHeap = new Heap((a, b) => a - b);
minHeap.insert(1);
const visit = new Set();
let ugly = 0;
let list = ;
for (let index = 0; index < n; index++) {
    ugly = minHeap.extractMin();
    for (const num of list) {
      const mul = num * ugly;
      if (!visit.has(mul)) {
      visit.add(mul);
      minHeap.insert(mul);
      }
    }
}
return ugly;
};


```

李恒道 发表于 2025-1-31 00:24:16

https://leetcode.cn/problems/reverse-string-ii/submissions/596001041/
过年题简单
```js
var reverseStr = function (s, k) {
s = s.split("");
let base = 0;
let ans = "";
while (base < s.length) {
    ans += s.slice(base, base + k).reverse().join("");
    ans += s.slice( base + k, base + k+k).join("")
    base+=k
    base+=k
}
return ans
};
```

李恒道 发表于 2025-1-31 00:25:47

https://leetcode.cn/problems/reverse-string/submissions/596001145/
过了
```js
var reverseString = function(s) {
    for (let index = 0; index < s.length/2; index++) {
   const temp=s;
   s=s;
   s=temp
    }
};
```

李恒道 发表于 2025-1-31 00:43:46

https://leetcode.cn/problems/6eUYwP/submissions/596002336/
秒了
```js
var pathSum = function (root, targetSum) {
const map = new Map();
map.set(0,1)
let ret = 0;
let presum = 0;
const dfs = (root) => {
    if(root==null){
      return
    }
    const haveNum =presum- (targetSum - root.val);
    if (map.has(haveNum)) {
      ret += map.get(haveNum);
    }
    presum += root.val;
    if (map.has(presum)) {
      map.set(presum, map.get(presum) + 1);
    } else {
      map.set(presum, 1);
    }
    dfs(root.left);
    dfs(root.right);
    map.set(presum, map.get(presum) - 1);
    presum -= root.val;
};
dfs(root);
return ret;
};
```

李恒道 发表于 2025-1-31 16:04:18

https://leetcode.cn/problems/average-selling-price/submissions/596066892/?envType=study-plan-v2&envId=sql-free-50
sql题
```js
# Write your MySQL query statement below
SELECT Prices.product_id,ROUND(COALESCE(SUM(UnitsSold.units*Prices.price )/SUM(UnitsSold.units ),0),2) as average_price    FROM UnitsSold RIGHT JOIN Prices ON (UnitsSold.purchase_date BETWEEN Prices.start_date   AND Prices.end_date) AND   UnitsSold.product_id    =Prices.product_id    GROUP BY Prices.product_id
```

李恒道 发表于 2025-2-1 00:41:35

https://leetcode.cn/problems/grumpy-bookstore-owner/submissions/596135641/

```js
var maxSatisfied = function (customers, grumpy, minutes) {
    let all = 0;
    for (let index = 0; index < customers.length; index++) {
      const customer = customers;
      if (grumpy == 0) {
      all += customer;
      }
    }
    let maxCustomer = 0;
    let sepCustomer = 0;
    for (let index = 0; index < customers.length; index++) {
      const customer = customers;
      if (grumpy == 1) {
      sepCustomer += customer;
      }
      if (index >= minutes&&grumpy == 1) {
      sepCustomer -= customers;
      }
      maxCustomer=Math.max(maxCustomer,sepCustomer)
    }
    return all+maxCustomer
};
```

李恒道 发表于 2025-2-1 00:51:16

https://leetcode.cn/problems/check-if-a-string-contains-all-binary-codes-of-size-k/submissions/596136286/

```js
var hasAllCodes = function (s, k) {
let str = "";
let set = new Set();
for (let index = 0; index < s.length; index++) {
    const char = s;
    str += char;
    if (index >= k) {
      str = str.slice(1);
    }
    if(str.length==k){
      set.add(str);
    }
}
return set.size == Math.pow(2, k);
};
```
页: 38 39 40 41 42 43 44 45 46 47 [48] 49 50 51 52 53 54
查看完整版本: 【当前排名42710】挑战leetcode进入前1w名