๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Coding Interview University] ๋ฐฐ์ด : ์ด๋ก ๋ฐ ๊ตฌํ ๋ณธ๋ฌธ
[Coding Interview University] ๋ฐฐ์ด : ์ด๋ก ๋ฐ ๊ตฌํ
์ง์ง์ํ์นด 2023. 6. 28. 15:05<๋ณธ ๋ธ๋ก๊ทธ๋ ์ฝ๋ฉ์ธํฐ๋ทฐ๋ํ ๋์ Git๊ณผ ์์ ์ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
๐ซง ๋ฐฐ์ด๊ณผ ๋ฌธ์์ด
โค๏ธ ํด์ํ ์ด๋ธ (hash table)
: ํจ์จ์ ์ธ ํ์์ ์ํ ์๋ฃ๊ตฌ์กฐ๋ก์, key๋ฅผ value ์ ๋์์ํด
: ์ต์ ์ ๊ฒฝ์ฐ O(n) // n์ ํค์ ๊ฐ์
: ์ต์ ์ ๊ฒฝ์ฐ O(1)
1) ํค์ ํด์ ์ฝ๋ ๊ณ์ฐ
- ํค์ ์๋ฃํ์ ๋ณดํต int or long
- ํค์ ๊ฐ์ ๋ฌดํ (int ๋ ์ ํ)
2) hash(key) % array_length ๋ฐฉ์์ผ๋ก ํด์ ์ฝ๋ ์ด์ฉํด ๋ฐฐ์ด์ ์ธ๋ฑ์ค ๊ตฌํจ
- ์๋ก ๋ค๋ฅธ ๋ ๊ฐ์ ํด์ ์ฝ๋๊ฐ ๊ฐ์ ์ธ๋ฑ์ค ๊ฐ๋ฆฌํฌ ์ ์์
3) ๋ฐฐ์ด์ ๊ฐ ์ธ๋ฑ์ค์๋ ํค์ ๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ง ์ฐ๊ฒฐ๋ฆฌ์คํธ ์กด์ฌ
- ํค์ ๊ฐ์ ํด๋น ์ธ๋ฑ์ค์ ์ ์ฅ (๋ฐ๋์ ์ฐ๊ฒฐ๋ฆฌ์คํธ ์ด์ฉ!)
- ๐ฅ ์ถฉ๋ : ์๋ก ๋ค๋ฅธ ๋ ๊ฐ์ ํค๊ฐ ๊ฐ์ ์ฝ๋๋ฅผ ๊ฐ๋ฆฌํค๊ฑฐ๋, ์๋ก ๋ค๋ฅธ ๋ ๊ฐ์ ํด์ ์ฝ๋๊ฐ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒฝ์ฐ
⇒ ํค์ ์์ํ๊ธฐ ์ํด์ ์๋ฅผ ๋ฐ๋ณต!
⇒ ์ฃผ์ด์ง ํค๋ก๋ถํฐ ํด์ ์ฝ๋๋ฅผ ๊ณ์ฐ, ํด์ ์ฝ๋๋ฅผ ์ด์ฉํด ์ธ๋ฑ์ค ๊ณ์ฐ, ํด๋น ํค์ ์์ํ๋ ๊ฐ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์์ ํ์
๊ท ํ ์ด์ง ํ์ ํธ๋ฆฌ (balanced binary search tree)
: ํ์ ์๊ฐ์ O(logN)
: ํฌ๊ธฐ๊ฐ ํฐ ๋ฐฐ์ด์ ๋ฏธ๋ฆฌ ํ ๋นํด ๋์ง ์์๋ ๋๊ธฐ ๋๋ฌธ์ ์ ์ ๊ณต๊ฐ ์ฌ์ฉ ๊ฐ๋ฅ
: ํค์ ์งํฉ์ ํน์ ์์๋๋ก ์ ๊ทผ ๊ฐ๋ฅ
โค๏ธ ArrayList ์ ๊ฐ๋ณ ํฌ๊ธฐ ๋ฐฐ์ด
- ๋์ ๊ฐ๋ณ ํฌ๊ธฐ ๊ธฐ๋ฅ ๋ด์ฌ์ ๋ฐฐ์ด์ผ ๋ ์ฌ์ฉ
- ํ์์ ๋ฐ๋ผ ํฌ๊ธฐ๋ฅผ ๋ณํ ๊ฐ๋ฅ
- O(1)์ ์ ๊ทผ ์๊ฐ์ ์ ์ง
- ๋ฐฐ์ด์ด ๊ฐ๋ ์ฐจ๋ฉด, ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ ๋ฐฐ๋ก ๋๋ฆผ
- O(n)์ผ๋ก ๋๋ ค์ง์ง๋ง, ์ํ ์ ๋ ฅ ์๊ฐ์ผ๋ก ์ธํด O(1)
- n๊ฐ์ ์์๋ฅผ ์ฝ์ ํ ๋ ์์ ์์ ์ O(n)
- ํ๊ท ์ ์ผ๋ก ๊ฐ ์ฝ์ ์ O(1)
โค๏ธ StringBuilder
: ๊ฐ๋ณ ํฌ๊ธฐ ๋ฐฐ์ด์ ์ด์ฉํด์ ํ์ํ ๊ฒฝ์ฐ์๋ง ๋ฌธ์์ด ๋ณต์ฌ
String joinWords(String[] words) {
StringBuilder sentence = new StringBuilder();
for (String w : words) {
sentence.append(w);
}
return sentence.toString();
}
๐ซง ๋ฒกํฐ ๊ตฌํํ๊ธฐ
โก๏ธ size() - ํญ๋ชฉ์ ๊ฐ์
public static void main(String[] args){
ArrayList<Object> sizeTest = new ArrayList<Object>();
System.out.println( sizeTest.size() ); // 0
}
โก๏ธ capacity() - ๋ค์ด๊ฐ ์ ์๋ ํญ๋ชฉ์ ์ต๋ ๊ฐ์
public static void main(String[] args) {
StringBuffer buff = new StringBuffer(20);
buff.append("GwangJik");
System.out.println("Capacity : " + buff.capacity()); // 20
}
โก๏ธ is_empty()
String test = null;
if(test != null && test.isEmpty()) {
System.out.println(test);
}
โก๏ธ at(index) - ์ธ๋ฑ์ค์ ์๋ ํญ๋ชฉ์ ๋๋ ค์ฃผ๊ณ , ์ธ๋ฑ์ค๊ฐ ๋ฒ์ ๋ฐ์ด๋ฉด ์๋ฌ๋ฅผ ๋
int digitStr = "456";
// 0๋ฒ์งธ์ ์๋ char ๊ฐ์ ๋ฆฌํด
digitStr.charAt(0) // 4
โก๏ธ push(item)
var fruit = ["์ฌ๊ณผ", "๋ฐฐ", "ํฌ๋"];
fruit.push("๋ธ๊ธฐ"); // ๋ฐฐ์ด ๋ค์ชฝ์ ๋ฐ์ดํฐ ์ฝ์
["์ฌ๊ณผ", "๋ฐฐ", "ํฌ๋", "๋ธ๊ธฐ"]
โก๏ธ insert(index, item) - index์ item์ ์ฝ์ ํ๊ณ ๊ธฐ์กด ์ธ๋ฑ์ค์ ๊ฐ๋ถํฐ ์ญ ์ค๋ฅธ์ชฝ์ผ๋ก ์ฌํํธ
public class ArrayListDemo {
public static void main(String[] args)
{
// create an empty list with an initial
// capacity
List<String> list = new ArrayList<String>(5);
// use add() method to initially
// add elements in the list
list.add("Geeks");
list.add("For");
list.add("Geeks");
// Add a new element at index 0
list.add(0, "Hello");
// prints all the elements available in list
for (String str : list) {
System.out.print(str + " ");
}
}
}
// Hello Geeks For Geeks
โก๏ธ prepend(item) - ๋งจ ์์ ์์๋ฅผ ์ฝ์
int[] arr = { 10, 20, 30 };
arr = ArrayUtils.add(arr, 40);
โก๏ธ pop() - ๋ง์ง๋ง ์์๋ฅผ ์ญ์ ํ๊ณ ๊ฐ์ ๋๋ ค์ค๋ค
var fruit = ["์ฌ๊ณผ", "๋ฐฐ", "ํฌ๋"];
fruit.pop(); // ๋ฐฐ์ด ๋ค์ชฝ์ ๋ฐ์ดํฐ ์ ๊ฑฐ ["์ฌ๊ณผ", "๋ฐฐ"]
โก๏ธ delete(index) - delete item at index, shifting all trailing elements left
import java.io.File; // Import the File class
public class DeleteFile {
public static void main(String[] args) {
File myObj = new File("filename.txt");
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}
// Deleted the file: filename.txt
โก๏ธ remove(item) - looks for value and removes index holding it (even if in multiple places)
String[] fruitsArray = {"apple", "banana", "kiwi", "mango"};
ArrayList<String> fruits = new ArrayList<>(Arrays.asList(fruitsArray));
System.out.println("all: " + fruits.toString());
String returned = fruits.remove(2);
System.out.println("remove(2): " + fruits.toString());
System.out.println("returned: " + returned);
// all: [apple, banana, kiwi, mango]
// remove(2): [apple, banana, mango]
// returned: kiwi
โก๏ธ find(item) - looks for value and returns first index with that value, -1 if not found
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BooleanFindMethodExample1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p=Pattern.compile("java");
Matcher m=p.matcher("HellojavaHellojava");
int c=0;
// finding matching char
while(m.find())
{
c=c+1;
System.out.println("Start position of matching String "+m.start());
System.out.println("End position of Matching String (java) "+m.end());
}
System.out.println(" Number of matches : "+c);
}
}
Pattern pat = Pattern.compile("์ฌ๊ธฐ์ ์ ๊ท์ ์
๋ ฅ");
ํจํด์ ์ ์ ํ ํ
Matcher match = pat.matcher("์ฌ๊ธฐ์ ์กฐ์ฌํ ๋ฌธ์์ด");
์ ์๋ ํจํด์ ๋งค์น ๋๋ ๊ฐ์ ์ ์ฅํ๋ค.
match.find()
๋งค์น๋ ๊ฐ์ด ์์ผ๋ฉด true ์์ผ๋ฉด false๋ฅผ ๋ฐํํ๋ค.
match.group()
๋งค์น๋ ๊ฐ์ ๋ฐํํ๋ค.
โก๏ธ resize(new_capacity) // private ํจ์
- ์ฉ๋์ด ๊ฝ ์ฐจ๋ฉด, ๊ทธ ๋๋ฐฐ๋ก ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ค
- item์ ํ๋ ๊บผ๋ผ ๋, ์ฉ๋์ด 1/4์ด๋ผ๋ฉด, ์ฉ๋์ ์ ๋ฐ์ผ๋ก ์ค์ธ๋ค
'๐ฉโ๐ป ์ปดํจํฐ ๊ตฌ์กฐ > Computer Science' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Coding Interview University] ํด์ํ ์ด๋ธ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.05 |
---|---|
[Coding Interview University] ํ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.03 |
[Coding Interview University] ์คํ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.01 |
[Coding Interview University] ๋งํฌ๋ ๋ฆฌ์คํธ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.06.29 |
[Coding Interview University] ๋น ์ค(Big-O)ํ๊ธฐ๋ฒ (0) | 2023.06.28 |