class Solution {
public:
int pivotIndex(vector<int>& nums) {
    int total = 0;
    for (int num : nums) {
        total += num;
    }
    
    int leftSum = 0;
    for (int i = 0; i < nums.size(); i++) {
        int rightSum = total - leftSum - nums[i];
        if (leftSum == rightSum) {
            return i;
        }
        leftSum += nums[i];
    }
    
    return -1;
}
};

 

rightSum 이 total - leftSum - nums[i]; 라는 사실이 중요하다

 

[1,7,3,6,5,6]

#1

leftSum = 0 ,  num[0] = 1, rightSum = total - leftSum - num[0] = num[1] + num[2] + num[3] + num[4] + num[5];

 

#2

[1]                               [7]                [3,6,5,6]

leftSum = 0 + num[0],  num[1] = 7, rightSum = total - leftSum  - num[1] = num[2] + num[3] + num[4] + num[5];

'알고리즘 > 리트코드' 카테고리의 다른 글

392. Is Subsequence  (0) 2023.03.14
1480. Running Sum of 1d Array  (0) 2023.03.08
409. Longest Palindrome  (0) 2023.02.28
231.Power of Two  (0) 2023.02.28
122. Best Time to Buy and Sell Stock II  (0) 2021.09.06

1. 레벤슈타인 거리

2. longest palindrome

 

'스터디 > 코딩스터디' 카테고리의 다른 글

23/03/14 C++ String  (0) 2023.03.14

레벤슈타인 거리에 대해 쉽게 설명 된 블로그가 있어 복붙 하여 참고함.

다만 JAVA로 짠 코드가 잘 못된 부분이 있어 Python 코드로 새로 짰음. 

처음 비교 대상은 공집합과 공집합이다. 둘 다 같은 문자열이기 때문에 비용은 0이다. 그 다음은 공집합과 "나" 이다. 공집합이 "나"가 되려면 비용이 1이다. 그 다음은 공집합과 "나는"이다. 마찬가지로 비용이 2가든다. 계속 진행하면 위와 같이 표가 완성된다.

 

이제 "너" 와 공집합 - "나" - "나는" - "나는 "... - "나는 너를 좋아해!"를 비교해보자.

"너"와 공집합을 보자.  '너'가 {}이 되려면 문자를 삭제해야 한다. 그러므로 비용 1

"너"와 "나"를 보자. '너'와 '나'는 서로 다르기 때문에 교체해야 한다. 그러므로 비용 1

"너"와 "나는"을 보자. 길이가 다르기 때문에 추가해야 하고, 교체해야 한다. 그러므로 비용 2

"너"와 "나는 "을 보자. 역시 길이가 다르기 때문에 2개 추가해야 하고, 교체해야 한다. 그러므로 비용 3

"너"와 "나는 너"를 보자. 길이가 다르기 때문에 문자 3개를 추가해야 하지만, '너'는 서로 같기 때문에 그대로 둔다. 그러므로 비용 3

이런식으로 "나는 너를 좋아해!"까지 표를 완성하면 위와 같이 된다.

 

한번만 더 해보자. "너는"과 공집합 - "나" - "나는" - "나는 " - ... - "나는 너를 좋아해!"를 비교해보자

"너는"과 공집합을 비교해보자. "너는"이 {}이 되려면 문자 두개를 삭제해야 한다. 그러므로 비용 2

"너는"과 "나"를 비교해보자. 문자 한개 삭제와 교체가 필요하다. 그러므로 비용 2

"너는"과 "나는"을 비교해보자. '는'은 서로 같고, '너'와 '나'는 다르기 때문에 교체가 필요하다. 그러므로 비용 1

"너는"과 "나는 "을 비교해보자. 추가, 교체가 필요하다. 그러므로 비용 2

 

이런식으로 표를 완성하면 다음과 같이 된다.

