LeetCode每日一题(2014. Longest Subsequence Repeated k Times)

YOLOv5 改进点总览:轻量化、高精度的背后有哪些魔改? YOLOv5是由Ultralytics团队开发的高效目标检测模型,其核心改进包括:主干网络采用CSPDarknet53提升梯度传播效率;特征融合使用PANet增强小目标识别;输出结构采用解耦头设计优化定位和分类任务。此外,YOLOv5通过CIoU损失函数、Mosaic数据增强和SimOTA标签分配机制进一步提升性能。模型支持多种推理格式(ONNX/TensorRT等),并具备自动锚框匹配和多尺度训练功能,使其在轻量化(yolov5s仅7MB)与高性能(最高51.2% mAP)间取得平衡,但缺乏正式论文支撑是 阅读详情

You are given a string s of length n, and an integer k. You are tasked to find the longest subsequence repeated k times in string s.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

A subsequence seq is repeated k times in the string s if seq _ k is a subsequence of s, where seq _ k represents a string constructed by concatenating seq k times.

For example, “bba” is repeated 2 times in the string “bababcba”, because the string “bbabba”, constructed by concatenating “bba” 2 times, is a subsequence of the string “bababcba”.
Return the longest subsequence repeated k times in string s. If multiple such subsequences are found, return the lexicographically largest one. If there is no such subsequence, return an empty string.

Example 1:

Input: s = “letsleetcode”, k = 2
Output: “let”

Explanation: There are two longest subsequences repeated 2 times: “let” and “ete”.
“let” is the lexicographically largest one.

Example 2:

Input: s = “bb”, k = 2
Output: “b”

Explanation: The longest subsequence repeated 2 times is “b”.

Example 3:

Input: s = “ab”, k = 2
Output: “”
Explanation: There is no subsequence repeated 2 times. Empty string is returned.

Constraints:

  • n == s.length
  • 2 <= n, k <= 2000
  • 2 <= n < k * 8
  • s consists of lowercase English letters.

限制条件中的 2 <= n < k * 8 是这题的关键, 两边同时除以 k 得到 n/k < 8, 也就是我们最终答案的长度不会超过 8。我们首先对 s 中的 char 进行 freq 计数, 只有 freq/k > 0 的 char 才可能出现在答案中。我们将这些 char 和对应的 freq 收集起来, 然后从 length = n / k 开始,以字母顺序倒序排列这些 char, 组成长度为 length 的字符串, 然后检验这些字符串是不是在 s 中出现 k 次,如果符合直接返回, 因为我们的 length 是递减的, 并且我们是按字母顺序倒序来组合的。



impl Solution {
    fn is_match(origin: &[char], target: &[char]) -> bool {
        let mut oi = 0;
        let mut ti = 0;
        while oi < origin.len() {
            if origin[oi] != target[ti] {
                oi += 1;
                continue;
            }
            if ti == target.len() - 1 {
                return true;
            }
            ti += 1;
            oi += 1;
        }
        false
    }

    fn match_permutation(counts: &mut Vec<(char, i32)>, target: &mut Vec<char>, indices: &mut Vec<usize>, k: usize, i: usize, origin: &[char]) -> Option<String> {
        if i == target.len() {
            if Solution::is_match(origin, &target.repeat(k)) {
                return Some(target.into_iter().map(|c| *c).collect());
            }
            return None;
        }
        let counts_length = counts.len();
        for _ in 0..counts_length {
            if counts[indices[i]].1 == 0 {
                indices[i] += 1;
                indices[i] %= counts_length;
                continue;
            }
            counts[indices[i]].1 -= 1;
            target[i] = counts[indices[i]].0;
            if let Some(ans) = Solution::match_permutation(counts, target, indices, k, i + 1, origin) {
                return Some(ans);
            }
            counts[indices[i]].1 += 1;
            indices[i] += 1;
            indices[i] %= counts_length;
        }
        None
    }

