diff --git a/contains-duplicate/yhkee0404.kt b/contains-duplicate/yhkee0404.kt new file mode 100644 index 000000000..0e8b6cc12 --- /dev/null +++ b/contains-duplicate/yhkee0404.kt @@ -0,0 +1,6 @@ +class Solution { + fun containsDuplicate(nums: IntArray): Boolean { + return nums.toSet() + .size != nums.size + } +} diff --git a/house-robber/yhkee0404.rs b/house-robber/yhkee0404.rs new file mode 100644 index 000000000..aa5026e24 --- /dev/null +++ b/house-robber/yhkee0404.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let mut dp = vec![0; nums.len() + 1]; + dp[1] = nums[0]; + for i in 2..dp.len() { + dp[i] = dp[i - 1].max(dp[i - 2] + nums[i - 1]); + } + return *dp.last() + .unwrap_or(&0) + } +} diff --git a/longest-consecutive-sequence/yhkee0404.go b/longest-consecutive-sequence/yhkee0404.go new file mode 100644 index 000000000..95e261ee1 --- /dev/null +++ b/longest-consecutive-sequence/yhkee0404.go @@ -0,0 +1,22 @@ +func longestConsecutive(nums []int) int { + ans := 0 + sort.Ints(nums) + i := 0 + for i != len(nums) { + j := i + 1 + k := 0 + for j != len(nums) { + diff := nums[j] - nums[j - 1] + if diff > 1 { + break + } + j++ + if diff == 0 { + k++ + } + } + ans = max(ans, j - i - k) + i = j + } + return ans +} diff --git a/top-k-frequent-elements/yhkee0404.scala b/top-k-frequent-elements/yhkee0404.scala new file mode 100644 index 000000000..06fa6a9c8 --- /dev/null +++ b/top-k-frequent-elements/yhkee0404.scala @@ -0,0 +1,11 @@ +object Solution { + def topKFrequent(nums: Array[Int], k: Int): Array[Int] = { + return nums.groupBy(identity) + .view.mapValues(_.size) + .toSeq + .sortBy(- _._2) + .take(k) + .map(_._1) + .toArray + } +} diff --git a/two-sum/yhkee0404.go b/two-sum/yhkee0404.go new file mode 100644 index 000000000..f223dc396 --- /dev/null +++ b/two-sum/yhkee0404.go @@ -0,0 +1,11 @@ +func twoSum(nums []int, target int) []int { + indices := make(map[int]int) + for i, num := range nums { + j, ok := indices[target - num] + if ok { + return []int{j, i} + } + indices[num] = i + } + return []int{} +}