쉽게 정리하면

  1. 글자가 서로 동일하면 대각선 값을 가져온다
  2. 변경이 필요하면 대각선 값에서 + 1을 한다.
  3. 삽입이 필요하면 위의 값에서 +1을 한다.
  4. 삭제가 필요하면 왼쪽 값에서 +1을 한다.
  5. 이전까지의 값에서 대각선 위 왼쪽 에서 최소로 바뀔 수 있는 값을 참고한다.
  6. 그래야 가장 가까운 값이니깐.

 

결론 :

 

코드 : 

 

def levenshtein_distance(t1, t2):
    # Create a 2D array to store the minimum edit distances between all prefixes of t1 and t2.
    rows, columns = len(t1) + 1, len(t2) + 1
    arr = [[0 for j in range(columns)] for i in range(rows)]
    
    # Initialize the first row and column of the array to be the edit distances required to transform an empty string to the corresponding prefixes of t1 and t2, respectively.
    for i in range(rows):
        arr[i][0] = i
    for j in range(columns):
        arr[0][j] = j
    
    # Compute the edit distances for longer prefixes of t1 and t2 by considering the minimum edit distance among three possible operations: deletion, insertion, and substitution.
    for i in range(1, rows):
        for j in range(1, columns):
            if t1[i-1] == t2[j-1]:
                # If the characters at positions i-1 and j-1 are equal, there is no need to perform a substitution.
                # The minimum edit distance is equal to the edit distance between the prefixes up to positions i-2 and j-2.
                arr[i][j] = arr[i-1][j-1]
            else:
                # Otherwise, compute the minimum edit distance by adding 1 to the minimum edit distance among the three possible operations.
                deletion = arr[i-1][j]
                insertion = arr[i][j-1]
                substitution = arr[i-1][j-1]
                arr[i][j] = min(deletion, insertion, substitution) + 1
    
    # Print the entire 2D array.
    for row in arr:
        print(row)
    
    # Return the minimum edit distance between the entire strings t1 and t2.
    return arr[rows-1][columns-1]

levenshtein_distance('abdc','asdf')
 

 

레벤슈타인 알고리즘을 사용해서 엑셀 파일들을 파일명에 따라 분리하는 코드 파이썬으로 짜줘

 

 

먼저, 파이썬에서 엑셀 파일을 다루기 위해서는 openpyxl이나 pandas와 같은 라이브러리를 사용해야 합니다. 또한, 레벤슈타인 알고리즘을 사용하기 위해서는 jellyfish 라이브러리를 설치해야 합니다.

아래는 엑셀 파일들을 파일명에 따라 분리하는 코드입니다. 이 코드는 파일명에 대해 레벤슈타인 거리를 계산하고, 일정 거리 이내에 있는 파일들을 같은 폴더에 저장합니다.

import os
import shutil
import jellyfish

# 폴더 경로와 레벤슈타인 거리의 기준값을 지정합니다.
folder_path = "/path/to/folder"
threshold = 2

# 폴더 내의 파일들을 모두 불러옵니다.
file_names = os.listdir(folder_path)

# 파일명에 대해 레벤슈타인 거리를 계산하여 같은 그룹에 속하는 파일들을 찾습니다.
groups = {}
for i, name in enumerate(file_names):
    for j in range(i+1, len(file_names)):
        if jellyfish.levenshtein_distance(name, file_names[j]) <= threshold:
            # 동일한 그룹을 위해 새로운 파일 그룹을 추가합니다.
            if name not in groups:
                groups[name] = set([name])
            groups[name].add(file_names[j])

# 같은 그룹에 속하는 파일들을 하나의 폴더에 저장합니다.
for group in groups.values():
    # 공통 접두사를 폴더 이름으로 사용합니다.
    folder_name = os.path.commonprefix(group)
    new_folder_path = os.path.join(folder_path, folder_name)
    # 폴더가 없는 경우 새로 생성합니다.
    os.makedirs(new_folder_path, exist_ok=True)
    for file_name in group:
        file_path = os.path.join(folder_path, file_name)
        new_file_path = os.path.join(new_folder_path, file_name)
        # 파일을 새 폴더로 복사합니다.
        shutil.copyfile(file_path, new_file_path)

