add: 二叉树的最近公共祖先
This commit is contained in:
25
src/array/maximal-square.js
Normal file
25
src/array/maximal-square.js
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* @param {character[][]} matrix
|
||||
* @return {number}
|
||||
*/
|
||||
export const maximalSquare = function (matrix) {
|
||||
if (!matrix || !matrix[0]) return 0
|
||||
|
||||
const rows = matrix.length
|
||||
const cols = matrix[0].length
|
||||
let max = 0
|
||||
|
||||
const dp = new Array(rows).fill().map(_ => new Array(cols).fill(0))
|
||||
|
||||
for (let n = 0; n < rows; n++) {
|
||||
for (let i = 0; i < cols; i++) {
|
||||
if (Number(matrix[n][i]) === 1) {
|
||||
if (n === 0 || i === 0) dp[n][i] = 1
|
||||
else dp[n][i] = Math.min(dp[n - 1][i], dp[n][i - 1], dp[n - 1][i - 1]) + 1 // 找规律
|
||||
max = Math.max(max, dp[n][i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return max * max
|
||||
}
|
43
src/array/minimum-cost-for-tickets.js
Normal file
43
src/array/minimum-cost-for-tickets.js
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @param {number[]} days
|
||||
* @param {number[]} costs
|
||||
* @return {number}
|
||||
*/
|
||||
export const mincostTickets = function (days, costs) {
|
||||
// 动态规划:
|
||||
// dp[i]: 从第i天开始,到最后一天所用的票价总和
|
||||
|
||||
// 思路:
|
||||
// 1. 记录数组中的最早的一天和最后的一天,动态规划从最后一天向前到最早一天即可,因为
|
||||
// 其他时间不需要消耗通行证
|
||||
// 2. 用一个变量 k 指针从 days 的最后一个索引向前走,i 指针也从最后一天向前走,但是遇到了
|
||||
// 不需要花费通行证的某些天走 else 分支即可,即后一天的总花费就是这一天的总花费
|
||||
// 3. 如果遇到需要通行证的时候,分别对比买一天、七天、三十天、所需的总花费,选择总花费最少的策略即可
|
||||
// 4. 初始化的时候 dp 数组多了 30,是为了简化有的用例第 365 天需要出行,选择买 30 天
|
||||
// 通行证的时候的特判
|
||||
|
||||
// 作者:ignore_express
|
||||
// 链接:https://leetcode-cn.com/problems/minimum-cost-for-tickets/solution/js-dong-tai-gui-hua-si-lu-jiang-jie-by-ignore_expr/
|
||||
// 来源:力扣(LeetCode)
|
||||
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
||||
const dp = new Array(366 + 30).fill(0)
|
||||
const n = days.length
|
||||
const maxDay = days[n - 1]
|
||||
const minDay = days[0]
|
||||
let k = n - 1
|
||||
|
||||
for (let i = maxDay; i >= minDay; i--) {
|
||||
if (i === days[k]) {
|
||||
dp[i] = Math.min(
|
||||
dp[i + 1] + costs[0],
|
||||
dp[i + 7] + costs[1],
|
||||
dp[i + 30] + costs[2]
|
||||
)
|
||||
k--
|
||||
} else {
|
||||
dp[i] = dp[i + 1]
|
||||
}
|
||||
}
|
||||
|
||||
return dp[minDay]
|
||||
}
|
28
src/list/linked-list-cycle.js
Normal file
28
src/list/linked-list-cycle.js
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* function ListNode(val) {
|
||||
* this.val = val;
|
||||
* this.next = null;
|
||||
* }
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {ListNode} head
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const hasCycle = function (head) {
|
||||
if (!head || !head.next) return false
|
||||
|
||||
// 双指针解法
|
||||
let slow = head
|
||||
let fast = head.next
|
||||
|
||||
while (true) {
|
||||
if (!fast || !fast.next) return false
|
||||
else if (fast.next === slow || fast === slow) return true
|
||||
else {
|
||||
fast = fast.next.next
|
||||
slow = slow.next
|
||||
}
|
||||
}
|
||||
}
|
8
src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js
Normal file
8
src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @param {string} s
|
||||
* @param {number} n
|
||||
* @return {string}
|
||||
*/
|
||||
export const reverseLeftWords = function (s, n) {
|
||||
return (s + s).substr(n, s.length)
|
||||
}
|
27
src/tree/subtree-of-another-tree.js
Normal file
27
src/tree/subtree-of-another-tree.js
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* function TreeNode(val, left, right) {
|
||||
* this.val = (val===undefined ? 0 : val)
|
||||
* this.left = (left===undefined ? null : left)
|
||||
* this.right = (right===undefined ? null : right)
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* @param {TreeNode} s
|
||||
* @param {TreeNode} t
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isSubtree = function (s, t) {
|
||||
if (!s && t) {
|
||||
return false
|
||||
}
|
||||
|
||||
const linkNode = function (node, target) {
|
||||
if ((!node && target) || (node && !target)) return false
|
||||
if (!node && !target) return true
|
||||
|
||||
return node.val === target.val ? linkNode(node.left, target.left) && linkNode(node.right, target.right) : false
|
||||
}
|
||||
|
||||
return linkNode(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t)
|
||||
}
|
Reference in New Issue
Block a user