    pub fn longest_subsequence_repeated_k(s: String, k: i32) -> String {
        let counts = s.chars().fold(vec![0; 26], |mut l, c| {
            l[c as usize - 97] += 1;
            l
        });
        let mut max_length = 0;
        let mut counts: Vec<(char, i32)> = counts
            .into_iter()
            .enumerate()
            .filter(|&(_, n)| n / k > 0)
            .map(|(i, n)| {
                max_length += n / k;
                ((i + 97) as u8 as char, n / k)
            })
            .collect();
        counts.reverse();
        let origin: Vec<char> = s.chars().collect();
        for l in (1..=max_length).rev() {
            if let Some(ans) = Solution::match_permutation(&mut counts, &mut vec!['-'; l as usize], &mut vec![0; l as usize], k as usize, 0, &origin) {
                return ans;
            }
        }
        "".into()
    }
}
从零开始开发AI Agent:基于大模型的智能体实战指南 ### 摘要 本文介绍了AI Agent的开发全流程,从基础概念到实际部署,涵盖大模型集成、工具调用和记忆系统构建等核心技术。通过电商客服案例展示实战应用,并提供调试优化与部署方案,最后展望了AI Agent的未来发展方向,帮助开发者快速掌握智能体开发技能。 阅读详情

相关推荐

Python 爬虫实战:淘宝直播间实时数据抓取(弹幕分析 + 流量监控)

随着电商直播的迅猛发展,淘宝直播已成为品牌推广和商品销售的重要阵地。通过爬取淘宝直播间的实时数据,包括弹幕互动和流量信息,可以帮助商家深入了解用户行为、优化直播策略,同时为市场分析和商业决策提供数据支持。本文将深入探讨如何利用 Python 爬虫技术实现对淘宝直播间实时数据的抓取,并进行弹幕分析和流量监控。

u014481728的博客 687

[leetcode]2014 重复K次的最长子序列

第259场周赛补题 2014. 重复 K 次的最长子序列 给你一个长度为 n 的字符串 s ,和一个整数 k 。请你找出字符串 s 中 重复 k 次的 最长子序列 。 子序列 是由其他字符串删除某些(或不删除)字符派生而来的一个字符串。 如果 seq * k 是 s 的一个子序列,其中 seq * k 表示一个由 seq 串联 k 次构造的字符串,那么就称 seq 是字符串 s 中一个 重复 k 次 的子序列。 举个例子,"bba" 是字符串 "bababcba" 中的一个重复 2 次的子序列,因为字符串

Chris_Eddy的博客 487

【异常】解决 Cursor 报错 Unexpected seqno: 2 != 1. Please try again, or contact support if the issue 的排坑指南

在使用 Cursor 辅助编程时,你是否遇到过 AI 突然中断输出,并在右下角弹出一个红色的 Network Error 弹窗?今天我们就来彻底解析并解决这个网络断连问题。

奶爸的编程之路,也就一周冷个三天 2118

[Leetcode]2014. 重复 K 次的最长子序列

【题目描述如下】 给你一个长度为n的字符串s,和一个整数k。请你找出字符串s中重复k次的最长子序列。 子序列是由其他字符串删除某些(或不删除)字符派生而来的一个字符串。 如果seq * k是s的一个子序列,其中seq * k表示一个由seq串联k次构造的字符串,那么就称seq是字符串s中一个重复k次的子序列。 举个例子,"bba"是字符串"bababcba"中的一个重复2次的子序列,因为字符串"bbabba"是由"bba...

晚月明的博客 817

leetcode Ch2-Dynamic Programming [2014]

