ARTS Week 46

Algorithm 本周的算法题为 1156. 单字符重复子串的最大长度 如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。 给你一个字符串 text,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。 示例 1: 输入:text = "ababa" 输出:3 实现代码如下: const maxRepOpt1 = function (text) { const count = {}; for (const char of text) { if (!count[char]) { count[char] = 0; } count[char]++; } let max_length = 0; const n = text.length; for (let i = 0; i < n; i++) { const char = text[i]; let length = 1; let j = i + 1; while (j < n && text[j] === char) { length++; j++; } if (length < count[char]) { if (j < n && text[j] !== char && j + 1 < n && text[j + 1] === char) { let k = j + 1; while (k < n && text[k] === char) { length++; k++; } if (length < count[char]) { length++; } } else { length++; } } max_length = Math.max(max_length, length); } return max_length; } 解题思路: 使用一个对象 count 来统计每个字符的出现次数。遍历字符串,将每个字符的出现次数记录在 count 中。 使用一个外层循环,从每个字符开始,向右扩展,找到当前字符的最长连续重复子串。 如果当前子串长度小于该字符的总出现次数,尝试检查子串两端是否有相同字符,或者中间是否有不同字符可以交换。 每次找到一个更长的子串时,更新 max_length。 Review Lunar New Year sees record migration in China - Breaking News English Lesson ...

2025-02-07 · 1 分钟 · 202 字

ARTS Week 45

Algorithm 本周的算法题为 1475. 商品折扣后的最终价格 给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。 商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。 请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。 示例 1: 输入:prices = [8,4,6,2,3] 输出:[4,2,4,2,3] 解释: 商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。 商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。 商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。 商品 3 和 4 都没有折扣。 实现代码如下: const finalPrices = function (prices) { let priceDifferences = [] prices.forEach((currentPrice, currentIndex) => { let lowerPriceIndexes = [] let priceDifference = 0 prices.forEach((comparePrice, compareIndex) => { if (compareIndex > currentIndex && comparePrice <= currentPrice) { lowerPriceIndexes.push(compareIndex); } }) if (lowerPriceIndexes.length === 0) { priceDifference = currentPrice } else { let minIndex = Math.min(...lowerPriceIndexes) priceDifference = currentPrice - prices[minIndex] } priceDifferences.push(priceDifference) }) return priceDifferences } 解题思路: 先遍历查询满足条件的下标,然后获取最小下标。 如果没有符合条件的折扣,则使用当前价格。 Review Mystery drones in USA causing alarm - Breaking News English Lesson ...

2025-01-01 · 1 分钟 · 175 字

ARTS Week 44

Algorithm 本周的算法题为 557. 反转字符串中的单词 III 给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例 1: 输入:s = "Let's take LeetCode contest" 输出:"s'teL ekat edoCteeL tsetnoc" 实现代码如下: const reverseWords = function (s) { let result = ''; s.match(/\S+/g).forEach(word => { result += word.split('').reverse().join('') + ' '; }); return result.trimEnd(); } 解题思路: String.prototype.match() 方法检索字符串与正则表达式进行匹配的结果。比如以上示例,s.match(/\S+/g)的结果为 [ "Let's", 'take', 'LeetCode', 'contest' ]。 Review Record number of people in space - Breaking News English Lesson 太空变得越来越拥挤了。目前,太空中创纪录地有19人。这一记录是在俄罗斯联盟号宇宙飞船的三名乘员与国际空间站(ISS)对接后打破的。最近的任务包括NASA宇航员Don Pettit和俄罗斯宇航员Alexey Ovchinin以及Ivan Vagner。他们将国际空间站的居民人数增加到了12人。中国的宇航员也是这一记录的一部分。他们在天宫空间站有三名“太空人”在工作。剩下的四名太空旅行者是SpaceX的极光黎明任务的一部分。该项目在上周四见证了历史上第一次全平民太空行走。 关于最近的记录有一点争议,那就是关于外太空起点的定义。NASA和美国军方将地球大气层和外太空的边界视为海拔80公里。按照这种解释,人类在太空中的记录是20人,在2023年5月和今年1月创下。然而,外太空边缘的传统定义被称为卡门线。国际航空联合会将这条线定为海拔100公里。新的记录使用了这个定义。卡门线用于法律目的,以区分飞机和宇宙飞船。 Tip 在给项目配置Sentry时,要将sourcemaps通过sentry-wizard自动上传至Sentry,但是却提示以下报错信息: Loading Wizard failed. Did you provide the right URL? 再三确认URL是没有问题,所以,就很奇怪这是什么问题? 使用curl命令是没有问题的,但是,返回了Sentry网站没有证书的信息。 查看Github上面的Issues,发现有类似的问题,如下所示: Loading Wizard failed for NextJs #416 提供的解决方案是,使用命令 set NODE_TLS_REJECT_UNAUTHORIZED=0 ,因为在Node.js 中,NODE_TLS_REJECT_UNAUTHORIZED 是一个环境变量,用于控制Node.js在执行HTTPS请求时是否应该拒绝未经授权的SSL证书。 ...

2024-10-09 · 1 分钟 · 125 字

ARTS Week 43

Algorithm 本周的算法题为 1822. 数组元素积的符号 已知函数 signFunc(x) 将会根据 x 的正负返回特定值: 如果 x 是正数,返回 1 。 如果 x 是负数,返回 -1 。 如果 x 是等于 0 ,返回 0 。 给你一个整数数组 nums 。令 product 为数组 nums 中所有元素值的乘积。 返回 signFunc(product) 。 示例 1: 输入:nums = [-1,-2,-3,-4,3,2,1] 输出:1 解释:数组中所有值的乘积是 144 ,且 signFunc(144) = 1 实现代码如下: let arraySign = function (nums) { let product = 1 nums.forEach(n => { if (n === 0) { product = 0 return } product = product * n }); return signFunc(product) function signFunc(p) { if (p > 0) { return 1 } else if (p < 0) { return -1 } else { return 0 } } } 解题思路: 题目很简单。 Review Nature can reduce anxiety and boost mental health - Breaking News English Lesson ...

2024-10-08 · 2 分钟 · 240 字

ARTS Week 42

Algorithm 本周的算法题为 2283. 判断一个数的数字计数是否等于数位的值 给你一个下标从 0 开始长度为 n 的字符串 num ,它只包含数字。 如果对于 每个 0 <= i < n 的下标 i ,都满足数位 i 在 num 中出现了 num[i]次,那么请你返回 true ,否则返回 false 。 示例 1: 解释: num[0] = '1' 。数字 0 在 num 中出现了一次。 num[1] = '2' 。数字 1 在 num 中出现了两次。 num[2] = '1' 。数字 2 在 num 中出现了一次。 num[3] = '0' 。数字 3 在 num 中出现了零次。 "1210" 满足题目要求条件,所以返回 true 。 实现代码如下: const digitCount = function (num) { for (let i = 0; i < num.length; i++) { const n = num[i]; console.log({ i, n }); if (Number(n) !== getCount(i, num)) { return false } } return true function getCount(char, s) { let count = 0 let postion = s.indexOf(char) while (postion !==-1) { count++ postion = s.indexOf(char, postion + 1) } return count } }; 解题思路: 只需求得下标字符i在字符串中出现的次数与num[i]相等即可。 Review Marmoset monkeys call each other by name - Breaking News English Lesson ...

2024-10-02 · 1 分钟 · 188 字