Skip to content

Instantly share code, notes, and snippets.

View Turskyi's full-sized avatar
💭
Everything is simple.

Dmytro Turskyi Turskyi

💭
Everything is simple.
View GitHub Profile
@Turskyi
Turskyi / RemoveDuplicates.kt
Last active July 6, 2022 22:41
Given a sorted integer array, nums, remove duplicates from the array in-place such that each element only appears once. Once you’ve removed all the duplicates, return the length of the new array.
fun removeDuplicates(nums: IntArray): Int {
var counter = 0
for(i: Int in 1 until nums.size) {
// two pointers. one always at least one step back. first one is comparing two digits from index 0 and 1
if(nums[counter] != nums[i]) {
counter++
nums[counter] = nums[i]
}
}
@Turskyi
Turskyi / BottomLeftTreeValue.kt
Created May 5, 2022 19:52
Given a binary tree, return the bottom-left most value.
fun findBottomLeftValue(root: TreeNode?): Int {
val queue: Queue<TreeNode?> = LinkedList<TreeNode?>()
queue.offer(root)
var node: TreeNode? = queue.peek()
while (queue.isNotEmpty()) {
node = queue.poll()
if (node?.right != null) {
queue.add(node.right)
}
if (node?.left != null) {
@Turskyi
Turskyi / InsertInBst.kt
Created May 5, 2022 18:41
Given the reference to a binary search tree and a value to insert, return a reference to the root of the tree after the value has been inserted in a position that adheres to the invariants of a binary search tree.
fun insertIntoBST(
root: TreeNode?, value: Int
): TreeNode? {
if (root == null) {
return TreeNode(value)
} else if (root.`val` < value) {
root.right = insertIntoBST(root.right, value)
} else if (root.`val` > value) {
root.left = insertIntoBST(root.left, value)
}
@Turskyi
Turskyi / MaxChain.kt
Created May 5, 2022 16:56
You are given n pairs of numbers and asked to form a chain. Two pairs of numbers can create a link in the chain if the second number in the first pair is less than the first number in the second pair. Return the length of the longest chain you can form.
fun findLongestChain(pairs: Array<IntArray>): Int {
return if (pairs.isEmpty()) {
0
} else {
var count: Int = 1
pairs.sortWith(compareBy { it[1] })
var end: Int = pairs[0][1]
for (i: Int in 1..pairs.lastIndex) {
if (end < pairs[i][0]) {
count++
@Turskyi
Turskyi / ClosePoints.kt
Created May 4, 2022 14:19
Given a list of points, return the k closest points to the origin (0, 0).
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
return points.sortedWith(comparator = compareBy { it[0] * it[0] + it[1] * it[1] }).take(k).toTypedArray()
}
/*
Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).
The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
@Turskyi
Turskyi / LuckySeven.kt
Last active May 5, 2022 19:25
Given an integer, num, return its base seven representation as a string.
fun convertToBase7(num: Int): String {
return num.toString(radix = 7)
}
/*
Given an integer num, return a string of its base 7 representation.
Example 1:
Input: num = 100
Output: "202"
@Turskyi
Turskyi / FindAllDuplicatedInArray.kt
Created April 27, 2022 00:03
Given an array of integers, nums, each element in the array either appears once or twice. Return a list containing all the numbers that appear twice.
/*
Given an integer array nums of length n where all the integers of nums are in the range [1, n]
and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n) time and uses only constant extra space.
* */
// Input: nums = [1,1,2] Output: [1]
fun findDuplicates(nums: IntArray): List<Int> {
val setToCheck: MutableSet<Int> = mutableSetOf()
val duplicates: MutableList<Int> = mutableListOf()
for (element: Int in nums) {
@Turskyi
Turskyi / RomanNumerals.kt
Created April 26, 2022 17:48
Given a roman numeral, convert it to an integer.
/*
Given a string, s, that represents a Roman numeral, return its associated integer value.
Ex: s = "DCLII", return 652.
* */
fun romanToInt(s: String): Int {
val map: MutableMap<Char, Int> = mutableMapOf(
'I' to 1, 'V' to 5, 'X' to 10, 'L' to 50, 'C' to 100, 'D' to 500, 'M' to 1000
)
var number = 0
var last = 1000
@Turskyi
Turskyi / KthSmallestInBst.kt
Last active May 5, 2022 19:45
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
/*
Given the reference to a binary search tree, return the kth the smallest value in the tree.
* */
fun kthSmallest(root: TreeNode?, k: Int): Int {
val nodeQue: Queue<TreeNode?> = LinkedList<TreeNode?>()
nodeQue.offer(root)
val values: HashSet<Int> = HashSet()
while (nodeQue.isNotEmpty()) {
val currentNode: TreeNode? = nodeQue.poll()
if (currentNode != null) {
@Turskyi
Turskyi / AddDigits.kt
Created April 23, 2022 18:36
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
/*
Given a non-negative integer, num, repeatedly add all its digits until it has a single digit remaining and return it.
Ex:
num = 123, return 6 (1 + 2 + 3 = 6)
Ex:
num = 8353, return 1 (8 + 3 + 5 + 3 = 19 = 1 + 9 = 10 = 1 + 0 = 1).
* */
fun addDigits(num: Int): Int { // 23
return (num - 1) % 9 + 1 // 23 - 1 = 22 // 22 % 9 = 4 // 4 + 1 = 5
}