leetcode - 2038. Remove Colored Pieces if Both Neighbors are the Same Color

Description

There are n pieces arranged in a line, and each piece is colored either by ‘A’ or by ‘B’. You are given a string colors of length n where colors[i] is the color of the ith piece.

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

Alice is only allowed to remove a piece colored ‘A’ if both its neighbors are also colored ‘A’. She is not allowed to remove pieces that are colored ‘B’.
Bob is only allowed to remove a piece colored ‘B’ if both its neighbors are also colored ‘B’. He is not allowed to remove pieces that are colored ‘A’.
Alice and Bob cannot remove pieces from the edge of the line.
If a player cannot make a move on their turn, that player loses and the other player wins.
Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

Example 1:

Input: colors = "AAABABB"
Output: true
Explanation:
AAABABB -> AABABB
Alice moves first.
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.Now it's Bob's turn.
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
Thus, Alice wins, so return true.

Example 2:

Input: colors = "AA"
Output: false
Explanation:
Alice has her turn first.
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
Thus, Bob wins, so return false.

Example 3:

Input: colors = "ABBBBBBBAAA"
Output: false
Explanation:
ABBBBBBBAAA -> ABBBBBBBAA
Alice moves first.
Her only option is to remove the second to last 'A' from the right.ABBBBBBBAA -> ABBBBBBAA
Next is Bob's turn.
He has many options for which 'B' piece to remove. He can pick any.On Alice's second turn, she has no more pieces that she can remove.
Thus, Bob wins, so return false.

Constraints:

1 <= colors.length <= 10^5
colors consists of only the letters 'A' and 'B'

Solution

Solved after hints…

The number of removing alphabets doesn’t depend on the other player, but simply depends on the number of consecutive A or B. The number Alice could remove A is the number of consecutive A - 2, so does Bob.

Calculate the number of moving alphabets, then compare.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

class Solution:def winnerOfGame(self, colors: str) -> bool:alice, bob = 0, 0consec_a, consec_b = -2, -2for i in colors:if i == 'A':consec_b = -2consec_a += 1if consec_a > 0:alice += 1elif i == 'B':consec_a = -2consec_b += 1if consec_b > 0:bob += 1return alice > bob

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/98658.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++统一初始化和初始化列表

一直对C初始化使用圆括号和花括号的区别有所疑惑&#xff0c;参考书籍和博客简单总结一下 文章目录 常见的初始化操作统一初始化(Uniform Initialization)初始化列表(Initializer Lists) 常见的初始化操作 对于一个基础数据类型进行初始化&#xff0c;比如 int&#xff1a; i…

深度学习基础知识 nn.Sequential | nn.ModuleList | nn.ModuleDict

深度学习基础知识 nn.Sequential &#xff5c; nn.ModuleList &#xff5c; nn.ModuleDict 1、nn.Sequential 、 nn.ModuleList 、 nn.ModuleDict 类都继承自 Module 类。2、nn.Sequential、nn.ModuleList 和 nn.ModuleDict语法3、Sequential 、ModuleDict、 ModuleList 的区别…

【多线程进阶】线程安全的集合类

文章目录 前言1. 多线程环境使用 ArrayList2. 多线程环境使用队列3. 多线程环境使用哈希表3.1 HashTable3.2 ConcurrentHashMap 总结 前言 本文主要讲解 Java 线程安全的集合类, 在之前学习过的集合类中, 只有 Vector, Stack, HashTable, 是线程安全的, 因为在他们的关键方法中…

vue项目中使用rem替换px-使用方法-03-使用插件

使用场景:有适配pc端改为适配pc端和移动端,使用2套css App.vue created: function () {if(document.documentElement.clientWidth > 640){require(./style/index.:);this.plaform = pc;}else{require(./style/mobile.scss);this.plaform = mobile;} } 注意: 为2套css分…

axios登录,登出接口的简单封装步骤详解!

目录 总结一、步骤1.安装Axios&#xff1a;2.axios对象封装3.请求api封装4.使用pinia临时库保存响应信息&#xff08;按需求用&#xff09;5.最后&#xff0c;在组件中使用&#xff01; 总结 封装axios对象&#xff0c;编写公共请求代码、添加拦截逻辑、然后分层实现axios请求…

anaconda安装及配置+pytorch安装与配置(自用笔记)

anaconda安装及配置 1、anaconda官网下载安装包 下载好后进行安装 2、anaconda安装地址(记住安装路径)&#xff1a; 3、配置环境变量 打开anaconda prompt: 输入命令conda list: 可以看到安装好的很多包&#xff01; 至此anaconda配置完成。 PyTorch的安装与配置 使用con…

android 代码设置静态Ip地址的方法

在Android中&#xff0c;可以使用以下代码示例来设置静态IP地址&#xff1a; import android.content.Context import android.net.ConnectivityManager import android.net.LinkAddress import android.net.Network import android.net.NetworkCapabilities import android.ne…

