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 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] | |
} | |
} |
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 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) { |
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 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) | |
} |
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 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++ |
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 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). |
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 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" |
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
/* | |
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) { |
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
/* | |
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 |
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
/* | |
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) { |
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
/* | |
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 | |
} |
NewerOlder