본문 바로가기

Computer Science/Algorithm with Code

(8)
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, ..
Hacker Rank_2/54 tests (Easy, Min-Max Sum) While solving basic problems from the beginning, I should also practice summarizing problems written in English.And plan to organize Swift grammar thoroughly starting from the basics.Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two spa..
Hacker Rank_1/54 tests (Easy, Plus Minus) Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute errors of up to 10(-4) are acceptable. Examplearr = [1, 1, -1, -1, 0]Results ..
Updated_Hacker Rank (Intermediate, Equal) Dynamic ProgrammingDivide complex problems into simpler sub-problems. - Tabulation (Bottom-Up) - Iteration(반복문) : For a large set of inputs  →  Make the results one by one.   - Memoization (Top/Deep-Down): Recursion(재귀함수) : For a small set of inputs  →  Save each result to use next. [Problem Summary]▪︎ Each colleague has some chocolates (The number of chocolates each colleague has initially ▪︎ ..