This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unilecs | |
fun findMaxArea(walls : IntArray) : Int { | |
var left = 0 | |
var right = walls.size - 1 | |
var maxArea = 0 | |
while (left != right) { | |
maxArea = maxOf(maxArea, minOf(walls[left], walls[right]) * (right - left)) | |
if (walls[left] < walls[right]) left++ else right-- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.math.ceil | |
import kotlin.math.log2 | |
import kotlin.math.min | |
import kotlin.math.pow | |
fun findMinOperationCount(x : Int, y : Int) : Int { | |
val countMultiplyingBy2 = ceil(log2(y.toDouble() / x)).toInt() // посчитали умножения на 2 | |
val multiplierByTwo = 2.toFloat().pow(countMultiplyingBy2).toInt() // во сколько увеличили X умножая его на 2 | |
var diff = x * multiplierByTwo - y // разница, которую нужно покрыть вычитанием единиц | |
var countDecrementByOne = diff / multiplierByTwo // столько вычитаний единиц ДО умножений на 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unilecs | |
fun getLongestHarmonicSubsequence(array : Array<Int>) : Int { | |
val valuesCount = mutableMapOf<Int, Int>() | |
array.forEach { i -> valuesCount[i] = valuesCount.getOrDefault(i,0) + 1 } | |
return valuesCount.keys.maxOf { n -> valuesCount[n]!! + valuesCount.getOrDefault(n + 1, -valuesCount[n]!!) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unilecs | |
import java.lang.Integer.max | |
import java.lang.Integer.min | |
fun getShortestDistanceToChar(s: String, c: Char): IntArray { | |
val length = s.length | |
val result = IntArray(length) { length } | |
var previousSymbolPosition = -length | |
var nextSymbolPosition = s.indexOf(c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tree | |
class BinaryTreeNode <T : Comparable<T>>(var value: T?, var parent: BinaryTreeNode<T>?) { | |
var left: BinaryTreeNode<T>? = null | |
var right: BinaryTreeNode<T>? = null | |
fun toListString() : String { | |
return toListString("").trim() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun isEvenAmountOfDigits(number: Int): Int { | |
if ((number < 1) or (number > 100000)) { | |
return -1 | |
} | |
when(0) { | |
number / 10 -> return 0 | |
number / 100 -> return 1 | |
number / 1000 -> return 0 | |
number / 10000 -> return 1 | |
number / 100000 -> return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
решение в общем виде | |
Обозначим номером 10 того, у кого максимальная зарплата, | |
у 9 меньше, у 8 ещё меньше ... у 1 - минимальная. | |
Тогда сначала повышаем всем, кроме номера 2, пока у 1 и 2 не будет одинаково, | |
потом всем, кроме номера 3, пока у 1, 2 и 3 не будет одинаково, | |
потом всем, кроме 4, пока у 1, 2, 3 и 4 не будет одинаково. И так до 10. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
public class ObjectFiller { | |
public static Object fill(Object object) throws NoSuchFieldException, IllegalAccessException { | |
HashMap<Class, List> hashMap = new HashMap<>(); | |
Class currentClass = object.getClass(); | |
while (currentClass != Object.class) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Сортируем массив, потом проходим по массиву и ищем самый большой интервал, а потом не забываем про интервал "через старт" | |
def task(a, dist): | |
if len(a) == 0: # если питстопов нет, то доехать до них невозможно | |
return None | |
a.sort() # сортируем массив | |
max_interval = 0 | |
for i in range(0, len(a) - 1): # проходим по массиву и если находим интервал больше максимального, то сохраняем его | |
current_interval = a[i + 1] - a[i] | |
if current_interval > max_interval: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Вариант 1: Сортировка пузырьком с флагом так, чтобы сортировались только нули | |
def task148_1(a): | |
fl = True | |
while(fl): # пока есть неотсортированные элементы | |
fl = False | |
for j in range(len(a) - 1): # по всему массиву | |
if a[j] == 0 and a[j + 1] != 0: # если видим ноль, а за ним число, то меняем их местами | |
a[j], a[j + 1] = a[j + 1], a[j] | |
fl = True | |
return a |
NewerOlder