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

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

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

给你三个长度为 n 的数组,分别描述 n 个优惠券的属性:code、businessLine 和 isActive。其中,第 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] 的值为 true 或 false。

解题方法:分组 + 排序

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

使用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和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

相关推荐
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
CoderYanger3 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
圣保罗的大教堂3 天前
leetcode 3524. 求出数组的 X 值 I 中等
leetcode
Tim_103 天前
【LeetCode】338、比特位计数
c++·算法·leetcode
mmmmath_33 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Navigator_Z3 天前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.3 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
开开心心就好3 天前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法
All for pursuit.3 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
圣保罗的大教堂3 天前
leetcode 1665. 完成所有任务的最少初始能量 中等
leetcode