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 / Transpose.kt
Created April 20, 2022 21:29
Given a 2D integer array matrix, return the transpose of matrix. The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
/*
Given a 2D matrix, return the matrix transposed.
Note: The transpose of a matrix is an operation that flips each value in the matrix across its main diagonal.
Ex: Given the following matrix…
matrix = [
[1, 2],
[3, 4]
]
@Turskyi
Turskyi / CountingWords.kt
Created April 18, 2022 02:48
Given a list of words, return the top k frequently occurring words.
/*
Given an array of strings words and an integer k, return the k most frequent strings.
["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
* */
fun topKFrequent(words: Array<String>, k: Int): List<String> {
return words.groupingBy { it }.eachCount()
.toList()
.sortedBy { (s, _) -> s }
.sortedByDescending { (_, v) -> v }
@Turskyi
Turskyi / GoodPair.kt
Created April 18, 2022 02:02
Return two unique indices (one based) such that the values at those indices sums to the given target.
/*
Given an array of integers nums and an integer target,
return indices of the two numbers such that they add up to target.
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
* */
fun twoSum(nums: IntArray, target: Int): IntArray {
val map: MutableMap<Int, Int> = mutableMapOf()
@Turskyi
Turskyi / GroupWords.kt
Created April 17, 2022 22:56
Group the anagrams together.
/*
Given a list of strings, return a list of strings containing all anagrams grouped together.
strs = ["car", "arc", "bee", "eeb", "tea"],
return
[
["car","arc"],
["tea"],
["bee","eeb"]
]
@Turskyi
Turskyi / IsPowerOfThree.kt
Created April 17, 2022 22:00
Return whether or not 'n' is a power of three.
/*
Given an integer n, return true if it is a power of three. Otherwise, return false.
An integer n is a power of three, if there exists an integer x such that n == 3x.
Example 1:
Input: n = 27
Output: true
Example 2:
Input: n = 0
@Turskyi
Turskyi / RemoveElement.kt
Created April 17, 2022 21:26
Return the length of the new array.
/*
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
The relative order of the elements may be changed.
Since it is impossible to change the length of the array in some languages,
you must instead have the result be placed in the first part of the array nums.
More formally, if there are k elements after removing the duplicates,
then the first k elements of nums should hold the final result.
It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array.
@Turskyi
Turskyi / AddOne.kt
Created April 16, 2022 02:02
Given an array digits that represents a non-negative integer, add one to the number and return the result as an array.
/*
Given an array digits that represents a non-negative integer, add one to the number and return the result as an array.
Ex: Given the following digits...
digits = [1, 2], return [1, 3].
Ex: Given the following digits...
digits = [9, 9], return [1, 0, 0].
* */
fun plusOne(digits: IntArray): IntArray {
@Turskyi
Turskyi / MergeIntervals.kt
Last active April 15, 2022 21:40
merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
/*
Given an array of intervals where intervals[i] = [starti, endi],
merge all overlapping intervals,
and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
@Turskyi
Turskyi / LinkedListPalindrome.kt
Created April 15, 2022 15:47
Given a reference to a linked list, return whether or not it forms a palindrome.
/*
Given a reference to a linked list, return whether or not it forms a palindrome.
Ex: Given a reference to the following linked list...
head = 1->3->1, return true.
* */
fun isPalindrome(head: ListNode?): Boolean {
var slow: ListNode? = head
@Turskyi
Turskyi / AddStrings.kt
Last active April 14, 2022 00:29
Given two non-negative numbers, num1 and num2 represented as strings, sum the integers together and return the result as a string.
/*
Given two non-negative numbers, num1 and num2 represented as strings, sum the integers together and return the result as a string.
Ex: Given the following values for num1 and num2...
num1 = “2”, num2 = “5”, return “7”.
Ex: Given the following values for num1 and num2...
num1 = “7”, num2 = “95”, return “102”.
* */