`
superhack
  • 浏览: 32059 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-10-1000

 
阅读更多

Problem Statement

    

You have a string s containing only ones and zeroes. Your goal is to make all the characters in s equal using a minimal number of operations. A single operation consists of inverting all the characters in a contiguous substring of s. Inverting a character means transforming a one into a zero, and vice versa.

For example, consider the string "0001100" (quotes for clarity). We can transform this string into all ones using 2 operations:

  1. Invert the entire string: 0001100 -> 1110011.
  2. Invert the substring from the 4th character to the 5th character, inclusive: 1110011 -> 1111111.

We can transform the same string into all zeros using just 1 operation:

  1. Invert the substring from the 4th character to the 5th character, inclusive: 0001100 -> 0000000.
Thus, the answer for s="0001100" is 1.

You are given s as a String[]. Concatenate the elements of s to obtain the actual string, and return the minimal number of operations necessary to transform s into all ones or all zeroes.

Definition

    
Class: SwitchingBits
Method: minSwitches
Parameters: String[]
Returns: int
Method signature: int minSwitches(String[] s)
(be sure your method is public)
    
 

Constraints

- s will contain between 1 to 50 elements, inclusive.
- Each element of s will contain between 1 and 50 characters, inclusive.
- Each element of s will contain only ones ('1') and zeroes ('0').

Examples

0)  
    
{"0001100"}

Returns: 1

Invert the two ones in the middle.
1)  
    
{"11111"}

Returns: 0

Already all ones.
2)  
    
{"00000001"}

Returns: 1

There are two possibilities here: invert the one, or invert all the zeroes. Either way, only 1 operation is needed.
3)  
    
{"1100110011001100","000","1"}

Returns: 4

 
4)  
    
{"11","101","101"}

Returns: 2

There are several ways. For example: 11101101 -> 11110011 -> 11111111 or 11101101 -> 11111101 -> 11111111
public class SwitchingBits {

	public int minSwitches(String[] ss) {
		String str = "";
		for (String s : ss)
			str += s;
		int a = str.replaceAll("1+", "1").replaceAll("0", "").length();
		int b = str.replaceAll("0+", "0").replaceAll("1", "").length();
		return Math.min(a, b);
	}

}

 

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    - 计算`PDCHS_zhuan`和`TCHS_zhuan`(专用数据信道和语音信道转换后的数量)。 - 计算`DSP_pdch`和`DSP_tch`(所需的数字信号处理器数量)。 #### 输出结果 - 使用`printf`函数输出计算结果,包括基站数量、链路接...

    Bit Error Rate Testing with CMU300

    BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...

    基站代维考试复习题汇总.doc

    TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 <input type="text" tchs=""></input> 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

    TOPCODER 算法PPT1

    此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...

Global site tag (gtag.js) - Google Analytics