이 파이썬 스크립트는 파일의 레벤슈타인 거리(두 문자열 사이의 유사성 측정 지표)를 기반으로 폴더 내의 파일을 그룹화하고, 공통 접두사를 가진 새로운 폴더에 저장합니다. 다음은 코드가 작동하는 방식을 설명하는 주석입니다.

import os
import shutil
import jellyfish

# 폴더 경로와 레벤슈타인 거리의 기준값을 지정합니다.
folder_path = "/path/to/folder"
threshold = 2

# 폴더 내의 파일들을 모두 불러옵니다.
file_names = os.listdir(folder_path)
  • os 모듈은 운영 체제와 관련된 작업을 위해 가져오고, shutil 모듈은 파일 작업에 사용됩니다. 그리고 jellyfish 모듈은 레벤슈타인 거리를 계산하는 데 사용됩니다.
  • folder_path 변수는 파일이 들어 있는 폴더의 경로로 설정됩니다.
  • threshold 변수는 동일한 그룹으로 간주할 파일들의 최대 레벤슈타인 거리로 설정됩니다.
  • os.listdir() 함수는 폴더 내 모든 파일의 목록을 가져오는 데 사용됩니다.
# 파일명에 대해 레벤슈타인 거리를 계산하여 같은 그룹에 속하는 파일들을 찾습니다.
groups = {}
for i, name in enumerate(file_names):
    for j in range(i+1, len(file_names)):
        if jellyfish.levenshtein_distance(name, file_names[j]) <= threshold:
            if name not in groups:
                groups[name] = set([name])
            groups[name].add(file_names[j])
  • groups 딕셔너리는 공통 접두사를 가진 파일 그룹을 저장합니다.
  • 중첩된 루프는 각 파일 이름 쌍의 레벤슈타인 거리를 비교하는 데 사용됩니다.
  • 거리가 임계값 이하인 경우, 파일이 동일한 그룹에 속한다고 판단됩니다.
  • 파일 이름은 groups 딕셔너리에 추가됩니다.
# 같은 그룹에 속하는 파일들을 하나의 폴더에 저장합니다.
for group in groups.values():
    folder_name = os.path.commonprefix(group)
    new_folder_path = os.path.join(folder_path, folder_name)
    os.makedirs(new_folder_path, exist_ok=True)
    for file_name in group:
        file_path = os.path.join(folder_path, file_name)
        new_file_path = os.path.join(new_folder_path, file_name)
        shutil.copyfile(file_path, new_file_path)
  • 루프를 사용하여 각 파일 그룹을 반복합니다.
  • 그룹의 공통 접두사는 새 폴더의 폴더 이름으로 사용됩니다.
  • os.makedirs() 함수를 사용하여 새 폴더를 생성합니다. 이미 폴더가 존재하면 exist_ok=True 인자로 설정하여 에러 없이 진행합니다.
  • 그룹 내 파일은 shutil.copyfile() 함수를 사용하여 새 폴더에 복사됩니다.

 

 
 
 

 

'알고리즘' 카테고리의 다른 글

KMP/ ROBIN KMP  (0) 2023.03.22

class Solution:
    def longestPalindrome(self, s: str) -> int:
        dic = {}
        res = 0
        is_Odd = 0
        big_Odd = 0
        for s_index in s :
            if s_index in dic :
               
                dic[s_index] = int(dic[s_index]) + 1
            else :

                dic[s_index] = 1

        for value in dic.values() :
            if value % 2 == 0 :
                res = res + value
            else :
                res = res + value - 1
                is_Odd = 1

        if is_Odd :
            return res + 1
        else :
            return res

'알고리즘 > 리트코드' 카테고리의 다른 글

392. Is Subsequence  (0) 2023.03.14
1480. Running Sum of 1d Array  (0) 2023.03.08
724. Find Pivot Index  (0) 2023.03.08
231.Power of Two  (0) 2023.02.28
122. Best Time to Buy and Sell Stock II  (0) 2021.09.06

