Skip to content

[김한기] - Week 4 solutions #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions counting-bits/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Solution do
@spec count_bits(n :: integer) :: [integer]
def count_bits(n) do
Enum.map(0..n, fn i ->
Integer.digits(i, 2) |> Enum.count(&(&1 != 0))
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간 복잡도가 $O(n * log n)$이 될 것 같은데, 빨리 제출하셨으니 $O(n)$ 솔루션도 고민해보시면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 사실 시간복잡도 공간복잡도 개념이 익숙치 않은데 공부해야겠네요 ^^;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코딩 인터뷰에서 시공간 복잡도 분석이 필요한 걸로 알고 있는데 맞을까요?

end)
end
end
31 changes: 31 additions & 0 deletions group-anagrams/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Solution do
@spec group_anagrams(strs :: [String.t]) :: [[String.t]]
def group_anagrams(strs) do
Enum.group_by(strs, &(String.codepoints(&1) |> Enum.sort)) |> Map.values
end
end

# 최초엔 아래 코드로 시도했으나 strs.length가 커지면 시간초과...
#
# defmodule Solution do
# @spec group_anagrams(strs :: [String.t]) :: [[String.t]]
# def group_anagrams(strs) when length(strs) < 2, do: [strs]
# def group_anagrams([], res), do: res

# def group_anagrams([head|tail], res \\ []) do
# split_sort_head = split_sort(head)
# anagrams = Enum.reduce(tail, [head], fn x, acc ->
# if split_sort(x) == split_sort_head do
# acc ++ [x]
# else
# acc
# end
# end)

# group_anagrams(tail -- anagrams, [anagrams|res])
# end

# defp split_sort(str) do
# String.graphemes(str) |> Enum.sort_by(&(&1), :asc)
# end
# end
6 changes: 6 additions & 0 deletions missing-number/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Solution do
@spec missing_number(nums :: [integer]) :: integer
def missing_number(nums) do
(0..length(nums) |> Enum.to_list) -- nums |> Enum.at(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

신기한 연산자가 많네요. 언어별 특징이 보이는 것 같아 보는 맛이 있네요 ㅎㅎ

end
end
6 changes: 6 additions & 0 deletions number-of-1-bits/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Solution do
@spec hamming_weight(n :: integer) :: integer
def hamming_weight(n) do
Integer.to_string(n, 2) |> String.graphemes |> Enum.count(& &1 != "0")
end
end
6 changes: 6 additions & 0 deletions reverse-bits/han.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @param {Integer} n, a positive integer
# @return {Integer}
def reverse_bits(n)
# 이유는 모르겠으나 문제 설명과는 달리 leetcode 루비 테스트코드에선 n이 10진수 Integer로 들어오네요.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 언어에서도 마찬가지에요. 문제가 좀 오해가 소지가 있게 설명이 되어 있는 것 같아요 😅

n.to_s(2).rjust(32,'0').reverse.to_i(2)
end