leetcode 1996. 游戏中弱角色的数量

leetcode 1996. 游戏中弱角色的数量

leetcode 1996. 游戏中弱角色的数量

题目

你正在参加一个多角色游戏,每个角色都有两个主要属性:攻击 和 防御 。给你一个二维整数数组 properties ,其中 properties[i] = [attacki, defensei] 表示游戏中第 i 个角色的属性。

如果存在一个其他角色的攻击和防御等级 都严格高于 该角色的攻击和防御等级,则认为该角色为 弱角色 。更正式地,如果认为角色 i 弱于 存在的另一个角色 j ,那么 attackj > attacki 且 defensej > defensei 。

返回 弱角色 的数量。

示例 1:
输入:properties = [[5,5],[6,3],[3,6]]
输出:0
解释:不存在攻击和防御都严格高于其他角色的角色。

示例 2:
输入:properties = [[2,2],[3,3]]
输出:1
解释:第一个角色是弱角色,因为第二个角色的攻击和防御严格大于该角色。

示例 3:
输入:properties = [[1,5],[10,4],[4,3]]
输出:1
解释:第三个角色是弱角色,因为第二个角色的攻击和防御严格大于该角色。

提示:
2 <= properties.length <= 105
properties[i].length == 2
1 <= attacki, defensei <= 105

解答

typedef struct {
    int attack;
    int defense;
} hash_node;

int cmp(const void *a, const void *b)
{
    hash_node *n1 = (hash_node *)a;
    hash_node *n2 = (hash_node *)b;

    if (n1->attack != n2->attack) {
        return n2->attack - n1->attack;
    }
    return n1->defense - n2->defense;
    
}

int numberOfWeakCharacters(int** properties, int propertiesSize, int* propertiesColSize) {
    hash_node *n = (hash_node *)malloc(sizeof(hash_node) * propertiesSize);
    for (int i = 0; i < propertiesSize; i++) {
        n[i].attack = properties[i][0];
        n[i].defense = properties[i][1];
    }

    qsort(n, propertiesSize, sizeof(hash_node), cmp);

    int count = 0;
    int minAttack = n[0].attack;
    int minDefense = n[0].defense;
    for (int i = 1; i < propertiesSize; i++) {
        if (minDefense > n[i].defense) {
            if (minAttack > n[i].attack) {
                count++;
            }
        } else {
            minDefense = fmax(minDefense, n[i].defense);
            minAttack = n[i].attack;
        }
    }
    return count;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值