Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark authored May 28, 2024
2 parents 137a017 + 99e7ce5 commit e0508f0
Show file tree
Hide file tree
Showing 29 changed files with 772 additions and 0 deletions.
11 changes: 11 additions & 0 deletions counting-bits/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def countBits(self, n: int) -> List[int]:
counter = [0]

for i in range(1, n + 1):
counter.append(counter[i >> 1] + i % 2)

return counter

## TC: O(n), SC: O(n)
## this question should be under math section tbh
27 changes: 27 additions & 0 deletions counting-bits/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// 338. Counting Bits
// https://leetcode.com/problems/counting-bits/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/05/19.
//

final class Solution {

// MARK: - Time Complexity: O(n), Space Complexity: O(n)

func countBits(_ n: Int) -> [Int] {
var array: [Int] = .init(repeating: 0, count: n + 1)
for i in stride(from: 1, through: n, by: 1) {
array[i] = array[i >> 1] + (i & 1)
}
return array
}

// MARK: - nonzeroBitCount

func countBits2(_ n: Int) -> [Int] {
return (0...n).map(\.nonzeroBitCount)
}

}
65 changes: 65 additions & 0 deletions counting-bits/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
338. Counting Bits
https://leetcode.com/problems/counting-bits/description/
Solution 1:
- Convert the number to binary string
- Sum the number of 1s in the binary string
- Append the sum to the output list
- Return the output list
Time complexity: O(nlogn)
- The for loop runs n times
- The sum function runs O(logn) times
Space complexity: O(n)
- The output list has n elements
class Solution:
def countBits(self, n: int) -> List[int]:
output = []
for i in range(n + 1):
_str = str(bin(i))[2:]
_sum = sum(map(int, _str.strip()))
output.append(_sum)
return output
"""

"""
Solution 2
We can solve this problem with dynamic programming.
1. Initialize output with n elements
2. The first element is 0 because iteration starts from zero.
3. Iterate from 1 to n+1
4. The last digit of each number is 0 for even number 1 for odd number
So add (i & 1) to the output
5. The digits except the last one can be found when the number is divided by two.
Instead for division by two, we can use one step of bit shift to the right.
0 = 00000
1 = 00001
2 = 00010
3 = 00011
4 = 00100
5 = 00101
6 = 00110
7 = 00111
Time complexity: O(n)
- The for loop runs n times
Space complexity: O(n)
- The output list has n elements
"""

from typing import List

class Solution:
def countBits(self, n: int) -> List[int]:
output = [0] * (n+1)

for i in range(1, n+1):
output[i] = output[i >> 1] + (i & 1)

return output
45 changes: 45 additions & 0 deletions counting-bits/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
์ด์ „ ๊ฐ’๋“ค์„ ์žฌํ™œ์šฉํ•œ๋‹ค.
# Approach
<!-- Describe your approach to solving the problem. -->
1. ์—ฃ์ง€ ์ผ€์ด์Šค๋Š” 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
2. 0, 1์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•œ๋‹ค.
3. `>>`์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ + ์ง/ํ™€ ์—ฌ๋ถ€๋กœ ์ธํ•œ 1์„ ๋”ํ•ด์„œ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.
- ์ด์ง„์ˆ˜ `1001`์˜ ๊ฒฝ์šฐ `100` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `1`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
- ์ด์ง„์ˆ˜ `1010`์˜ ๊ฒฝ์šฐ `101` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `0`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

- ์†”๋ฃจ์…˜ ์ฐธ๊ณ : `i & (i-1)` ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ณ„์‚ฐํ•œ๋‹ค.
- 2์˜ ์ œ๊ณฑ์ˆ˜์ธ ๊ฒฝ์šฐ `0`์ด ๋‚˜์™€ 1์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
- ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ์•„์ง์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
:`n`ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋ชจ๋‘๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
:ํฌ๊ธฐ `n`์˜ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
# Code
```go
func countBits(n int) []int {
if n == 0 {
return []int{0}
}
ans := make([]int, n+1, n+1)

ans[0], ans[1] = 0, 1

for i := 2; i <= n; i++ {
ans[i] = ans[i>>1] + i&1
}
return ans
}

func countBitsSolution(n int) []int {
res := make([]int, n+1)
for i := 1; i <= n; i++ {
res[i] = res[i&(i-1)] + 1
}
return res
}
```
26 changes: 26 additions & 0 deletions counting-bits/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var countBits = function (n) {
// Create array which has 0 element length of n
const dp = new Array(n + 1).fill(0);
let offset = 1;

for (let i = 1; i <= n; i++) {
if (offset * 2 === i) offset = i;
dp[i] = 1 + dp[i - offset];
}
return dp;
};

/**
0 -> 0000 -> dp[0] = 0
1 -> 0001 -> dp[1] = 1 + dp[1-1] = 1
2 -> 0010 -> dp[2] = 1 + dp[2-2] = 1
3 -> 0011 -> dp[3] = 1 + dp[3-2] = 2
4 -> 0100 -> dp[4] = 1 + dp[4-4] = 1
5 -> 0101 -> dp[5] = 1 + dp[5-4] = 2
6 -> 0110 -> dp[6] = 1 + dp[6-4] = 2
7 -> 0111 -> dp[7] = 1 + dp[7-4] = 3
8 -> 1000 -> dp[8] = 1 + dp[8-8] = 1
*/

// TC: O(n)
// SC: O(1)
13 changes: 13 additions & 0 deletions counting-bits/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

var countBits = function(n) {
// initialize an array to hold the result.
let ans = new Array(n + 1).fill(0);

// iterate through all numbers from 1 to n.
for (let i = 1; i <= n; i++) {
ans[i] = ans[i >> 1] + (i & 1);
}
return ans;
};
22 changes: 22 additions & 0 deletions group-anagrams/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ans = collections.defaultdict(list)

for s in strs:
ans[str(sorted(s))].append(s)

return list(ans.values())

## TC: O(n * klogk), SC: O(n * k), where n is len(strs) and k is len(longest_s)

# ans = {}

# for s in strs:
# sorted_s = ''.join(sorted(s))

# if sorted_s not in ans:
# ans[sorted_s] = []

# ans[sorted_s].append(s)

# return list(ans.values())
18 changes: 18 additions & 0 deletions group-anagrams/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// 49. Group Anagrams
// https://leetcode.com/problems/group-anagrams/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/05/19.
//

final class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var dictionary: [String: [String]] = [:]
for str in strs {
dictionary[String(str.sorted()), default: []].append(str)
}

return Array(dictionary.values)
}
}
46 changes: 46 additions & 0 deletions group-anagrams/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
49. Group Anagrams
https://leetcode.com/problems/group-anagrams/description/
Solution:
- Create a hash table and a list of counters
- For each string in the input list:
- Sort the string
- If the sorted string is in the hash table:
- Append the string to the corresponding counter list
- Else:
- Add the sorted string to the hash table
- Create a new counter list and append the string
- Return the list of counters
Time complexity: O(nmlogm)
- The for loop runs n times
- The sorted function runs O(mlogm) times
- m is the length of the longest string in the input list
Space complexity: O(n)
- The output list has n elements
- The hash table has n elements
- The list of counters has n elements
"""

