LeetCode 3606.优惠券校验器:分类 + 排序

【LetMeFly】3606.优惠券校验器:分类 + 排序

力扣题目链接:https://leetcode.cn/problems/coupon-code-validator/

给你三个长度为 n 的数组,分别描述 n 个优惠券的属性:codebusinessLineisActive。其中,第 i 个优惠券具有以下属性:

  • code[i]:一个 字符串,表示优惠券的标识符。
  • businessLine[i]:一个 字符串,表示优惠券所属的业务类别。
  • isActive[i]:一个 布尔值,表示优惠券是否当前有效。

当以下所有条件都满足时,优惠券被认为是 有效的

  1. code[i] 不能为空,并且仅由字母数字字符(a-z、A-Z、0-9)和下划线(_)组成。
  2. businessLine[i] 必须是以下四个类别之一:"electronics""grocery""pharmacy""restaurant"
  3. isActive[i]true

返回所有 有效优惠券的标识符组成的数组,按照以下规则排序:

  • 先按照其 businessLine 的顺序排序:"electronics""grocery""pharmacy""restaurant"
  • 在每个类别内,再按照 **标识符的字典序(升序)**排序。

示例 1:
输入: code = "SAVE20","","PHARMA5","SAVE@20", businessLine = "restaurant","grocery","pharmacy","restaurant", isActive = true,true,true,true

输出: "PHARMA5","SAVE20"

解释:

  • 第一个优惠券有效。
  • 第二个优惠券的标识符为空(无效)。
  • 第三个优惠券有效。
  • 第四个优惠券的标识符包含特殊字符 @(无效)。

示例 2:
输入: code = "GROCERY15","ELECTRONICS_50","DISCOUNT10", businessLine = "grocery","electronics","invalid", isActive = false,true,true

输出: "ELECTRONICS_50"

解释:

  • 第一个优惠券无效,因为它未激活。
  • 第二个优惠券有效。
  • 第三个优惠券无效,因为其业务类别无效。

提示:

  • n == code.length == businessLine.length == isActive.length
  • 1 <= n <= 100
  • 0 <= code[i].length, businessLine[i].length <= 100
  • code[i]businessLine[i] 由可打印的 ASCII 字符组成。
  • isActive[i] 的值为 truefalse

解题方法:分组 + 排序

分组/分类似乎差不多,暂不深究。

使用4个数组,分别存放合法的4类优惠券。最后对4个数组分别按字符串字典序排序,合并为一个数组并返回。

如何判断一个优惠券是否合法?

  1. active为true
  2. businessLine属于4类之一
  3. code不为空且只由数组字母下划线组成

注意,C++中合并vector时若被合并vector后续无需再使用,则可以使用make_move_iterator在内存上移动。

  • 时间复杂度 O ( L log ⁡ n ) O(L\log n) O(Llogn),其中 L L L是合法code总字符数
  • 空间复杂度:C++ O ( L ) O(L) O(L)

AC代码

C++
cpp 复制代码
/*
 * @LastEditTime: 2025-12-13 22:42:29
 */
class Solution {
private:
    inline bool is_ok(string& s) {
        for (char c : s) {
            if (c != '_' && !isalnum(c)) {
                return false;
            }
        }
        return !s.empty();
    }
public:
    vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
        vector<string> electronics, grocery, pharmacy, restaurant;
        for (int i = 0; i < code.size(); i++) {
            if (!isActive[i]) {
                continue;
            }
            if (!is_ok(code[i])) {
                continue;
            }
            if (businessLine[i] == "electronics") {
                electronics.push_back(code[i]);
            } else if (businessLine[i] == "grocery") {
                grocery.push_back(code[i]);
            } else if (businessLine[i] == "pharmacy") {
                pharmacy.push_back(code[i]);
            } else if (businessLine[i] == "restaurant") {
                restaurant.push_back(code[i]);
            }
        }
        sort(electronics.begin(), electronics.end());
        sort(grocery.begin(), grocery.end());
        sort(pharmacy.begin(), pharmacy.end());
        sort(restaurant.begin(), restaurant.end());
        vector<string> ans;
        ans.reserve(electronics.size() + grocery.size() + pharmacy.size() + restaurant.size());
        ans.insert(ans.end(), make_move_iterator(electronics.begin()), make_move_iterator(electronics.end()));
        ans.insert(ans.end(), make_move_iterator(grocery.begin()), make_move_iterator(grocery.end()));
        ans.insert(ans.end(), make_move_iterator(pharmacy.begin()), make_move_iterator(pharmacy.end()));
        ans.insert(ans.end(), make_move_iterator(restaurant.begin()), make_move_iterator(restaurant.end()));
        return ans;
    }
};

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

相关推荐
如竟没有火炬8 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
8Qi88 小时前
LeetCode 1143 & 718:最长公共子序列 / 最长重复子数组
算法·leetcode·职场和发展·动态规划
想吃火锅100510 小时前
【leetcode】1.两数之和js版
javascript·算法·leetcode
如何原谅奋力过但无声14 小时前
【灵神高频面试题合集09-13】二叉树、二叉搜索树
数据结构·算法·leetcode
小欣加油15 小时前
leetcode2161 根据给定数字划分数组
数据结构·c++·算法·leetcode·职场和发展
cpp_250115 小时前
P2947 [USACO09MAR] Look Up S
数据结构·c++·算法·题解·单调栈·洛谷
8Qi816 小时前
LeetCode 115 & 392:不同子序列 / 判断子序列
算法·leetcode·职场和发展·动态规划
圣保罗的大教堂17 小时前
leetcode 2161. 根据给定数字划分数组 中等
leetcode
8Qi818 小时前
LeetCode 72:编辑距离(Edit Distance)—— 题解
算法·leetcode·职场和发展·动态规划
8Qi818 小时前
LeetCode 583. 两个字符串的删除操作
算法·leetcode·职场和发展·动态规划