Leetcode with Swift - Medium Difficulty

Panther·2021년 10월 2일
0

Algorithm Practice

목록 보기
3/3
post-custom-banner
  1. Partition Array for Maximum Sum

1877. Minimize Maximum Pair Sum in Array

Source: https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/

Difficulty: Medium

func minPairSum(_ nums: [Int]) -> Int {

    let halfCount = (nums.count+1)/2
    let num = nums.sorted()
    let firstHalf = num[0..<halfCount].sorted()
    let secondHalf = num[halfCount..<nums.count].sorted(by: >)

    var temp = [Int]()
    
    for i in 0..<halfCount {
        temp.append(firstHalf[i] + secondHalf[i])
    }
        
    return temp.max()!
        
}

1823. Find the Winner of the Circular Game

Source: https://leetcode.com/problems/find-the-winner-of-the-circular-game/

Difficulty: Medium

func findTheWinner(_ n: Int, _ k: Int) -> Int {

    var arr = Array(1...n)
    if k == 1 {
        return arr[arr.count-1]
    } else {
        while arr.count != 1 {
            for i in 1...(k-1) {
                arr.append(arr.removeFirst())
            }
            arr.removeFirst()
        }
    }
    
    return arr[0]
    
}

347. Top K Frequent Elements

Source: https://leetcode.com/problems/top-k-frequent-elements/

Difficulty: Medium

func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
        
    var dict = [Int:Int]()
    var result: [Int] = []
    
    for i in nums {
        dict[i, default: 0] += 1
    }
        
    let sorted = dict.sorted { $0.1 > $1.1 }
        
    for i in 0..<k {
        result.append(sorted[i].key)
    }
        
    return result
        
}

49. Group Anagrams

Source: https://leetcode.com/problems/group-anagrams/

Difficulty: Medium

func groupAnagrams(_ strs: [String]) -> [[String]] {

    var temp = [String: [String]]()
    
    for i in 0..<strs.count {
        let mytemp = String(strs[i].sorted())
        temp[mytemp, default: []] += [strs[i]]
    }
        
    return Array(temp.values)
}

215. Kth Largest Element in an Array

Source: https://leetcode.com/problems/kth-largest-element-in-an-array/

Difficulty: Medium

func findKthLargest(_ nums: [Int], _ k: Int) -> Int {

    let sortedArray = nums.sorted(by: >)
    return sortedArray[k-1]
    
}

781. Rabbits in Forest

Source: https://leetcode.com/problems/rabbits-in-forest/

Difficulty: Medium

func numRabbits(_ answers: [Int]) -> Int {

    var setForDictKey = Set(answers)
    var result = 0
    var dict = [(Int, Int)]()
    
    for i in setForDictKey {
        let count = answers.filter { $0 == i }.count
        dict.append((i, count))
    }


    for i in dict {
        result += i.1 / (i.0 + 1) * (i.0 + 1)
        if i.1 % (i.0 + 1) > 0 {
            result += i.0 + 1
        } else {
            
        }
    }
    
    return result
    
}

189. Rotate Array

Source: https://leetcode.com/problems/rotate-array/

Difficulty: Medium

func rotate(_ nums: inout [Int], _ k: Int) {

    if k % nums.count > 0 {
        for i in 1...(k % nums.count) {
            nums.insert(nums.removeLast(), at: 0)
        }
    } else if nums.count == 1 {
            
    } else if k % nums.count == 0 {
        
    } else {
        
    }
    
}

1551. Minimum Operations to Make Array Equal

Source: https://leetcode.com/problems/minimum-operations-to-make-array-equal/

Difficulty: Medium

func minOperations(_ n: Int) -> Int {
        
    var arr = [Int]()
        
    for i in 0...(n - 1) {
        arr.append(2 * i + 1)
    }
        
    var result = 0
        
    if arr.count % 2 == 0 {
        let middleInd = arr.count / 2
        let target = (arr[middleInd] + arr[middleInd - 1]) / 2
        for i in arr[middleInd...] {
            result += i - target
        }
    } else if arr.count % 2 == 1 {
        let middleInd = arr.count / 2
        let target = arr[middleInd]
        for i in arr[(middleInd + 1)...] {
            result += i - target
        }
    }
        
    return result
        
}

442. Find All Duplicates in an Array

Source: https://leetcode.com/problems/find-all-duplicates-in-an-array/

Difficulty: Medium

// Solution 1
func findDuplicates(_ nums: [Int]) -> [Int] {
        
    var dict = [Int:Int]()
    var result = [Int]()
        
    for i in nums {
        dict[i, default: 0] += 1
    }
        
    for (key, value) in dict {
        if value > 1 {
            result.append(key)
        }
    }
        
    return result
        
}
 
// Solution 2
func findDuplicates(_ nums: [Int]) -> [Int] {
        
    let sorted = nums.sorted(by: < )
    
    var left = 0
    var right = 1
    var result = [Int]()
        
    while right < sorted.count {
        if sorted[left] == sorted[right] {
            result.append(sorted[right])
        }
        left += 1
        right += 1
    }
        
    return result
        
}

260. Single Number III

Source: https://leetcode.com/problems/single-number-iii/

Difficulty: Medium

func singleNumber(_ nums: [Int]) -> [Int] {
        
    var dict = [Int:Int]()
    var result = [Int]()
        
    for i in nums {
        dict[i, default: 0] += 1
    }
        
    for (key, value) in dict {
        if value == 1 {
            result.append(key)
        }
    }
        
    return result
    
}

1780. Check if Number is a Sum of Powers of Three

Source: https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/

Difficulty: Medium

func checkPowersOfThree(_ n: Int) -> Bool {
        
    var origin = n
    var temp = [Int]()
        
    while origin != 0 {
        temp.append(origin % 3)
        origin = origin / 3
    }
        
    return !temp.contains(2)
        
}

137. Single Number II

Source: https://leetcode.com/problems/single-number-ii/

Difficulty: Medium

func singleNumber(_ nums: [Int]) -> Int {
        
    var dict = [Int:Int]()
    var result = Int()
        
    for i in nums {
        dict[i, default: 0] += 1
    }
        
    for (key, value) in dict {
        if value == 1 {
            result = key
        }
    }
        
    return result
        
}

912. Sort an Array

Source: https://leetcode.com/problems/sort-an-array/

Difficulty: Medium

func sortArray(_ nums: [Int]) -> [Int] {

    return nums.sorted(by: < )
    
}

1492. The kth Factor of n

Source: https://leetcode.com/problems/the-kth-factor-of-n/

Difficulty: Medium

func kthFactor(_ n: Int, _ k: Int) -> Int {
        
    var temp = [Int]()
        
    for i in 1...n {
        if n % i == 0 {
            temp.append(i)
        }
    }
        
    if temp.count > k - 1 {
        return temp[k - 1]
    } else {
        return -1
    }
        
}

451. Sort Characters By Frequency

Source: https://leetcode.com/problems/sort-characters-by-frequency/

Difficulty: Medium

func frequencySort(_ s: String) -> String {
        
    var dict = [Character:Int]()
        
    for i in s {
        dict[i, default: 0] += 1
    }
        
    let sorted = dict.sorted{ $0.1 > $1.1 }
    
    var result = ""
        
    for (key, value) in sorted {
        for i in 1...value {
            result.append(key)
        }
    }
        
    return result
        
}
post-custom-banner

0개의 댓글