Instantly share code, notes, and snippets.

# Marcus Young UNISERVO1

• Prosper Marketplace
• Mountain View, CA
Created August 7, 2020 04:14
August LeetCoding Challenge Week 1: Day 6 (using negative substitution for O(n))
View find_all_array_dups_using_negs.kt
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.abs class Solution { fun findDuplicates(nums: IntArray): List { fun twiceFound(acc: Pair, MutableList>, num: Int): Pair, MutableList> { val (uniques, negs) = acc return if (negs[abs(num) - 1] < 0) { uniques.plus(num) to negs } else { negs[abs(num) - 1] = -1 * negs[abs(num) - 1]
Created August 6, 2020 22:22
August LeetCoding Challenge Week 1: Day 6
View find_all_array_dups.kt
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
 class Solution { fun findDuplicates(nums: IntArray): List { fun twiceFound(uniques: MutableMap, num: Int): MutableMap { uniques.set(num, uniques.getOrPut(num){ 0 } + 1) return uniques } return nums.fold(mutableMapOf(), { t, num -> twiceFound(t, num) }).toMap().filterValues{ it == 2 }.keys.toList() }
Created August 4, 2020 23:06
August LeetCoding Challenge Week 1: Day 4
View power_of_four.kt
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
 class Solution { val mask: Int = (0..31 step 2).fold(0){ acc, v -> acc + (1 shl v)} fun isPowerOfFour(num: Int): Boolean { return (num - 1) and num == 0 && (mask and num) > 0 } }
Last active August 4, 2020 02:14
August LeetCoding Challenge Week 1: Day 3
View is_palindrome.kt
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
 class Solution { fun isPalindrome(s: String): Boolean { val targetString: String = s.filter{ it.isLetterOrDigit() }.toLowerCase() return targetString == targetString.reversed() } }
Last active August 3, 2020 11:58
August LeetCoding Challenge Week 1: Day 2
View my_hash_set.kt
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
 class MyHashSet() { val numBuckets: Int = 97 val buckets = List>(numBuckets){ mutableSetOf() } val hashFunc : (Int) -> Int = { key: Int -> key % numBuckets } fun add(key: Int) { buckets.get(hashFunc(key)).add(key) }
Last active August 3, 2020 11:57
August LeetCoding Challenge Week 1: Day 1
View detect_capital.kt
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
 class Solution { fun detectCapitalUse(word: String): Boolean { return word.all{ it.isLowerCase() } || word.all{ it.isUpperCase() } || (word.first().isUpperCase() && word.drop(1).all{ it.isLowerCase() }) } }