跳至主要內容

1773. 统计匹配检索规则的物品数量


1773. 统计匹配检索规则的物品数量

🟢   🔖  数组 字符串  🔗 力扣open in new window LeetCodeopen in new window

题目

You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.

The ith item is said to match the rule if one of the following is true:

  • ruleKey == "type" and ruleValue == typei.
  • ruleKey == "color" and ruleValue == colori.
  • ruleKey == "name" and ruleValue == namei.

Return the number of items that match the given rule.

Example 1:

Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"

Output: 1

Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].

Example 2:

Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"

Output: 2

Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.

Constraints:

  • 1 <= items.length <= 10^4
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey is equal to either "type", "color", or "name".
  • All strings consist only of lowercase letters.

题目大意

给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。

另给你一条由两个字符串 ruleKeyruleValue 表示的检索规则。

如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配

  • ruleKey == "type"ruleValue == typei
  • ruleKey == "color"ruleValue == colori
  • ruleKey == "name"ruleValue == namei

统计并返回 匹配检索规则的物品数量

示例 1:

输入: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"

输出: 1

解释: 只有一件物品匹配检索规则,这件物品是 ["computer","silver","lenovo"] 。

示例 2:

输入: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"

输出: 2

解释: 只有两件物品匹配检索规则,这两件物品分别是 ["phone","blue","pixel"] 和 ["phone","gold","iphone"] 。注意,["computer","silver","phone"] 未匹配检索规则。

提示:

  • 1 <= items.length <= 10^4
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey 等于 "type""color""name"
  • 所有字符串仅由小写字母组成

解题思路

  1. 映射规则键到索引

    • 题目中指定的 ruleKey 表示数组中每个元素的属性(typecolorname)。
    • 使用一个 keyMap 映射表将这些键映射到对应的索引,方便访问 items 的具体列。
  2. 筛选匹配的元素

    • 利用 filter 遍历 items,对于每个元素,检查对应索引处的值是否与 ruleValue 相等。
    • 如果相等,则保留该元素。
  3. 统计数量

    • 筛选后,filter 返回的数组长度即为符合条件的元素数量。

复杂度分析

  • 时间复杂度O(n),其中 nitems 的长度,遍历 items 一次。
  • 空间复杂度O(n),使用了 filter 生成一个中间数组,最坏情况下所有元素都符合条件,占用 O(n) 的空间。

代码

/**
 * @param {string[][]} items
 * @param {string} ruleKey
 * @param {string} ruleValue
 * @return {number}
 */
var countMatches = function (items, ruleKey, ruleValue) {
	const keyMap = {
		type: 0,
		color: 1,
		name: 2
	};
	return items.filter((i) => i[keyMap[ruleKey]] == ruleValue).length;
};