from typing import List


class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:

hash_table = dict()
output = []

for s in strs:
count_s = ''.join(sorted(s))
if count_s in hash_table:
idx = hash_table[count_s]
output[idx].append(s)
else:
hash_table[count_s] = len(output)
output.append([s])

return output

87 changes: 87 additions & 0 deletions group-anagrams/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Intuition
์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ทธ๋ฃน ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•œ๋‹ค.
# Approach
1. `{์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, ๊ทธ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค}`๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
2. ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ, ์ธ๋ฑ์Šค๋ฅผ struct๋กœ ํ•˜์—ฌ ๋ฐฐ์—ด์— ์‚ฝ์ž…ํ•œ๋‹ค.
- ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ด์œ ๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. (์ •๋ ฌ์„ ํ–ˆ๊ธฐ์— ์ด๋ ‡์ง€ ์•Š์œผ๋ฉด ํ™•์ธ์ด ์–ด๋ ต๋‹ค.)
3. ๋ชจ๋“  struct๊ฐ€ ์‚ฝ์ž…๋œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
4. ๋ฐ˜๋ณต๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉฐ `์ด์ „ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€` ์—ฌ๋ถ€๋ฅผ ๊ทธ๋ฃน์„ ํŒ๋‹จํ•˜์—ฌ ์ธ๋ฑ์Šค๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•ด ๋ชจ์•„๋†“๋Š”๋‹ค.
5. ๊ทธ๋ฃจํ•‘ํ•œ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌธ์ž์—ด(์›๋ณธ ๋ฌธ์ž์—ด์„ ์ฐธ์กฐํ•ด์„œ)๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
# Complexity
- Time complexity: $$O(nklog(n))$$
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ -->
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`. ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด๋‹ค.
- Space complexity: $$O(nk)$$

: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฐฐ์—ด์ด ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`, (์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด, ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ ๋ชจ๋‘ `n`, `k`์ด๋‹ค.)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```go
type StrAndIdx struct {
Str string
Idx int
}

func sortSring(s string) string {
runes := []rune(s)

sort.Slice(runes, func(i, j int) bool {
return runes[i] < runes[j]
})

return string(runes)
}

func groupAnagrams(strs []string) [][]string {

strAndIdxs := make([]StrAndIdx, 0, len(strs)+5)

for i, s := range strs {
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i})
}

sort.Slice(strAndIdxs, func(i, j int) bool {
return strAndIdxs[i].Str < strAndIdxs[j].Str
})

groupedIdxs := make([][]int, 0, len(strAndIdxs)/4)

group := make([]int, 0)
defaultString := "NOT_EXIST_STRING"
prev := defaultString
for _, sAI := range strAndIdxs {
curr := sAI.Str
idx := sAI.Idx
if prev == curr {
group = append(group, idx)
} else {
if prev != defaultString {
groupedIdxs = append(groupedIdxs, group)
}
group = []int{idx}
}
prev = curr
}

if len(group) != 0 {
groupedIdxs = append(groupedIdxs, group)
}

groupedStrs := make([][]string, 0, len(groupedIdxs))
for _, idxs := range groupedIdxs {

groupStr := make([]string, 0, len(idxs))
for _, idx := range idxs {
groupStr = append(groupStr, strs[idx])
}
groupedStrs = append(groupedStrs, groupStr)
}

return groupedStrs
}
```

# **To** Learn
- GoLang์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋Š”์ง€.
- ํ•ด๋‹น ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์žˆ์–ด์„œ ์‚ฝ์งˆํ•œ ๊ฒƒ์€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์•ˆํ•  ์ˆ˜ ์žˆ๋Š”์ง€.
23 changes: 23 additions & 0 deletions group-anagrams/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var groupAnagrams = function (strs) {
// Declare hash map to store sorted strs
let map = new Map();

for (let str of strs) {
// Sorted each str
const sortedStr = str.split("").sort().join("");

// If there is alread sortedStr on the map, pushed str
if (map.has(sortedStr)) {
map.get(sortedStr).push(str);
} else {
// If there is no sortedStr on the map, insert [str]
map.set(sortedStr, [str]);
}
}
return Array.from(map.values());
};

// TC: O(n*klogk)
// SC: O(n*k)
// n -> length of strs array
// k -> amount of character for each element
Loading

0 comments on commit e0508f0

Please sign in to comment.