πͺπ» programmers κ³ λμ Kit - ν΄μ(Hash)
ν΄μ(Hash)
κ³ μ λ ν¬κΈ°
λ‘ κ°μ λ°κΎΈλ ν¨μλ μκ³ λ¦¬μ¦. μ΄ κ²°κ³Όλ‘ λ§λ€μ΄μ§ κ°μ ν΄μμ½λ(hash code) νΉμ ν΄μ κ°(hash value)λΌκ³ λΆλ₯Έλ€.
ν΄μλ μ μ¬μ©νλ κ²μΌκΉ?
- λ³΄ν΅ μ± μλ λͺ©μ°¨κ° μ‘΄μ¬νλ€. μλ₯Όλ€μ΄ 1,000νμ΄μ§κ° λλ μ± μμ μ΄λ€ λ΄μ©μ μ°Ύμ λ λͺ©μ°¨κ° μκ±°λ μ΄μ΄ λμλ€λ©΄ 1,000νμ΄μ§μμ ν΄λΉ λ΄μ©μ μ°Ύμ μ μλ€. νμ§λ§ λͺ©μ°¨κ° μλ€λ©΄ μνλ λ΄μ©μ λΉ λ₯΄κ² μ°Ύμ μ μκ²λλ€.
-> μ΄μ²λΌ μ½κ² μ°Ύμ μ μλλ‘ λμμ£Όλ λͺ©μ°¨λ ν΄μμ½λ(hash code)
λ₯Ό μλ―Ένλ€.
-> κ³ μ λ ν¬κΈ°
λ λͺ©μ°¨μ κ°μ
λ₯Ό μλ―Ένλ€.
μ¦, ν΄μλ λ³΄λ€ λΉ λ₯΄κ² μ°ΎκΈ°μν΄ μ‘΄μ¬νλ€.
ν΄μ ν¨μ(hash function) : ν΄μ μ½λλ₯Ό λ§λ€μ΄λ΄λ ν¨μλ₯Ό μλ―Έ
π μ’μ ν΄μ ν¨μ 쑰건
- κ³μ° μλκ° λΉ¨λΌμΌ νλ€.
- κ²°κ³Ό κ°μ΄ λ€μν΄μΌ νλ€.
π ν΄μ μΆ©λ
μλ‘ λ€λ₯Έ λμ(key κ°)μ΄ λμΌν ν΄μ κ°μ κ°μ§λ νμ
β ν΄μ μΆ©λμ μ λ°μν κΉ?
ν΄μ μΆ©λμ μλ²½ν ν΄μ ν¨μ ꡬνμ μ΄λ €μμμ λ°μνλ€. ν΄μ ν¨μλ μ λ ₯κ°μ λ²μκ° μ¬μ€μ 무νλμ κ°κΉκΈ° λλ¬Έμ, λ€μν μ λ ₯κ°μ κ³ μ λ ν¬κΈ°μ μΆλ ₯κ°μΌλ‘ λ§€ννλ κ³Όμ μμ μΆ©λμ΄ λ°μν μ μλ€. μλ₯Ό λ€μ΄, μ λ ₯κ°μ λ¬Έμμ΄, μ«μ λ± λ€μν λ°μ΄ν°λ‘ μ΄λ£¨μ΄μ§ μ μμ§λ§, ν΄μ ν¨μλ κ³ μ λ ν¬κΈ°μ μΆλ ₯κ°(μ: 4λ°μ΄νΈ μ μν λλ 256λΉνΈ κ° λ±)μΌλ‘ λ³ννλ€. μ΄λ¬ν μ νλ κ³΅κ° λ΄μμ μλ‘ λ€λ₯Έ μ λ ₯κ°μ΄ λμΌν μΆλ ₯κ°μ κ°μ§ μλ°μ μλ κ²½μ°κ° μκΈ°λλ°, μ΄λ₯Ό ν΄μ μΆ©λμ΄λΌκ³ νλ€.
ν΄μ μΆ©λ ν΄κ²° λ°©λ²
π κ°λ³ 체μ΄λ(seperate chaining)
: λΆλ¦¬μ°κ²°λ²
- μΆ©λμ΄ λ°μνλ©΄ μ°κ²°λ μλ‘μ΄ κ³΅κ°μ μ¬μ©νλ λ°©λ² ->
LinkedList
- ν€(key)μ ν΄μ κ°μ κ³μ°νλ€.
- ν΄μ κ°μ μ΄μ©ν΄ λ°°μ΄μ μΈλ±μ€λ₯Ό ꡬνλ€.
- κ°μ μΈλ±μ€κ° μλ€λ©΄
LinkedList
λ‘ μ°κ²°νλ€.
μΆ©λμ΄ λ°μν 'μ€μ'
, 'μν'
μ λ€μ μμ΄ν
μΈ 'μν'
μ ννλ‘ μλ‘ μ°κ²° 리μ€νΈλ‘ μ°κ²°λλ€.
-> μ½κ² μννΈμ²λΌ λͺ¨μ¬μλ€κ³ μκ°νλ©΄ μ’λ€. 'μ€μ'
λ 2μΈ΅ 1νΈ, 'μν'
μ 2μΈ΅ 2νΈ
LinkedList
λ₯Ό μ¬μ©νκΈ° λλ¬Έμ λ³΄λ€ μ μ°νκ² μ¬μ©ν μ μκ³ ν΄μ κ°μ κ·Έλλ‘ μ¬μ©ν μ μμ§λ§LinkedList
λ₯Ό μ¬μ©νλ€λ κ²μ μΆκ°μ μΈ κ³΅κ°μ΄ νμνλ€λ μλ―Έμ΄λ€. λ°λΌμ μ°μλ λ°μ΄ν°κ° μλκΈ° λλ¬Έμ μΊμμ λμμ λ°κΈ° μ΄λ ΅λ€.LinkedList
λ κ²μ μO(n)
λ§νΌμ μκ°μ΄ μμλλ€.
π μ€ν μ΄λλ μ±(oepn addressing)
: κ°λ°©μ£Όμλ²
- μΆ©λμ΄ λ°μνλ©΄ λ€λ₯Έ ν΄μ μ½λ(λ²ν·)λ₯Ό μ¬μ©νλ λ°©λ². μΈμ ν λΉ κ³΅κ°μ μ μ₯
μ ν νμ¬(Linear Probing)
: μΆ©λ μ λ€μ λ²ν·, νΉμ λͺκ°λ₯Ό 건λλ°μ΄ λ°μ΄ν° μ½μμ κ³± νμ¬(Quadratic Probing)
: μΆ©λ μ μ κ³±λ§νΌ 건λλ΄ λ²ν·μ λ°μ΄ν°λ₯Ό μ½μμ΄μ€ ν΄μ(Double Hashing)
: μΆ©λ μ λ€λ₯Έ ν΄μ ν¨μλ₯Ό νλ² λ μ μ©ν κ²°κ³Όλ₯Ό μ΄μ©
κ³ μ λ ν¬κΈ°μ λ°°μ΄μ μ¬μ©νκΈ° λλ¬Έμ μΆκ°μ μΈ κ³΅κ°μ νμλ‘ νμ§ μλλ€. λ°μ΄ν°κ° μ μ λ κ°λ³ 체μ΄λλ³΄λ€ μ’μ μ±λ₯μ 보μΈλ€. μ°μλ 곡κ°μ μ¬μ©νκΈ° λλ¬Έμ CPUκ° λΉ λ₯΄κ² μ°Ύμ μ μκΈ° λλ¬Έμ΄λ€.
νμ§λ§ λ°μ΄ν°κ° λ§μμ§λ©΄ μΊμμμ μ°Ύλ κ°μ κΊΌλ΄μ¬ νλ₯ μ΄ μ€μ΄λ€μ΄ μ’μ μΊμμ μ±λ₯μ κΈ°λν μ μλ€.
μΆ©λμ΄ λ°μνμ λ λ€μ λΉ κ³΅κ°μ μ°ΎκΈ° μ΄λ €μμ§κ³ κ°μ΄ μμ£Ό μΆκ°λκ³ μμ λλ€λ©΄ μ±λ₯μ΄ λ¨μ΄μ§λ€.
HashMap
key
μ μ€λ³΅μ νΌνκΈ° μν΄ ν΄μλ₯Ό μ¬μ©νλ Map
μλ£κ΅¬μ‘°μ΄λ€.
Java
μHashMap
μν΄μ μΆ©λ
μκ°λ³ 체μ΄λ
λ°©μμΌλ‘ λμνλ€.
보쑰 ν΄μ ν¨μ : ν΄μ κ°μ΄ λ κ· λ±νκ² λμ¬ μ μκ² λμμ£Όλ ν¨μ
HashTable
HashMap
κ³Ό μ μ¬ν μλ£κ΅¬μ‘°Map
μμλkey
μvalue
λͺ¨λnull
μ μ¬μ©ν μ μμ§λ§Table
μμλ μ¬μ©ν μ μλ€.
-> ν΄λΉ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ Java 1.5
μ΄νλΆν° ConcurrentHashMap
μ μ§μνλ€.
HashTable
μThread-safe
νμ§λ§ μ±λ₯μ΄ λΆμ‘±ν΄μConcurrentHashMap
μ μ¬μ©ν΄μ μ°λ λ μμ μ±μ μ±κΈ°λ©΄ λλ€.
HashMap
κ³Ό HashTable
μ°¨μ΄μ
HashTable
μJavaμ API. 컬λ μ νλ μμν¬κ° λ§λ€μ΄μ§κΈ° μ μ μ‘΄μ¬νλ κ²μ΄μ§λ§ νΈνμ μν΄ λ¨κ²¨λ κ²μ΄κ³HashMap
μ Java Collections Framework APIμ΄λ€.HashTable
μsyncronized
ν€μλκ° μμ΄ λκΈ°νλ₯Ό μ§ννμ§λ§HashMap
μ κ²½μ°syncronized
ν€μλκ° μμ΄μ λ©ν°μ€λ λ νκ²½μμ λ°μ΄ν° 무결μ±μ 보μ₯νμ§ μλλ€.HashTable
μKey
μnull
μ΄ λ€μ΄μ€λ©΄NullPointerError
λ₯Ό λμ§μ§λ§HashMap
μKey
μnull
μ΄ λ€μ΄μ€λ©΄0μΌλ‘ ν΄μ±
νμ¬ μ μ₯νλ€.
HashSet
λ΄λΆμ μΌλ‘
HashMap
μ μ¬μ©νλ€.Map
μλ£ κ΅¬μ‘°μkey
κ° μ€λ³΅λ μ μλ€λ μ μ μ΄μ©ν΄set
μ΄λΌλ μλ£ κ΅¬μ‘°μ νΉμ§μ μ΄λ Έλ€.key
λ‘λ μ μ₯ν λ°μ΄ν°λ₯Ό,value
κ°μ λ΄λΆμ μΌλ‘ μμ±λμ΄ μλdummy
λ₯Ό μ¬μ©νλ€.
Red-Black Tree
LinkedList
λ μλ£ κ΅¬μ‘° κ²μ μ O(n)
μ μκ°μ μμνλ€. -> μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ Java
λ Tree
μλ£ κ΅¬μ‘°λ₯Ό νμ©νλ€.
Java
λ μΆ©λμ΄ λ°μν κ²½μ° Seperate Chaining
μ LinkedList
λ‘ κ΅¬νν΄ μΆ©λμ λμνμλ€. κ·Έλ¬λ Java 8
λΆν° λ°μ΄ν°μ κ°μκ° μΌμ μ΄μμΌ λλ κΈ°μ‘΄μ μ¬μ©νλ LinkedList
λμ Tree
λ₯Ό μ¬μ©νλ€.
λ°μ΄ν°κ° nλ² μΆ©λν΄ μ°κ²° 리μ€νΈμ nλ²μ§Έ λ
Έλμ μ μ₯λλ€λ©΄ ν΄λΉ λ
Έλ νμμ μν μκ°λ³΅μ‘λλ O(n)
μ΄ λλ€.
λ°λΌμ μΆ©λμ΄ λ§μμ§μλ‘ ν¨μ¨μ΄ μ μ λ¨μ΄μ§κΈ° λλ¬Έμ μΌμ μΆ©λ μκ° λμ΄κ°λ©΄ νΈλ¦¬ λ°©μμ μ¬μ©ν΄ μ±λ₯μ O(log n)
μΌλ‘ κ°μ νλ€.
Java8
μ HashMap
μ λ€μ΄κ°μ νμΈνλ©΄ μμλ‘ κΈ°μ€μ΄ μ ν΄μ Έ μλ€.
νλμ ν΄μ λ²ν·μ 8κ°μ key-value
μμ΄ λͺ¨μ΄λ©΄ LinkedList
λ₯Ό Tree
λ‘ λ³κ²½νκ³ λ°μ΄ν°λ₯Ό μμ ν΄ κ°μκ° 6κ°κ° λλ©΄ λ€μ LinkedList
λ‘ λ³κ²½νλ€.
μ¬κΈ°μ μ°¨μ΄κ° 1μ΄ μλ 2μΈ μ΄μ λ μ΄λ€ ν key-value
μμ΄ λ°λ³΅λμ΄ μ½μ
/μμ
κ° λλ κ²½μ° λΆνμνκ² Tree
μ LinkedList
λ₯Ό λ°λ³΅μ μΌλ‘ λ³κ²½ν΄ μ±λ₯ μ ν
κ° λ°μν μ μκΈ° λλ¬Έμ΄λ€.
λ, λ°μ΄ν°μ κ°μκ° 6 μ΄νμΌ λ LinkedList
λ‘ λ€μ λ³κ²½νλ μ΄μ λ λ°μ΄ν° κ°μκ° μ μ λ LinkedList
μ μ΅μ
μ κ²½μ° μν μκ° μ°¨μ΄κ° λ§μ§ μκ³ Tree
λ LinkedList
λ³΄λ€ λ©λͺ¨λ¦¬ μ¬μ©λμ΄ λ§κΈ° λλ¬Έμ΄λ€.
νΉν Red-Black Tree
λΌλ κ²μμ μ΅μ νλ νΈλ¦¬ ꡬ쑰λ₯Ό μ¬μ©νλ€.
Java
λ λ°μ΄ν°μ μΌμ μΆ©λ μκ° λμ΄κ°λ©΄Red-Black Tree
λ‘ λ°μ΄ν°λ₯Ό μ μ₯νλ€. μ°μ λ°μ΄ν° κ°μκ° λ§κΈ° μ μλNode
κ°μ²΄λ‘LinkedList
λ₯Ό ꡬννμ§λ§Red-Black Tree
λ λ¨μNode
κ°μ²΄λ‘ ꡬνν μ μλ€.
Node
κ°μ²΄λ₯ΌtreeifyBin
λ©μλλ₯Ό ν΅ν΄TreeNode
κ°μ²΄λ‘ λ°κΏμ€λ€.treeify
λ©μλλ₯Ό ν΅ν΄parent
,left
,right
νλμRed-Black Tree
λ‘μ§μ μν μ μ ν κ°μ λ£μ΄μ€λ€.
π Java 8
μ΄νλ‘, κΈ°μ‘΄μ LinkedList
λ‘ Chainning
κΈ°λ²μ μ¬μ©ν TreeMap
μ Red-Black Tree
λ‘ λ³κ²½νμ¬ μκ°λ³΅μ‘λλ₯Ό O(n)
μμ O(logn)
μΌλ‘ μ€μΌ μ μλ€.
ν¬μΌλͺ¬ - νλ‘κ·Έλλ¨Έμ€
ν΄λΉ λ¬Έμ λ λ§€κ°λ³μλ‘ μ λ ₯ λ°λ μ μμμ N/2 λ§λ¦¬λ₯Ό μ νν λ κ°μ₯ λ§μ μ’ λ₯μ ν¬μΌλͺ¬μ μ ννλ λ°©λ²μ ꡬνλ λ¬Έμ μ΄λ€.
[3, 1, 2, 3] μΌ κ²½μ°
- (3, 1)
- (3, 2)
- (3, 3)
- (1, 2)
- (1, 3)
- (2, 3)
μΌλ‘ 6κ°μ§μ κ²½μ°μ΄μ§λ§ ν¬μΌλͺ¬ μ’ λ₯ μμ μ΅λκ°μ 2μ΄λ€.
[3, 3, 3, 2, 2, 2] μΌ κ²½μ°
- (2, 3) μΌλ‘ μ΅λκ°μ 2μ΄λ€.
μ¬κΈ°μ HashSet
μ μ€λ³΅μ νμ©νμ§ μλλ€.
μ¦, [3, 1, 2, 3]μ set.size()
λ 3μ΄λ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
class Solution {
public int solution(int[] nums) {
int answer = 0;
int num = nums.length/2;
HashSet<Integer> set = new HashSet<>();
for(int i=0;i<nums.length;i++)
{
set.add(nums[i]);
}
if(num < set.size())
{
answer = num;
}
else{
answer = set.size();
}
return answer;
}
}
λ¨Όμ
HashSet
μ μ μΈνκ³nums
λ₯Όset
μ μΆκ°νλ€.num
μ κ°μ΄set
μ μ¬μ΄μ¦λ³΄λ€ μλ€λ©΄num
μ΄ λ΅μ΄ λλ€.
[3, 1, 2, 3]
μμnum = 2
,set.size() = 3
μΈλ°,num < set.size()
μ΄λ―λ‘num
μ΄ λΆ
num
μ κ°μ΄set
μ μ¬μ΄μ¦λ³΄λ€ ν¬λ€λ©΄set.size()
κ° λ΅μ΄ λλ€.
[3, 3, 3, 2, 2, 2]
μμnum = 3
,set.size() = 2
μΈλ°,num > set.size()
μ΄λ―λ‘set.size()
κ° λΆ
μμ£Όνμ§ λͺ»ν μ μ - νλ‘κ·Έλλ¨Έμ€
ν΄λΉ λ¬Έμ λ λ§λΌν€μ μ°Έμ¬ν μ μλ€μ μ΄λ¦μ΄ λ΄κΈ΄ λ°°μ΄ participant, μμ£Όν μ μλ€μ μ΄λ¦μ΄ λ΄κΈ΄ λ°°μ΄ completion, μμ£Όνμ§ λͺ»ν μ μμ μ΄λ¦μ ꡬνλ λ¬Έμ μ΄λ€.
μ¬κΈ°μ μ°Έκ°μ μ€μ λμΌν μ΄λ¦μ΄ μμ μ μκ³ key-valueλ₯Ό μ¬μ©νμ¬ λΉ λ₯΄κ² μ°ΎμμΌ λλ―λ‘ HashMapμ μ¬μ©νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
Map<String, Integer> map = new HashMap<>();
for(String s : participant)
{
map.put(s, map.getOrDefault(s, 0)+1);
}
for(String s : completion)
{
map.put(s, map.get(s)-1);
}
for(String s : map.keySet())
{
if(map.get(s) > 0)
{
answer = s;
// break;
}
}
return answer;
}
}
λ¨Όμ
HashMap
μ μ μΈνλ€.ν₯μλ
for
λ¬Έμ μ΄μ©ν΄μs
μparticipant
μ κ°μ λμ νκ³map
μput
λ©μλλ₯Ό μ΄μ©ν΄ μΆκ°νλ€. μ¬κΈ°μmap.getOrDefault(s, 0)
λ ν΄λΉ μ΄λ¦s
κ° μ΄λ―Έ μ μ₯λμ΄ μμΌλ©΄ κ·Έ κ°μ κ°μ Έμ€κ³ κ·Έλ μ§ μμ κ²½μ°0
μ λ°ννλ€.+1
μ ν΄μ€ μ΄μ λ νμλ₯Ό μΈκΈ° μν¨μ΄λ€.ν₯μλ
for
λ¬Έμ μ΄μ©ν΄μs
μcompletion
κ°μ λμ νκ³map
μput
λ©μλλ₯Ό μ΄μ©ν΄ μΆκ°νλ€. μ¬κΈ°μmap.get(s)-1
μ νμ¬map
μ μ μ₯λs
λ₯Ό κ°μ Έμμ-1
μ μννλ€.map.keySet()
μmap
μ λͺ¨λ ν€λ₯Ό μννλ€.
μ νλ²νΈ λͺ©λ‘ - νλ‘κ·Έλλ¨Έμ€
ν΄λΉ λ¬Έμ λ μ νλ²νΈλΆμ μ ν μ νλ²νΈλ₯Ό λ΄μ λ°°μ΄μ΄ μ£Όμ΄μ§κ³ μ΄λ€ λ²νΈκ° λ€λ₯Έ λ²νΈμ μ λμ΄μ μμ κ²½μ° false, κ·Έλ μ§ μμ κ²½μ° trueλ₯Ό λ°ννλ ν¨μλ₯Ό ꡬνλ κ²μ΄λ€.
HashMapμ μ μΈνλ€.
mapμ phone_book λ°°μ΄μ κ°μ μΆκ°νλ€.
μ΄μ€ forλ¬Έμ μ¬μ©νμ¬ mapμ νΉμ ν€κ° μ‘΄μ¬νλμ§ νμΈνλ€. μ¬κΈ°μ substringμ μ¬μ©νμ¬ 0λΆν° μκΈ°μμ κΉμ§ νμΈνλ€.
- [12, 13, 123]μ μμλ‘ λ λ€λ©΄ phone_book[2] μΌ λ, substring(0,1) -> 1, substring(0,1) ->2 μ΄λ―λ‘ β12βμ κ²ΉμΉλ―λ‘ falseκ° λλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
class Solution {
public boolean solution(String[] phone_book) {
Map<String, Integer> map = new HashMap<>();
for(String s : phone_book)
{
map.put(s, map.getOrDefault(s,0));
}
for(int i=0;i<phone_book.length;i++)
{
for(int j=0;j<phone_book[i].length();j++)
{
if(map.containsKey(phone_book[i].substring(0,j)))
{
return false;
}
}
}
return true;
}
}
λ°°μ΄μ length
: μμ κ°μΌλ‘ μ μ₯λ ν¬κΈ°λ₯Ό λ¨μν μ½μ. μλ°μμ μμ±μΌλ‘ ꡬνλμ΄ μκ³ μ΄λ λ°°μ΄μ ν¬κΈ°λ₯Ό μμ κ°μΌλ‘ μ μ₯νκ³ λ°ννλ λ°©μ.
λ¬Έμμ΄μ length()
: λ¬Έμμ΄μ κΈΈμ΄λ₯Ό κ³μ°νκ±°λ λ°ννλ λ‘μ§μ΄ ν¬ν¨λ μ μμ. λ©μλλ‘ κ΅¬νλμ΄ μκ³ λ΄λΆμ μΌλ‘ λ¬Έμμ΄μ κΈΈμ΄λ₯Ό κ³μ°ν΄μ λ°νν¨.
κ·Έ μΈμ μ λ°νλ νμ΄
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
class Solution {
public boolean solution(String[] phone_book) {
boolean answer = true;
Arrays.sort(phone_book);
for(int i=0;i<phone_book.length-1;i++)
{
if(phone_book[i+1].startsWith(phone_book[i]))
{
return false;
}
}
return answer;
}
}
μμ - νλ‘κ·Έλλ¨Έμ€
ν΄λΉ λ¬Έμ λ κ° μ’ λ₯λ³λ‘ μ΅λ 1κ°μ§ μμλ§ μ°©μ©ν μ μμΌλ©° μ·μ μ‘°ν©νμ¬ λμ€λ κ²½μ°μ κ°μλ₯Ό ꡬνλ κ²μ΄λ€.
1
[["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]
ν΄λΉ μμ λ₯Ό 보면 headgearμ΄ (yellow_hat
, green_turban
) 2κ°, eyewearμ΄ (blue_sunglasses
) 1κ°μ΄λ€. κ·Έλ¬λ©΄ λμ€λ κ²½μ°λ λ€μκ³Ό κ°λ€.
- yellow_hat
- blue_sunglasses
- green_turban
- yellow_hat + blue_sunglasses
- green_turban + blue_sunglasses
μ΄λ headgear
κ°μ 2
μ eyewear
μ κ°μ 1
μ 1
μ© λν λ€ κ³±νκ³ λ§μ§λ§μ λͺ¨λ μ μ
λ κ²½μ°(1)λ₯Ό λΉΌμ£Όλ©΄ λλ€.
- headgear: 2κ°μ μμ΄ν + 1 (μ무κ²λ μ μ μ) β 3κ°μ§ μ ν
- eyewear: 1κ°μ μμ΄ν + 1 (μ무κ²λ μ μ μ) β 2κ°μ§ μ ν
μ‘°ν©μ μ κ°
yellow_hatμ μ ννμ λ:
- yellow_hat + blue_sunglasses
- yellow_hat + μ무κ²λ μ μ μ
green_turbanμ μ ννμ λ:
- green_turban + blue_sunglasses
- green_turban + μ무κ²λ μ μ μ
μ무κ²λ μ μ μμ μ ννμ λ:
- μ무κ²λ μ μ μ + blue_sunglasses
- μ무κ²λ μ μ μ + μ무κ²λ μ μ μ
(2+1)Γ(1+1)=6
μ΄κ³ μ무κ²λ μ
μ§ μλ κ²½μ°λ₯Ό μ μΈν΄μΌ νλ―λ‘ 1
μ λΉΌμ€λ€. 6β1=5
κ° λλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
int answer = 1;
Map<String, Integer> map = new HashMap<>();
for(String[] s : clothes)
{
map.put(s[1], map.getOrDefault(s[1], 0)+1);
}
for(String s: map.keySet())
{
answer *= map.get(s) + 1;
}
return answer-1;
}
}
map.put(s[1], map.getOrDefault(s[1], 0)+1);
λ₯Ό 보면 mapμ key λΆλΆμλ s[1]μ κ°μ λ£κ³ , value λΆλΆμλ s[1]μ΄ μμΌλ©΄ κ·Έ κ°μ λ£κ³ μμΌλ©΄ 0+1μ λμ νλ€.map.keySet()
μ mapμ λͺ¨λ ν€λ₯Ό μνmap.get(s)
λ mapμ μ μ₯λ sλ₯Ό κ°μ Έμ΄.
λ¬λ¦¬κΈ° κ²½μ£Ό - νλ‘κ·Έλλ¨Έμ€
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
Map<String, Integer> map = new HashMap<>();
for(int i=0;i<players.length;i++)
{
map.put(players[i], i); // μ΄λ¦, μμ
}
for(String calling : callings)
{
int grade = map.get(calling); // νΈμΆλ μ΄λ¦μ νμ¬ μμ
String forwardCalling = players[grade-1]; // ν΄λΉ μμ μ§μ μ players μ΄λ¦μ κ°μ Έμ΄
players[grade-1] = calling; // νΈμΆν νλ μ΄μ΄λ₯Ό μμΌλ‘ μ΄λ
map.put(calling, grade -1); // mapμμ μμ νμΉΈ μμΌλ‘ μ΄λ
players[grade] = forwardCalling; // μμ μλ νλ μ΄μ΄ λ€λ‘ μ΄λ
map.put(forwardCalling, grade); //mapμμ μμ μ
λ°μ΄νΈ
}
return players;
}
}
λ² μ€νΈ μ¨λ² - νλ‘κ·Έλλ¨Έμ€
μλλ μ 곡λ Java μ½λμ λν΄ ν μ€μ© μ£Όμμ μΆκ°νμ¬ ν΄μν κ²μ
λλ€. κ·Έλ¦¬κ³ Entry
μ λν μ€λͺ
λ ν¬ν¨νμμ΅λλ€.
μ½λ μ€λͺ λ° μ£Όμ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*; // λ€μν Java μ νΈλ¦¬ν° ν΄λμ€λ€(ArrayList, HashMap, Entry λ±)μ μ¬μ©νκΈ° μν΄ import
import java.util.Map.Entry; // Mapμ λ΄λΆ μΈν°νμ΄μ€ Entryλ₯Ό μ¬μ©νκΈ° μν΄ import
class Solution {
public int[] solution(String[] genres, int[] plays) { // μ₯λ₯΄ λ°°μ΄κ³Ό μ¬μ μ λ°°μ΄μ μ
λ ₯λ°μ λ² μ€νΈ μ¨λ²μ ν¬ν¨λ λ
Έλ μΈλ±μ€λ₯Ό λ°ννλ ν¨μ
int[] answer = {}; // μ΅μ’
κ²°κ³Όλ₯Ό λ΄μ λ°°μ΄ μ μΈ
ArrayList<Integer> answerList = new ArrayList<>(); // κ²°κ³Όλ₯Ό μ μ₯ν ArrayList(λμ μΌλ‘ ν¬κΈ°λ₯Ό κ΄λ¦¬ κ°λ₯)
Map<String, Integer> map = new HashMap<>(); // μ₯λ₯΄λ³ μ΄ μ¬μ μλ₯Ό μ μ₯ν HashMap
for(int i = 0; i < genres.length; i++) {
// κ° μ₯λ₯΄λ³λ‘ μ¬μ μ ν©κ³λ₯Ό κ³μ°νμ¬ HashMapμ μ μ₯
map.put(genres[i], map.getOrDefault(genres[i], 0) + plays[i]);
// map.getOrDefault(key, defaultValue)λ keyκ° μ‘΄μ¬νλ©΄ κ°μ λ°ννκ³ , μμΌλ©΄ κΈ°λ³Έκ°(defaultValue)μ λ°ν
}
// mapμ entrySet()μ μ¬μ©ν΄ μ₯λ₯΄μ μ¬μ μλ₯Ό Listλ‘ λ³ν
List<Entry<String, Integer>> genresList = new ArrayList<>(map.entrySet());
// μ₯λ₯΄λ³ μ΄ μ¬μ μλ₯Ό κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μ μ λ ¬
Collections.sort(genresList, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
// o2μ κ°(μ¬μ μ)μ΄ λ ν¬λ©΄ μμ μ€λλ‘ μ λ ¬
}
});
// μ λ ¬λ μ₯λ₯΄λ³λ‘ λ°λ³΅
for (Entry<String, Integer> entry : genresList) {
Map<Integer, Integer> temp = new HashMap<>(); // νμ¬ μ₯λ₯΄μ μν 곑μ μΈλ±μ€μ μ¬μ μλ₯Ό μ μ₯ν μμ HashMap
for (int i = 0; i < genres.length; i++) {
if (entry.getKey().equals(genres[i])) {
// νμ¬ μ₯λ₯΄(entry.getKey())μ μν 곑μΈμ§ νμΈ
temp.put(i, plays[i]); // ν΄λΉ 곑μ μΈλ±μ€(i)μ μ¬μ μ(plays[i])λ₯Ό μ μ₯
}
}
// μ₯λ₯΄ λ΄ κ³‘λ€μ μ¬μ μ κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μ μ λ ¬
List<Entry<Integer, Integer>> songsList = new ArrayList<>(temp.entrySet());
Collections.sort(songsList, new Comparator<Entry<Integer, Integer>>() {
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
// μ¬μ μ κΈ°μ€ λ΄λ¦Όμ°¨μ μ λ ¬
}
});
// μμ λ 곑μ μΈλ±μ€λ₯Ό answerListμ μΆκ°
for (int j = 0; j < songsList.size() && j < 2; j++) {
answerList.add(songsList.get(j).getKey());
// λ
Έλμ μΈλ±μ€λ₯Ό μΆκ°
}
}
// ArrayListλ₯Ό λ°°μ΄λ‘ λ³ννμ¬ λ°ν
answer = new int[answerList.size()];
for (int i = 0; i < answerList.size(); i++) {
answer[i] = answerList.get(i);
}
return answer; // μ΅μ’
κ²°κ³Ό λ°ν
}
}
Entry
λ?
Entry
λ Map μΈν°νμ΄μ€μ λ΄λΆ μΈν°νμ΄μ€λ‘,Map
μ μ μ₯λ ν€-κ° μ(key-value pairs)μ λνλΈλ€.μλ₯Ό λ€μ΄,
HashMap
μμentrySet()
λ©μλλ₯Ό νΈμΆνλ©΄ λͺ¨λ ν€-κ° μμ΄ ν¬ν¨λSet<Entry<K, V>>
λ₯Ό λ°ννλ€.Entry<K, V>
κ°μ²΄λ λ κ°μ§ μ£Όμ λ©μλλ₯Ό μ 곡ν¨getKey()
: νμ¬Entry
κ°μ²΄μ ν€λ₯Ό λ°ν.getValue()
: νμ¬Entry
κ°μ²΄μ κ°μ λ°ν.
Entry
μ μ¬μ© μ΄μ
HashMap
μ λ°μ΄ν°λ₯Ό μ λ ¬νκ±°λ νΉμ 쑰건μΌλ‘ μ²λ¦¬ν λ, key
μ value
λ₯Ό ν¨κ» μ¬μ©ν΄μΌ νλ κ²½μ°κ° λ§λ€. μ΄λ, entrySet()
μ ν΅ν΄ λͺ¨λ ν€-κ° μμ μ½κ² κ°μ Έμ μ λ ¬μ΄λ νν°λ§μ μνν μ μλ€.
1
2
3
4
5
6
7
8
9
10
11
12
Map<String, Integer> map = new HashMap<>();
map.put("Pop", 500);
map.put("Rock", 300);
// entrySet()μΌλ‘ ν€-κ° μμ κ°μ Έμ΄
for(Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
// μΆλ ₯:
// Key: Pop, Value: 500
// Key: Rock, Value: 300
1) μ₯λ₯΄λ³ μ¬μ νμλ₯Ό HashMap
μ μ μ₯νκ³ λ΄λ¦Όμ°¨μ μ λ ¬
2) λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬λ μ₯λ₯΄λ³λ‘ κ³ μ λ²νΈμ μ¬μνμλ₯Ό HashMap
μ μ μ₯
3) HashMap
μ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬, μ¬μνμκ° λ§μ 2κ°μΌ,γ
£ κ³ μ λ²νΈλ₯Ό 리μ€νΈμ μ μ₯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
import java.util.Map.Entry;
class Solution {
public int[] solution(String[] genres, int[] plays) {
int[] answer = {};
ArrayList<Integer> answerList = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
for(int i=0;i<genres.length;i++)
{
map.put(genres[i], map.getOrDefault(genres[i],0)+plays[i]);
}
// μ₯λ₯΄μ μ¬μ μλ₯Ό Listλ‘ λ³ν
List<Entry<String, Integer>> genresList = new ArrayList<Entry<String, Integer>>(map.entrySet());
// μ₯λ₯΄λ³ μ΄ μ¬μ μλ₯Ό κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μ μ λ ¬
Collections.sort(genresList, new Comparator<Entry<String, Integer>>(){
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2)
{
return o2.getValue().compareTo(o1.getValue());
}
});
// μ λ ¬λ μ₯λ₯΄λ³λ‘ λ°λ³΅
for(Entry<String, Integer> entry : genresList)
{
Map<Integer, Integer> temp = new HashMap<>();
for(int i=0;i<genres.length;i++)
{
if(entry.getKey().equals(genres[i]))
{
temp.put(i,plays[i]);
}
}
// μ₯λ₯΄ λ΄ κ³‘λ€μ μ¬μ μ κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μ μ λ ¬
List<Entry<Integer,Integer>> songsList = new ArrayList<Entry<Integer, Integer>>(temp.entrySet());
Collections.sort(songsList, new Comparator<Entry<Integer, Integer>>(){
public int compare(Entry<Integer,Integer> o1, Entry<Integer, Integer> o2)
{
return o2.getValue().compareTo(o1.getValue());
}
});
// μμ λ 곑μ μΈλ±μ€λ₯Ό answerListμ μΆκ°
for(int j=0;j<songsList.size() && j<2; j++)
{
answerList.add(songsList.get(j).getKey());
}
}
// ArrayListλ₯Ό λ°°μ΄λ‘ λ³ννμ¬ λ°ν
answer = new int[answerList.size()];
for(int i=0;i<answerList.size();i++)
{
answer[i] = answerList.get(i);
}
return answer;
}
}