 # minimum-cost-to-move-chips-to-the-same-position.swift

Jan 22nd, 2022 (edited)
2,023
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import Foundation
2.
3. // 1217. Minimum Cost to Move Chips to The Same Position
4. // https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/
5. // https://github.com/asahiocean
6.
7. class Solution {
8.     func minCostToMoveChips(_ position: [Int]) -> Int {
9.         var even = 0, odd = 0
10.         for p in position { p % 2 == 0 ? (even += 1) : (odd += 1) }
11.         return odd > even ? even : odd
12.     }
13. }
14.
15. // MARK: - Test cases -
16.
17. // Result: Executed 3 tests, with 0 failures (0 unexpected) in 0.007 (0.010) seconds
18.
19. import XCTest
20.
21. class Tests: XCTestCase {
22.
23.     private let solution = Solution()
24.
25.     // First step: Move the chip at position 3 to position 1 with cost = 0.
26.     // Second step: Move the chip at position 2 to position 1 with cost = 1.
27.     // Total cost is 1.
28.     func test0() {
29.         let value = solution.minCostToMoveChips([1,2,3])
30.         XCTAssertEqual(value, 1)
31.     }
32.
33.     // We can move the two chips at position  3 to position 2. Each move has cost = 1. The total cost = 2.
34.     func test1() {
35.         let value = solution.minCostToMoveChips([2,2,2,3,3])
36.         XCTAssertEqual(value, 2)
37.     }
38.
39.     func test2() {
40.         let value = solution.minCostToMoveChips([1,1000000000])
41.         XCTAssertEqual(value, 1)
42.     }
43. }
44.
45. Tests.defaultTestSuite.run()
RAW Paste Data Copied