๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Coding Interview University] ๋ฐฐ์—ด : ์ด๋ก  ๋ฐ ๊ตฌํ˜„ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ์ปดํ“จํ„ฐ ๊ตฌ์กฐ/Computer Science

[Coding Interview University] ๋ฐฐ์—ด : ์ด๋ก  ๋ฐ ๊ตฌํ˜„

์ง•์ง•์•ŒํŒŒ์นด 2023. 6. 28. 15:05
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ์ฝ”๋”ฉ์ธํ„ฐ๋ทฐ๋Œ€ํ•™ ๋‹˜์˜ 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์ด๋ผ๋ฉด, ์šฉ๋Ÿ‰์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์ธ๋‹ค
728x90
๋ฐ˜์‘ํ˜•
Comments