Skip to content
Open
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
93 changes: 93 additions & 0 deletions 13/JiYongKim/binarySearch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## 탐색 알고리즘의 종류

- 선형 탐색
- 이진 탐색
- 해시

<br>

## 선형 탐색(Linear Search) 알고리즘

- 정의 : 배열 전체를 하나씩 확인해 가며 탐색하는 알고리즘
- 시간 복잡도 : O(n)
- 특징
- 모든 데이터 타입에 사용 가능 (문자열, 숫자,…etc)
- 데이터의 양 n만큼의 저장 공간이 필요

<br>

## 이진 탐색 (Binary Search) 알고리즘

- 정의 : 전체 배열의 중앙을 비교하여 찾으려 하는 값이 작으면 왼쪽 크면 오른쪽 에서 부터 다시 탐색하는 방법으로 , 탐색양을 절반씩 줄여가며 찾는 탐색 알고리즘
- 시간 복잡도 : O(log n)

<br>

- 특징
- 전제 조건
- 정렬이 미리 되어있어야 한다
- 입력시마다 재정렬을 필요로 한다
- 데이터의 비교가 가능해야 한다.
- [ 출력 < 입력 ]의 상황시 입력시 마다 재정렬을 해야하기 때문에 느려질 수 있다.
- 분할 정복 기법 중 하나이다.

<Binary Search Source by Java>

```java
public class BinarySearch {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

binarySearch(2, arr);
}

public static void binarySearch(int iKey, int arr[]) {
int mid;
int left = 0;
int right = arr.length - 1;

while (right >= left) {
mid = (right + left) / 2;

if (iKey == arr[mid]) {
System.out.println(iKey + " is in the array with index value: " + mid);
break;
}

if (iKey < arr[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}

}
}
}

```

<br>

## 해시

- 정의 : 해싱함수를 통해 얻은 해시값을 저장 위치로 삼아 데이터를 탐색하는 방법
- 시간 복잡도 : O(1)
- 특징
- HashMap or HashTable 사용
- 모든 데이터에 사용이 가능하다
- 해시 충돌의 수를 줄이기 위하여 데이터 n 이상의 저장 공간이 필요하다
- 해시 충돌이 일어날 경우
- 해시충돌을 어떻게 예방 하느냐에 따라 시간복잡도가 변경될 수 있다

⇒ 대부분 느려진다라고 할 수 있다.


---

<br>

## Search Algorithm 정리

- [ 데이터 삽입 < 데이터 탐색 ] → 이진 탐색이 유리
- [ 데이터 삽입 > 데이터 탐색 ] → 선형 탐색이 유리
- 데이터를 저장할 용량이 커도 좋다 → 해시
273 changes: 273 additions & 0 deletions 13/JiYongKim/leetcode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
# Binary Search

<details>
<summary>704. Binary Search</summary>

- 704. Binary Search

```java
// 시간 복잡도 : O(log(n)) ... n = nums.length
// 공간 복잡도 : O(1)
class Solution {
public int search(int[] nums, int target) {
int result = binarySearch(target, nums);
if (nums[result] == target) {
return result;
}
return -1;
}
public static int binarySearch(int key, int arr[]) {
int mid = 0;
int left = 0;
int right = arr.length - 1;

while (right >= left) {
mid = (right + left) / 2;

if (key == arr[mid]) {
return mid;
}
if (key < arr[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return mid;
}
}
```

</details>

<br>

~~[35. Search Insert Position](https://leetcode.com/problems/search-insert-position/)~~

<br>

<details>
<summary>349. Intersection of Two Arrays</summary>

- 349. Intersection of Two Arrays

```java
// 시간 복잡도 : O( m^2 + n log(m) ) ... m = nums2.length , n = nums1.length
// 공간 복잡도 : O( n + m ) ... m = nums1의 set값 변환 값 수 , n = nums1,nums2 중복 값 수
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
List<Integer> result = new ArrayList<>();

Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
set.add(nums1[i]);
}

Arrays.sort(nums2);

for (int i : set) {
if(binarySearch(i ,nums2))
result.add(i);
}

return result.stream().mapToInt(
Integer::intValue).toArray();
}
public static Boolean binarySearch(int key, int arr[]) {
int mid;
int left = 0;
int right = arr.length -1;

while(right >= left){
mid = (right + left) / 2;

if (key == arr[mid]) {
return true;
}

if(key < arr[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}
return false;
}

}
```
</details>

<br>

<details>
<summary>350. Intersection of Two Arrays II</summary>

- 350. Intersection of Two Arrays II

```java
// 시간 복잡도 : O( m^2 + n log(m) ) ... m = nums2.length , n = nums1.length
// 공간 복잡도 : O( n ) .... n = nums1 과 nums2의 중복값 갯수
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {

List<Integer> list = new ArrayList<>();

Arrays.sort(nums2);

for (int i = 0; i < nums1.length; i++) {
if(binarySearch(nums1[i],nums2)){
list.add(nums1[i]);
Arrays.sort(nums2);
}
}

return list.stream().mapToInt(
Integer::intValue).toArray();;

}

public static Boolean binarySearch(int key, int arr[]) {
int mid;
int left = 0;
int right = arr.length -1;

while(right >= left){
mid = (right + left) / 2;

if (key == arr[mid]) {
arr[mid] = -1;
return true;
}

if(key < arr[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}
return false;
}
}
```
</details>

<br>

<details>
<summary>1346. Check If N and Its Double Exist</summary>

- 1346. Check If N and Its Double Exist

```java
// 시간 복잡도 : O(n^2 + n +log(n) ) ... n = arr.length
// 공간 복잡도 : O(1)
class Solution {
public boolean checkIfExist(int[] arr) {

Arrays.sort(arr); // n^2
for (int i = 0; i < arr.length; i++) { //n
if(arr[i] == 0){
if(arr[i+1] == 0){
return true;
}
continue;
}

if (arr[i] % 2 == 0) {
if(binarySearch(arr[i]/2,arr)){ //log n
return true;
}
}

}
return false;

}
public static Boolean binarySearch(int key, int arr[]) {
int mid;
int left = 0;
int right = arr.length -1;

while(right >= left){
mid = (right + left) / 2;

if (key == arr[mid]) {
return true;
}

if(key < arr[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}
return false;
}
}
```
</details>

<br>

<details>
<summary>1608. Special Array With X Elements Greater Than or Equal X</summary>

- 1608. Special Array With X Elements Greater Than or Equal X

```java
//시간 복잡도 : O(n^2 - n) ... n = nums.length
//공간 복잡도 : O(1)
class Solution {
public int specialArray(int[] nums) {

int n = nums.length;
int result = -1;

for(int i=1; i<=n; i++){
int count = 0;
for(int j=0; j<n; j++){
if(nums[j] >= i){
count++;
}
}
if(count == i){
return count;
}
}

return result;
}
}
```
</details>

<br>

<details>
<summary>2089. Find Target Indices After Sorting Array</summary>

- 2089. Find Target Indices After Sorting Array
```java
//시간 복잡도 : O(n^2 + n) ... n = nums.length
//공간 복잡도 : O(n) ... n = result.length
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {

List<Integer> result = new ArrayList<>();
Arrays.sort(nums);

for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
result.add(i);
}
}


return result;

}
}
```
</details>