본문 바로가기

전체 글

(42)
[Swift] Collection Types & Higher Order Functions [Collection Types] Array(Ordered)var someInts: [Int] = []// var someIntss: [[Int]] = Array(repeating:Array(repeating:0, count:5), count:5)// 5x5// var someIntss: [[Int]] = Array(repeating:[Int](), count:5)// 5x5// String을 제외하고는, subscript로 접근 가능var arr = [[1,2,3],[2,3],[4]]var flattenInts = arr.flatMap {$0} // 2차원 -> 1차원으로 병합 // return optional// Swift 4.1 부터 -> flatMap {$0} .compactMap {$0} // ..
[HackerRank] Preparation_Kit (15. Mark and Toys) [Sorting] func maximumToys(prices: [Int], k: Int) -> Int { var ans = 0 let sortedPrices = prices.sorted() var budget = k for p in sortedPrices { if budget > p { budget -= p ans += 1 } } return ans} https://www.hackerrank.com/challenges/mark-and-toys/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=sorting
[HackerRank] Preparation_Kit (11. Two Strings) [Dictionaries & Hashmaps] Given two strings, determine if they share a common substring. A substring may be as small as one character. Examplea1 = 'and', a2 = 'art'These share the common substring a. a1 = 'be', a2 = 'cat'These do not share a substring. Function DescriptionComplete the function twoStrings in the editor below. twoStrings has the following parameter(s): string s1: a stringstring s2: another string Returnsst..
[HackerRank] Preparation_Kit (10. Ransom Note) [Dictionaries & Hashmaps] Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use only whole words available in the magazine. He cannotuse substrings or conc..
[HackerRank] Preparation_Kit (08. Minimum Swaps 2) [Array] You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order.Examplearr = [7, 1, 3, 2, 4, 5, 6]Perform the following steps:i arr swap (indices)0 [7, 1, 3, 2, 4, 5, 6] swap (0,3)1 [2, 1, 3, 7, 4,..
[HackerRank] Preparation_Kit (06. Left Rotation) [Array] A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 6, 7]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.Given an array a of n integers and a number, d, perform d left rotations on the ..
[HackerRank] Preparation_Kit (05. 2D Array - DS) [Array] Given a 6x6 2D Array, arr:1 1 1 0 0 00 1 0 0 0 01 1 1 0 0 00 0 0 0 0 00 0 0 0 0 00 0 0 0 0 0An hourglass in  is a subset of values with indices falling in this pattern in 's graphical representation:a b c de f gThere are 16 hourglasses in arr. An hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum. The ar..
[HackerRank] Preparation_Kit (04. Repeated String) There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n letters of the infinite string.Examples = 'abcac'n = 10The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.Function DescriptionComplete the repeated..