[Swift]LeetCode775. 全局倒置與局部倒置 | Global and Local Inversions

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

We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A.git

The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j].github

The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1].數組

Return true if and only if the number of global inversions is equal to the number of local inversions.微信

Example 1:this

Input: A = [1,0,2]
Output: true
Explanation: There is 1 global inversion, and 1 local inversion.

Example 2:spa

Input: A = [1,2,0]
Output: false
Explanation: There are 2 global inversions, and 1 local inversion.

Note:code

  • A will be a permutation of [0, 1, ..., A.length - 1].
  • A will have length in range [1, 5000].
  • The time limit for this problem has been reduced.

數組 A 是 [0, 1, ..., N - 1] 的一種排列,N 是數組 A 的長度。全局倒置指的是 i,j 知足 0 <= i < j < N 而且 A[i] > A[j] ,局部倒置指的是 i 知足 0 <= i < N 而且 A[i] > A[i+1] 。htm

當數組 A 中全局倒置的數量等於局部倒置的數量時,返回 true 。 blog

示例 1:

輸入: A = [1,0,2]
輸出: true
解釋: 有 1 個全局倒置,和 1 個局部倒置。

示例 2:

輸入: A = [1,2,0]
輸出: false
解釋: 有 2 個全局倒置,和 1 個局部倒置。

注意:

  • A 是 [0, 1, ..., A.length - 1] 的一種排列
  • A 的長度在 [1, 5000]之間
  • 這個問題的時間限制已經減小了。

Runtime: 380 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func isIdealPermutation(_ A: [Int]) -> Bool {
 3         for i in 0..<A.count
 4         {
 5             if abs(A[i] - i) > 1
 6             {
 7                 return false
 8             }
 9         }
10         return true
11     }
12 }

476ms

 1 class Solution {
 2     func isIdealPermutation(_ A: [Int]) -> Bool {
 3         var leftMax = Int.min
 4         for i in 0..<A.count - 1 {
 5             if A[i] > A[i + 1] && leftMax > A[i + 1] {
 6                 return false
 7             }
 8             leftMax = max(leftMax, A[i])
 9         }
10         var rightMin = Int.max
11         for i in (1..<A.count).reversed() {
12             if A[i - 1] > A[i] && rightMin < A[i - 1] {
13                 return false
14             }
15             rightMin = min(rightMin, A[i])
16         }
17         return true
18     }
19 }

656ms

 1 class Solution {
 2     func merge_sort(_ array: inout[Int], _ global: inout Int, _ local: inout Int, _ start: Int, _ end: Int) {
 3         //print("[\(start), \(end))")
 4         guard end - start > 1 else { return }
 5         let mid = (start + end) / 2
 6         if mid - 1 >= start {
 7             local += array[mid - 1] > array[mid] ? 1 : 0
 8         }
 9         merge_sort(&array, &global, &local, start, mid)
10         merge_sort(&array, &global, &local, mid, end)
11         
12         let copy = Array(array[start..<mid])
13         var i = copy.startIndex, j = mid, k = start
14         while i < copy.endIndex {
15             if j >= end || copy[i] <= array[j] { 
16                 array[k] = copy[i]
17                 i += 1
18             } else {
19                 array[k] = array[j]
20                 j += 1
21                 global += copy.endIndex - i
22             }
23             k += 1
24         }
25     }
26     func isIdealPermutation(_ A: [Int]) -> Bool {
27         var global = 0, local = 0
28         var array = A
29         merge_sort(&array, &global, &local, 0, array.count)
30         return global == local
31     }
32 }

8688ms

 1 class Solution {
 2     func isIdealPermutation(_ A: [Int]) -> Bool {
 3         for i in 0..<A.count-1 {
 4             var k = i - 1
 5             if A[i] <= A[i+1] {
 6                 while k >= 0 {
 7                     if A[k] > A[i+1] { return false }
 8                     k -= 1
 9                 }
10             }
11             else {
12                 while k >= 0 {
13                     if A[k] >= A[i] { return false }
14                     if A[k] < A[i] && A[k] > A[i+1] { return false }
15                     k -= 1
16                 }
17             }
18         }
19         return true
20     }
21 }
相關文章
相關標籤/搜索