分库分表理论总结

一、概述 分库分表是在面对高并发、海量数量时常见的数据库层面的解决方案。通过把数据分散到不同的数据库中&#xff0c;使得单一数据库的数据量变小来缓解单一数据库的性能问题&#xff0c;从而达到提升数据库性能的目的。比如&#xff1a;将电商数据库拆分为若干独立的数据…

安装配置deep learning开发环境

1. 下载安装anacondahttps://www.anaconda.com/download-success vim ~/.condarcchannels: - bioconda - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/ - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/ - https://mirrors.tuna.tsinghua.edu.cn/anaco…

Android 自定义PopupWindow,实现下拉框

1、效果图 2、前言 1、页面由 MagicIndicator ViewPager2 Fragment 实现&#xff1b; 2、下拉框是基于WindowManager实现&#xff1b; 3、我使用PopupWindow实现下拉框时&#xff0c;发现一个问题&#xff0c;PopupWindow 在窗口显示的情况下&#xff0c;无法直接从外部修…

面试经典 150 题 4 —(数组 / 字符串)— 80. 删除有序数组中的重复项 II

80. 删除有序数组中的重复项 II 方法一 class Solution { public:int removeDuplicates(vector<int>& nums) {int len 0;for(auto num : nums)if(len < 2 || nums[len-2] ! num)nums[len] num;return len;} };方法二 class Solution { public:int removeDupli…

分享购商业模式解析:三个角色、排位机制、奖励机制

分享购是一个自循环商业模式&#xff0c;它通过将第三方平台&#xff08;如京东、天猫、淘宝等&#xff09;的优惠券和折扣等信息整合到自营商城中&#xff0c;并合理分配给用户或消费者&#xff0c;增强了用户黏性&#xff0c;实现了持续多次的变现。分享购的核心机制是其排位…

Rabbitmq安装-docker版

1.简介 2.安装消息队列 下载地址https://www.rabbitmq.com/download.html 使用docker方式安装 需要先下载docker&#xff0c;参考文章https://blog.csdn.net/weixin_43917045/article/details/104747341?csdn_share_tail%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22arti…

操作系统备考学习 day7 (2.3.4 ~ 2.3.5)

操作系统备考学习 day7 第二章 进程与线程2.3 同步与互斥2.3.4 信号量 用信号量实现进程互斥、同步、前驱关系信号量机制实现进程互斥信号量机制实现进程同步信号量机制实现前驱关系 2.3.5 经典同步问题生产者-消费者问题多生产者和多消费者模型抽烟者问题读者-写者问题哲学家进…

使用运放产生各种波形

目录复制 文章目录 RC正弦振荡电路文氏电桥振荡电路移项式正弦波振荡电路 集成函数发生器运算放大器驱动电容性负载峰值检波多通道运放未使用的运放接法 RC正弦振荡电路 文氏电桥振荡电路 这个振荡器起振条件RF > 2R1,起振后又希望RF 2R1产生矛盾怎么办&#xff1f; 将RF换…

centos7终端无图形界面安装tbb

1、官网下载tbb&#xff1a; https://www.intel.com/content/www/us/en/developer/articles/tool/oneapi-standalone-components.html#onetbb 2、终端执行&#xff1a; ./l_tbb_oneapi_p_2021.10.0.49543_offline.sh -a --cli3、cd /opt/intel/oneapi 4、source setvars.sh …

鼎盛合:adc芯片的五种结构

随着国内消费电子产品、通信、工业自动化、汽车电子等行业的快速发展&#xff0c;对ADC芯片的需求不断增加&#xff0c;国内ADC芯片近年也在持续稳定地发展着。ADC种类多样&#xff0c;可分为单通道ADC、多通道ADC、高速ADC、精密ADC和集成ADC等&#xff0c;主要应用于医疗仪器…

LeetCode 1251. 平均售价

题目链接&#xff1a;1251. 平均售价 题目描述 表&#xff1a;Prices Column NameTypeproduct_idintstart_datedateend_datedatepriceint (product_id&#xff0c;start_date&#xff0c;end_date) 是 prices 表的主键&#xff08;具有唯一值的列的组合&#xff09;。 price…

【LeetCode高频SQL50题-基础版】打卡第3天:第16~20题

文章目录 【LeetCode高频SQL50题-基础版】打卡第3天&#xff1a;第16~20题⛅前言 平均售价&#x1f512;题目&#x1f511;题解 项目员工I&#x1f512;题目&#x1f511;题解 各赛事的用户注册率&#x1f512;题目&#x1f511;题解 查询结果的质量和占比&#x1f512;题目&am…

VS2022配置Opencv

配置环境变量 配置路径 由于新版本VS属性管理器没有Microsoft.cpp.x64.user文件&#xff0c;可以选择直接在Debug x64进行配置 配置包含目录和库目录 配置链接器