This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func repeatedNTimesII(_ A: [Int]) -> Int { | |
| var hash = [Int:Int]() | |
| for num in A { | |
| if let val = hash[num] { | |
| hash[num] = val + 1 | |
| } else { | |
| hash[num, default: 0] += 1 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func repeatedNTimes(_ A: [Int]) -> Int { | |
| var set = Set<Int>() | |
| for num in A { | |
| if set.contains(num) { | |
| return num | |
| } | |
| set.insert(num) | |
| } | |
| return -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| trip[i] = [num_passengers, start_location, end_location] | |
| Intuition | |
| Since we only have 1,001 stops, we can just figure out how many people get it and out in each location. | |
| Solution | |
| Process all trips, adding passenger count to the start location, and removing it from the end location. | |
| After processing all trips, a positive value for the specific location tells that | |
| we are getting more passengers; negative means more empty seats. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func corporateFlightBooking(bookings: [[Int]], n: Int) -> [Int]{ | |
| if n == 0 { | |
| return bookings[0] | |
| } | |
| var seats = Array(repeating: 0, count: n) | |
| for booking in bookings { | |
| for i in booking[0]...booking[1] { | |
| seats[i-1] += booking[2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func sortArrayByParity(_ A: [Int]) -> [Int] { | |
| var evens = [Int]() | |
| var odds = [Int]() | |
| for i in 0..<A.count { | |
| if A[i] % 2 == 0 { | |
| evens.append(A[i]) | |
| } else { | |
| odds.append(A[i]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func sortArrayByParity(_ A: [Int]) -> [Int] { | |
| var nums = A | |
| var idx = 0 | |
| for i in 0..<nums.count { | |
| if A[i] % 2 == 0 { | |
| nums[idx] = A[i] | |
| idx += 1 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //There are 4 cases for (A[i] % 2, A[j] % 2): | |
| //If it is (0, 1), then everything is correct: i++ and j += 1. | |
| //If it is (1, 0), we swap them so they are correct, then continue. | |
| //If it is (0, 0), only the i place is correct, so we i += 1 and continue. | |
| //If it is (1, 1), only the j place is correct, so we j -= 1 and continue. | |
| func sortArrayByParity(A: inout [Int]) { | |
| var i = 0 | |
| var j = A.count-1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func flipInvertImage(_ array: [[Int]]) -> [[Int]] { | |
| var array2d = array | |
| //flip | |
| for i in 0..<array.count { | |
| let subarray = array2d[i] //[[1,2,3,4], [5,6,7,8], [9,10,11,12]] here each(3) column has 4 rows | |
| for j in 0..<subarray.count { | |
| array2d[i][j] = array2d[i][j] == 0 ? 1 : 0 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public func smallestPositive(array : inout [Int]) -> Int { | |
| var dict = [Int:Bool]() | |
| for number in array { | |
| dict[number] = true | |
| } | |
| var i = 1 | |
| while true { | |
| if dict[i] == nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| var ans = 0 | |
| func rangeSumBST(_ root: TreeNode?, _ left: Int, _ right: Int) -> Int { | |
| guard let root = root else { | |
| return 0 | |
| } | |
| dfs(root, left, right) |