365天挑战LeetCode1000题——Day 110 状态机

801. 使序列递增的最小交换次数

在这里插入图片描述

代码实现

class Solution {
public:
    int minSwap(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        int a = 0, b = 1;
        for (int i = 1; i < n; i++) {
            int at = a, bt = b;
            a = b = n;
            if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])  {
                a = min(a, at);
                b = min(b, bt + 1);
            }
            if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
                a = min(a, bt);
                b = min(b, at + 1);
            }
        }
        return min(a, b);
    }
};

LCP 17. 速算机器人

在这里插入图片描述

代码实现

class Solution {
public:
    int calculate(string s) {
        int x = 1, y = 0;
        for (char c : s) {
            if (c == 'A') {
                x = x * 2 + y;
            }
            else {
                y = y * 2 + x;
            }
        }
        return x + y;
    }
};

LCP 39. 无人机方阵

在这里插入图片描述

代码实现

class Solution {
public:
    int minimumSwitchingTimes(vector<vector<int>>& source, vector<vector<int>>& target) {
        unordered_map<int, int> mp1, mp2;
        int n = source.size();
        int m = source[0].size();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                mp1[source[i][j]]++;
                mp2[target[i][j]]++;
            }
        }
        int ans = 0;
        for (auto it = mp2.begin(); it != mp2.end(); it++) {
            if (!mp1.count(it->first)) {
                ans += it->second;
            }
            else if (mp1[it->first] < it->second) {
                ans += it->second - mp1[it->first];
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值