1. Triangle 1 class Solution { 2 public: 3 int minimumTotal(vector<vector<int> > &triangle) { 4 int n=triangle.size(); 5 int* res=new int[n]; 6 ...

dongkai0918的博客 133

leetcode Ch1-search 2014

1. Search Insert Position 1 class Solution { 2 public: 3 int searchInsert(int A[], int n, int target) { 4 int left=0,right=n-1; 5 while(left<=right) 6 ...

dongkai0918的博客 150

LeetCode 2014. Longest Subsequence Repeated k Times【BFS/字符串】困难

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。 为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.

memcpy0的博客 619

Word Search -- LeetCode

原题链接:http://oj.leetcode.com/problems/word-search/这道题很容易感觉出来是图的题目,其实本质上还是做深度优先搜索。基本思路就是从某一个元素出发,往上下左右深度搜索是否有相等于word的字符串。这里注意每次从一个元素出发时要重置访问标记(也就是说虽然单次搜索字符不能重复使用,但是每次从一个新的元素出发,字符还是重新可以用的)。深度优先搜索的算法就不再重复...

编程语言小筑 265

LeetCode每日一题(Clone Graph)

Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. class Node { public int val; public List neighbors; } Test case fo

wangjun861205的博客 7646

LeetCode每日一题(410. Split Array Largest Sum)

Given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Example 1: Input: nums = [7,2,5,10,8], m =

wangjun861205的博客 4965

LeetCode每日一题(1734. Decode XORed Permutation)

There is an integer array perm that is a permutation of the first n positive integers, where n is always odd. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2],

wangjun861205的博客 4674

LeetCode每日一题(Majority Element)

Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1: Input: nums = [3,2,3] Output: 3 Example

wangjun861205的博客 4625

LeetCode每日一题(1000. Minimum Cost to Merge Stones)

j]合并为 k 个堆, 从 i…=m], 右侧为 stones[m+1…=j], 左侧合并为 k-1 个堆的成本加上右侧合并为 1 个堆的成本就是整体合并为 k 个堆的成本, dp[i][j][k] = min(dp[i][m][k-1], dp[m+1][j][1])最后一步一定是将 k 个堆合并为 1 个堆: dp[i][j][1] = dp[i][j][k] + sum(i,j)假设 dp[i][j][k]是从 stones[i]到 stones[j]合并为 k 个堆所需要的成本。

wangjun861205的博客 1706

LeetCode每日一题(909. Snakes and Ladders)

这题其实就是个计算最短路径的题目。

wangjun861205的博客 1525

LeetCode每日一题(982. Triples with Bitwise AND Equal To Zero)

因为 nums[i] < 2 ^ 16, 所以 nums[i] & nums[j] < 2 ^ 16, 我们用一个长度为 2 ^ 16 的数组 counts 来保存每个 nums[i] & nums[j]的出现频次,假设 n 为 0 到 2^16 中的每个数字, 如果 nums[i] & n == 0, 则 ans += counts[n]

wangjun861205的博客 1524

LeetCode每日一题(943. Find the Shortest Superstring)

看得官方的解法, 不想多说了, 有兴趣的看。

wangjun861205的博客 1380

LeetCode每日一题(2359. Find Closest Node to Given Two Nodes)

分别计算从 node1 到每个 node 的最短距离 dist_set_1 和 node2 到每个 node 的最短距离 dist_set_2, dist_set_1[i]是从 node1 到 nodes[i]的最短距离, dist_set_2[i]是从 node2 到 nodes[i]的最短距离, 取 max(dist_set_1[i], dist_set_2[i])的最小值, 0

wangjun861205的博客 1142

LeetCode每日一题(2538. Difference Between Maximum and Minimum Price Sum)

假设 to_leaf_sums[i]是 node i 到所有 leaf node 的 sum 的最大值, not_to_leaf_sums[i]是 node i 到所有 leaf node 的 father node 的 sum 的最大值, 那经过 node i 的最大 diff 就是 to_leaf_sums[i] + not_to_leaf_sums[i], 这里要注意, 两个值所对应的 leaf node 不能是同一个 leaf node。not_to_leaf_sums 同理如上。

wangjun861205的博客 1076

LeetCode每日一题(2376. Count Special Integers)

已经使用的数字我们用一个 mask 来保存, 每次取值只能取没有用过的数字。最后要注意的是 0 不能在第一位。

wangjun861205的博客 1035

LeetCode每日一题(2364. Count Number of Bad Pairs)

bad pair 的, 这样我们可以根据 diff 来对 nums 进行分组, 在求出每一组所能组成的 pairs 的数量, 用 nums 所能组成的所有 pairs 的数量减去每组的 pairs 的数量就是最终答案。以上两条是解这题的核心, 假设 diff[i] = nums[i] - i, 那如果 diff[i] == diff[j]则 nums[i]和 nums[j]一定可以组成。bad pair, 所以任何两个有相同的 diff 的 nums 相互之间一定是可以组成。

wangjun861205的博客 982

RVDS2.2破解说明及破解文件

1.要想用Jlink调试ARM11如6410等,只需要用RVDS2.2版本即可,高一点的版本不支持JLINK调试。2.RVDS2.2工程文件后缀名是mcp,和ADS1.2一样。界面及风格看起来一模一样,但是比ADS1.2好用多了。3.破解步骤说明很详细,文中提到的两个破解文件也同时提供。破解后皆可调试。

【光学】模拟像面数字全息离轴干涉含Matlab源码.zip

【光学】模拟像面数字全息离轴干涉含Matlab源码.zip

上一篇: LeetCode每日一题(2014. Longest Subsequence Repeated k Times)
下一篇: LeetCode每日一题(1627. Graph Connectivity With Threshold)
wangjun861205
博客等级 码龄12年 11粉丝 · 464原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值