★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hqwpphev-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A string of '0'
s and '1'
s is monotone increasing if it consists of some number of '0'
s (possibly 0), followed by some number of '1'
s (also possibly 0.)git
We are given a string S
of '0'
s and '1'
s, and we may flip any '0'
to a '1'
or a '1'
to a '0'
.github
Return the minimum number of flips to make S
monotone increasing.數組
Example 1:微信
Input: "00110"
Output: 1 Explanation: We flip the last digit to get 00111.
Example 2:app
Input: "010110"
Output: 2 Explanation: We flip to get 011111, or alternatively 000111.
Example 3:spa
Input: "00011000"
Output: 2 Explanation: We flip to get 00000000.
Note:code
1 <= S.length <= 20000
S
only consists of '0'
and '1'
characters.若是一個由 '0'
和 '1'
組成的字符串,是以一些 '0'
(可能沒有 '0'
)後面跟着一些 '1'
(也可能沒有 '1'
)的形式組成的,那麼該字符串是單調遞增的。htm
咱們給出一個由字符 '0'
和 '1'
組成的字符串 S
,咱們能夠將任何 '0'
翻轉爲 '1'
或者將 '1'
翻轉爲 '0'
。blog
返回使 S
單調遞增的最小翻轉次數。
示例 1:
輸入:"00110" 輸出:1 解釋:咱們翻轉最後一位獲得 00111.
示例 2:
輸入:"010110" 輸出:2 解釋:咱們翻轉獲得 011111,或者是 000111。
示例 3:
輸入:"00011000" 輸出:2 解釋:咱們翻轉獲得 00000000。
提示:
1 <= S.length <= 20000
S
中只包含字符 '0'
和 '1'
36ms
1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var zeros = 0; 4 var ones = 0; 5 var res = 0; 6 var S = Array(S) 7 for i in S { 8 if i == "0" { 9 zeros+=1 10 } else { 11 ones+=1 12 } 13 } 14 15 var start = 0; 16 var end = S.count - 1; 17 while end > start { 18 if ones >= zeros { 19 if(S[end] == "1") { 20 ones-=1 21 } 22 if S[end] == "0" { 23 zeros-=1 24 res+=1 25 } 26 end-=1 27 } 28 else{ 29 if(S[start] == "0") { 30 zeros-=1 31 } 32 if(S[start] == "1"){ 33 ones-=1; 34 res+=1; 35 } 36 start+=1; 37 } 38 } 39 return res; 40 } 41 }
56ms
1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 4 var chars = Array(S) 5 let totalItems = chars.count 6 var prefixSums = [Int](repeating: 0, count: totalItems + 1) 7 for i in 0..<chars.count { 8 prefixSums[i + 1] = prefixSums[i] + (chars[i] == "1" ? 1 : 0) 9 } 10 var result = Int.max 11 for i in 0..<prefixSums.count { 12 13 let flipCounter = prefixSums[i] + (totalItems - i) - (prefixSums.last! - prefixSums[i]) 14 result = min(result, flipCounter) 15 } 16 return result 17 } 18 }
60ms
1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var s: [Int] = S.map { $0 == "0" ? 0 : 1} 4 var onlyOnes: [Int] = Array(repeating: 0, count: s.count) 5 var onlyZeros: [Int] = Array(repeating: 0, count: s.count) 6 7 for i in (0..<s.count).reversed() { 8 let prev = (i < (s.count - 1)) ? onlyOnes[i+1] : 0 9 onlyOnes[i] = (s[i] == 0 ? 1 : 0) + prev 10 } 11 12 for i in 0..<s.count { 13 let prev = (i > 0) ? onlyZeros[i-1] : 0 14 onlyZeros[i] = (s[i] == 1 ? 1 : 0) + prev 15 } 16 17 var minFlips = min(onlyZeros.last!, onlyOnes.first!) 18 for i in 0..<(onlyOnes.count - 1) { 19 minFlips = min(minFlips, onlyZeros[i] + onlyOnes[i+1]) 20 } 21 return minFlips 22 } 23 }
108ms
1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var arr:[Character] = [Character]() 4 for char in S.characters 5 { 6 arr.append(char) 7 } 8 let len = S.count 9 //聲明數組 10 var ct:[Int] = [Int](repeating: 0,count: len + 1) 11 var x:Int = 0 12 for i in 0...len 13 { 14 ct[i] += x 15 if i < len && arr[i] == "1" 16 { 17 x += 1 18 } 19 } 20 x = 0 21 for i in (0...len).reversed() 22 { 23 ct[i] += x 24 if i > 0 && arr[i - 1] == "0" 25 { 26 x += 1 27 } 28 } 29 var res = 999999999 30 for i in 0...len 31 { 32 res = min(res,ct[i]) 33 } 34 return res 35 } 36 }
116ms
1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var arr:[Character] = [Character]() 4 for char in S.characters 5 { 6 arr.append(char) 7 } 8 let len = S.count 9 //聲明數組 10 var ct:[Int] = [Int](repeating: 0,count: len + 1) 11 var x:Int = 0 12 for i in 0...len 13 { 14 ct[i] += x 15 if i < len && arr[i] == "1" 16 { 17 x += 1 18 } 19 } 20 x = 0 21 for i in (0...len).reversed() 22 { 23 ct[i] += x 24 if i > 0 && arr[i - 1] == "0" 25 { 26 x += 1 27 } 28 } 29 var res = 999999999 30 for i in 0...len 31 { 32 res = min(res,ct[i]) 33 } 34 return res 35 } 36 }