본문 바로가기

Computer Science

(11)
2. Queue (Linear Data Structure-FIFO) public struct Queue { fileprivate var array = [T?]() fileprivate var head = 0 public var isEmpty: Bool { return count == 0 } public var count: Int { return array.count - head // 앞쪽 시작점(Starting Point) = head } public mutating func enqueue(_ element: T) { array.append(element) } public mutating func dequeue() -> T? { guard head 50 && percentage > 0.25 { // 상황에 따라 비우는 기..
1. Stack (Linear Data Structure-LIFO) * Module(import, Package(Source-Files/Code) Units)* Access Control (Encapsulation)- open(상속/오버라이드)/public: 모듈 외부 사용> internal(default): 특정 모듈(앱) 내 사용> firePrivate(같은 소스 파일 내) / private: 같은 클래스, 구조체 내 사용// mutating 키워드: 인스턴스에서 내부 값 수정 가능// Generic T(Type Parameter: Any)public struct Stack { fileprivate var arr = [T]() public var isEmpty: Bool { return arr.isEmpty } public var count: Int { ..
Data Structure & Algorithm Overview [Data Structure Overview] (Depending on programming language)+ Heaps → Priority Queue[Algorithm Overview] Examples)Binary Search (이진 탐색)DFS, BFS (깊이/너비 우선 탐색)Greedy (탐욕)Brute-Force Search (완전 탐색) -백트래킹/재귀/순열-조합 Advanced Simulation (구현) - 문자열/시나리오Sort (정렬) - Insertion / Counting / Quick / Selection / Heap / Merge최단 경로  - Floyd Warshall / Dijkstra's / Bellman FordKruskal's / Prim (크루스칼/프림)Prefix ..
Leet Code (2405. Amazon Spring '23 HF) 2405. Optimal partition of String Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once. Return the minimum number of substrings in such a partition.Note that each character should belong to exactly one substring in a partition. Example 1:Input: s = "abacaba"Output:..
Leet Code (1492. Amazon Spring '23 HF) 1492. The kth Factor of n You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.: The kth factor (약수) of the n. * Check The Test_Cases (Exceptions)Could you solve this problem in less than O(n) comp..
Hacker Rank_5/54 tests (Easy, Lonely Integer) Given an array of integers, where all elements but one occur twice, find the unique element.Examplea = [1, 2, 4, 3, 3, 2, 1]The unique element is 4.func lonelyinteger(a: [Int]) -> Int { var dics = [Int : Int]() for i in a { if dics[i] == nil { dics[i] = 1 } else { dics[i]! += 1 } } return dics.first{ $0.value == 1 }!.key}
Hacker Rank_4/54 tests (Easy, Sparse Array) There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Examplequeries = [a', 'b', c'd']strings = [a', b, c'd']return => [1, 0, 1] func matchingStrings(strings: [String], queries: [String]) -> [Int] { // Hashmap - Dictionary // Arr - size/order //var dic..
Hacker Rank_3/54 tests (Easy, Time Conversion) Given a time in -hour AM/PM format, convert it to military (24-hour) time.func timeConversion(s: String) -> String { //split / components / subscript / index var ans = s.map { $0 } // or copy-inout if s.contains("P") { ans.replaceSubrange(0...1, with: String(Int(s.split(separator: ":")[0])!+12)) if ans[0] == "2" && ans[1] == "4" { ans.replaceSubrange(0...1, ..