LeetCode contest 199 灯泡开关 IV Bulb Switcher IV

简介: LeetCode contest 199 灯泡开关 IV Bulb Switcher IV

LeetCode contest 199 灯泡开关 IV Bulb Switcher IV


Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

房间中有 n 个灯泡,编号从 0 到 n-1 ,自左向右排成一行。最开始的时候,所有的灯泡都是 关 着的。

请你设法使得灯泡的开关状态和 target 描述的状态一致,其中 target[i] 等于 1 第 i 个灯泡是开着的,等于 0 意味着第 i 个灯是关着的。

有一个开关可以用于翻转灯泡的状态,翻转操作定义如下:

选择当前配置下的任意一个灯泡(下标为 i )

翻转下标从 i 到 n-1 的每个灯泡

翻转时,如果灯泡的状态为 0 就变为 1,为 1 就变为 0 。

返回达成 target 描述的状态所需的 最少 翻转次数。

 

示例 1:

输入:target = "10111"
输出:3
解释:初始配置 "00000".
从第 3 个灯泡(下标为 2)开始翻转 "00000" -> "00111"
从第 1 个灯泡(下标为 0)开始翻转 "00111" -> "11000"
从第 2 个灯泡(下标为 1)开始翻转 "11000" -> "10111"
至少需要翻转 3 次才能达成 target 描述的状态

示例 2:

输入:target = "101"
输出:3
解释:"000" -> "111" -> "100" -> "101".

示例 3:

输入:target = "00000"
输出:0

示例 4:

输入:target = "001011101"
输出:5

提示:

1 <= target.length <= 10^5

target[i] == '0' 或者 target[i] == '1'


二、英文版

There is a room with n bulbs, numbered from 0 to n-1, arranged in a row from left to right. Initially all the bulbs are turned off.

Your task is to obtain the configuration represented by target where target[i] is '1' if the i-th bulb is turned on and is '0' if it is turned off.

You have a switch to flip the state of the bulb, a flip operation is defined as follows:

Choose any bulb (index i) of your current configuration.

Flip each bulb from index i to n-1.

When any bulb is flipped it means that if it is 0 it changes to 1 and if it is 1 it changes to 0.

Return the minimum number of flips required to form target.

 

Example 1:

Input: target = "10111"
Output: 3
Explanation: Initial configuration "00000".
flip from the third bulb:  "00000" -> "00111"
flip from the first bulb:  "00111" -> "11000"
flip from the second bulb:  "11000" -> "10111"
We need at least 3 flip operations to form target.

Example 2:

Input: target = "101"
Output: 3
Explanation: "000" -> "111" -> "100" -> "101".

Example 3:

Input: target = "00000"
Output: 0

Example 4:

Input: target = "001011101"
Output: 5

Constraints:

1 <= target.length <= 10^5

target[i] == '0' or target[i] == '1'


三、My answer

class Solution:
    def minFlips(self, target: str) -> int:
        res = 0
        n = len(target)
        last_one = 0
        for i in range(n):
            if target[i] != last_one:
                res += 1
                last_one = target[i]
        return res


四、解题报告

数据结构:数组

算法:遍历

实现:可以不按照题目给的例子进行翻转,将例1的翻转过程改为

"00000"--> "11111"-->"10000"--> "10111"

再结合其他例子就可以总结出翻转的方法:

从左往右遍历 target 数组,遇到第一个 1 时,将 1 及以后所有数字都翻转一次;下一次就是遇到 0 时,0及后边的数字都再翻转一次;也就是每次遇到跟之前数字不一样时,后面的数字都需要翻转。

如此,便可以统计出需要翻转的总次数。

相关文章
|
1月前
leetcode-1345:跳跃游戏 IV
leetcode-1345:跳跃游戏 IV
31 0
|
7月前
Leetcode 377. Combination Sum IV
赤裸裸的完全背包,属于动态规划的范畴,大家有兴趣可以在网上搜索下其他资料。个人觉得动态规划还是比较难理解的,更难给别人讲清楚,所以这里我直接附上我的代码供大家参考。
31 0
|
18天前
|
存储 算法 数据可视化
LeetCode 力扣题目:买卖股票的最佳时机 IV
LeetCode 力扣题目:买卖股票的最佳时机 IV
|
22天前
dp表,哈希表,力扣5.最长回文子串力扣1745.分割回文串IV力扣132.分割回文串II优先级队列(堆)是什么下面是手动实现小根堆力扣1046.最后一块石头的重量
dp表,哈希表,力扣5.最长回文子串力扣1745.分割回文串IV力扣132.分割回文串II优先级队列(堆)是什么下面是手动实现小根堆力扣1046.最后一块石头的重量
|
1月前
|
算法
leetcode代码记录(买卖股票的最佳时机 IV
leetcode代码记录(买卖股票的最佳时机 IV
25 2
|
1月前
|
SQL
leetcode-SQL-550. 游戏玩法分析 IV
leetcode-SQL-550. 游戏玩法分析 IV
32 1
|
1月前
|
Java
2454. 下一个更大元素 IV --力扣 --JAVA
给你一个下标从 0 开始的非负整数数组 nums 。对于 nums 中每一个整数,你必须找到对应元素的 第二大 整数。 如果 nums[j] 满足以下条件,那么我们称它为 nums[i] 的 第二大 整数: j > i nums[j] > nums[i] 恰好存在 一个 k 满足 i < k < j 且 nums[k] > nums[i] 。 如果不存在 nums[j] ,那么第二大整数为 -1 。 比方说,数组 [1, 2, 4, 3] 中,1 的第二大整数是 4 ,2 的第二大整数是 3 ,3 和 4 的第二大整数是 -1 。 请你返回一个整数数组 answer ,其中 answer
45 1
2454. 下一个更大元素 IV --力扣 --JAVA
|
1月前
代码随想录Day42 动态规划10 LeetCode T123 买卖股票的最佳时机III T188买卖股票的最佳时机IV
代码随想录Day42 动态规划10 LeetCode T123 买卖股票的最佳时机III T188买卖股票的最佳时机IV
38 0
|
1月前
|
测试技术
代码随想录 Day37 完全背包理论基础 卡码网T52 LeetCode T518 零钱兑换II T377 组合总和IV
代码随想录 Day37 完全背包理论基础 卡码网T52 LeetCode T518 零钱兑换II T377 组合总和IV
40 0
|
1月前
|
缓存 算法 测试技术
【单调栈】【二分查找】LeetCode: 2454.下一个更大元素 IV
【单调栈】【二分查找】LeetCode: 2454.下一个更大元素 IV