n 이 2의 지수로 표현이 되는 지 확인.

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n <= 0:
            return False
        ## 전제 조건 2의 지수는 2로 계속 정확히 나눠진다.
        while n > 1: ## 몫이 1 보다 클때
            if n % 2 == 1: ##몫이 홀수라면 False
                return False
            n = n // 2 ## 몫을 다시 대입
        
        return True
 

 

'알고리즘 > 리트코드' 카테고리의 다른 글

392. Is Subsequence  (0) 2023.03.14
1480. Running Sum of 1d Array  (0) 2023.03.08
724. Find Pivot Index  (0) 2023.03.08
409. Longest Palindrome  (0) 2023.02.28
122. Best Time to Buy and Sell Stock II  (0) 2021.09.06

1. 라이다의 모터 회전을 통해서 얻어진 포인트를 xyz 좌표 변환을 하지 않고, 각 포인트의 주변점을 검사함으로써

라이다와의 거리가 특정값 이상이 되면 제거하는 로직 개발

 

- 장점 :

하나의 Frame 에 대해서만 검사하여 움직이는 물체에 대한 이슈를 고려 안해도 됨.

xyz 좌표로써 포인트간의 거리를 측정하는 것이 아닌 라이다와 포인트간의 거리(이미 가지고 있는 데이터)로 판단하기 때문에 처리 시간이 짧음.

 

- 단점 :

Noise 두개의 점이 가까운 거리에서 뜨면 제거 하기가 힘듬.

'어플리케이션 > 라이다' 카테고리의 다른 글

3D 포인트 K-D Tree 구현  (0) 2021.09.07

3D 포인트 K-D Tree 구현

 

CrossTalk을 없애기 위한 논문 참조

 

1. Mitigation of crosstalk effects in multi-LiDAR configurations

 

 

논문상의 Kd-tree 구현

- 첫번째 시도 

https://codingstorywithme.tistory.com/3

 

C를 이용한 BST (Binary Search Tree) 와 KdTree(K Dimension Tree)

K-dtree 와 BST에 대해 알아보겠습니다! 이미 한 번 하다가 꺼졌는데,, 임시저장이 안되서 다시 처음부터 적어볼게요..흑흑 #BST - 기본 Binary Search Tree 의 약자로 위와 같이 최대 두개의 자녀 노드를

codingstorywithme.tistory.com

코드상에 KD tree를 만드는 부분과 서칭하는 부분에서 처리 시간이 많이들어 실시간성 확보가 힘들었음.

 

- 두번째 시도

https://rosettacode.org/wiki/K-d_tree

 

K-d tree - Rosetta Code

K-d tree You are encouraged to solve this task according to the task description, using any language you may know. A k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees ar

rosettacode.org

최적화된 C 코드를 사용하여 처리 시간을 최적화 시켰음. ( 정렬 사용 X , http://www.cplusplus.com/reference/algorithm/nth_element/ 사용)

 

추후 임베디드화 해야할 필요성이 있음.

 

 

설계

 

 

구현

'어플리케이션 > 라이다' 카테고리의 다른 글

Radius Outlier Removal - 주변 Point 검사  (0) 2021.10.15

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.

Find and return the maximum profit you can achieve.

 

Example 1:

Input: prices = [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.

 

Example 2:

Input: prices = [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.

 

Example 3:

Input: prices = [7,6,4,3,1] Output: 0 Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

 

Constraints:

  • 1 <= prices.length <= 3 * 104
  • 0 <= prices[i] <= 104

 

'알고리즘 > 리트코드' 카테고리의 다른 글

392. Is Subsequence  (0) 2023.03.14
1480. Running Sum of 1d Array  (0) 2023.03.08
724. Find Pivot Index  (0) 2023.03.08
409. Longest Palindrome  (0) 2023.02.28
231.Power of Two  (0) 2023.02.28

+ Recent posts