본문 바로가기
728x90

알고리즘99

Codesignal - areSimilar Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays. Given two arrays a and b, check whether they are similar. Example For a = [1, 2, 3] and b = ß[1, 2, 3], the output should be areSimilar(a, b) = true. The arrays are equal, no need to swap any elements. For a = [1, 2, 3] and b = [2, 1, 3], the output should be areSimila.. 2021. 4. 29.
Codesignal - areEquallyStrong Call two arms equally strong if the heaviest weights they each are able to lift are equal. Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms. Given your and your friend's arms' lifting capabilities find out if you two are equally strong. Example For yourLeft = 10, yourRight = 15, friends.. 2021. 4. 29.
Codesignal - alternatingSums Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1*again, the fourth into *team 2, and so on. You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is t.. 2021. 4. 29.
Codesignal - alphabeticShift Given a string, your task is to replace each of its characters by the next one in the English alphabet; i.e. replace a with b, replace b with c, etc (z would be replaced by a). Example For inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz". 설명 inputString의 각 문자를 알파벳 순서에 맞게 다음 알파벳으로 바꿔라 풀이 문자열의 각 문자들을 인코딩한 값에 1을 더하여 아스키코드에 맞는 문자를 리턴 func alphabeticShift(inputString.. 2021. 4. 29.
Codesignal - almostIncreasingSequence Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing. Example For sequence = [1, 3, 2, 1], the output sh.. 2021. 4. 29.
Codesignal - allLongestString Given an array of strings, return another array containing all of its longest strings. Example For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be allLongestStrings(inputArray) = ["aba", "vcd", "aba"]. 설명 인풋 스트링 배열의 원소 중 길이가 가장 긴 원소만 리턴 풀이 max()이용하여 길이가 가장 긴 원소를 찾아서 필터링 func allLongestStrings(inputArray: \[String\]) \-> \[String\] { let max \= inputArray.max(by: {$0.count < .. 2021. 4. 27.
Codesignal - adjacent Elements Product Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. Example. For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21. 7 and 3produce the largest product. Input/Output [execution time limit] 20 seconds (swift) [input] array.integer inputArrayGuaranteed constraints: 2 ≤ inputArray.length ≤.. 2021. 4. 27.
Codesignal - addBorder Given a rectangular matrix of characters, add a border of asterisks(*) to it. Example For picture = ["abc", "ded"] the output should be addBorder(picture) = ["*****", "*abc*", "*ded*", "*****"] 설명 *을 원소 앞뒤에 넣고 배열 처음과 마지막에 문자 길이 만큼 넣는다 풀이 pictrue 원소에 먼저 문자 앞뒤에 *을 넣어주고 문자 길이 만큼 picture 맨 앞뒤에 넣어줄 필요한 길이의 별을 star에 저장하고 picture 앞뒤에 넣어줌 func addBorder(picture: [String]) -> [String] { var matrix = pict.. 2021. 4. 27.
Codesignal - absoluteValuesSumMinimization Given a sorted array of integers a, your task is to determine which element of a is closest to all other values of a. In other words, find the element x in a, which minimizes the following sum: abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x) (where abs denotes the absolute value) If there are several possible answers, output the smallest one. Example For a = [2, 4, 7], the output .. 2021. 4. 26.