[Swift]LeetCode1111. 有效括號的嵌套深度 | Maximum Nesting Depth of Two Valid Parentheses Strings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ughouwtm-ky.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:git

  • It is the empty string, or
  • It can be written as AB (A concatenated with B), where A and B are VPS's, or
  • It can be written as (A), where A is a VPS.

We can similarly define the nesting depth depth(S) of any VPS S as follows:github

  • depth("") = 0
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.

For example,  """()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's. 數組

Given a VPS seq, split it into two disjoint subsequences A and B, such that A and B are VPS's (and A.length + B.length = seq.length).微信

Now choose any such A and B such that max(depth(A), depth(B)) is the minimum possible value.app

Return an answer array (of length seq.length) that encodes such a choice of A and B:  answer[i] = 0 if seq[i] is part of A, else answer[i] = 1.  Note that even though multiple answers may exist, you may return any of them. 編碼

Example 1:spa

Input: seq = "(()())"
Output: [0,1,1,1,1,0]

Example 2:code

Input: seq = "()(())()"
Output: [0,0,0,1,1,0,1,1] 

Constraints:htm

  • 1 <= text.size <= 10000

有效括號字符串 僅由 "(" 和 ")" 構成,並符合下述幾個條件之一:

  • 空字符串
  • 鏈接,能夠記做 ABA 與 B 鏈接),其中 A 和 B 都是有效括號字符串
  • 嵌套,能夠記做 (A),其中 A 是有效括號字符串

相似地,咱們能夠定義任意有效括號字符串 s 的 嵌套深度 depth(S)

  • s 爲空時,depth("") = 0
  • s 爲 A 與 B 鏈接時,depth(A + B) = max(depth(A), depth(B)),其中 A 和 B 都是有效括號字符串
  • s 爲嵌套狀況,depth("(" + A + ")") = 1 + depth(A),其中 A 是有效括號字符串

例如:"""()()",和 "()(()())" 都是有效括號字符串,嵌套深度分別爲 0,1,2,而 ")(" 和 "(()" 都不是有效括號字符串。 

給你一個有效括號字符串 seq,將其分紅兩個不相交的子序列 A 和 B,且 A 和 B 知足有效括號字符串的定義(注意:A.length + B.length = seq.length)。

如今,你須要從中選出 任意 一組有效括號字符串 A 和 B,使 max(depth(A), depth(B)) 的可能取值最小。

返回長度爲 seq.length 答案數組 answer ,選擇 A 仍是 B 的編碼規則是:若是 seq[i] 是 A 的一部分,那麼 answer[i] = 0。不然,answer[i] = 1。即使有多個知足要求的答案存在,你也只需返回 一個。 

示例 1:

輸入:seq = "(()())"
輸出:[0,1,1,1,1,0]

示例 2:

輸入:seq = "()(())()"
輸出:[0,0,0,1,1,0,1,1] 

提示:

  • 1 <= text.size <= 10000

24ms
 1 class Solution {
 2     func maxDepthAfterSplit(_ seq: String) -> [Int] {
 3         let N = seq.count 
 4         var chars = Array(seq)
 5         var result = [Int](repeating:0, count: N)
 6         for i in 0..<N {
 7             result[i] = (chars[i] == "(") ? (i & 1) : (1 - i & 1)
 8         }
 9         return result
10     }
11 }

Runtime: 32 ms

Memory Usage: 21.4 MB
 1 class Solution {
 2     func maxDepthAfterSplit(_ seq: String) -> [Int] {
 3         let n:Int = seq.count
 4         var levels:[Int] = [Int](repeating:0,count:n + 1)
 5         let arrSeq:[Character] = Array(seq)
 6         for i in 0..<n
 7         {
 8             let num:Int = arrSeq[i] == "(" ? +1 : -1
 9             levels[i + 1] = levels[i] + num
10         }
11         let max_level:Int = levels.max()!
12         let half:Int = max_level / 2
13         var answer:[Int] = [Int](repeating:0,count:n)
14         for i in 0..<n
15         {
16             answer[i] = min(levels[i], levels[i + 1]) < half ? 1 : 0
17         }
18         return answer
19     }
20 }

32ms 
1 class Solution {
2     func maxDepthAfterSplit(_ seq: String) -> [Int] {
3         var arr = [Int]()
4         for i in 0..<seq.length {
5             arr.append(1)
6         }
7         return arr
8     }
9 }
相關文章
相關標籤/搜索