hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

PS/PS Log

23.04.18. 풀었던 문제들

Leetcode 1768. Merge Strings Alternately

 string 2개를 index 하나씩 합치는 문제. merge-sort를 알고 있다면 merge하는 방식으로 쉽게 구현할 수 있다.

// Runtime 2 ms Beats 50.88%
// Memory 6.5 MB Beats 19.57%

class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        int i = 0, j = 0, len1 = word1.length(), len2 = word2.length();
        string result = "";
        while(i < len1 && j < len2){
            result += word1[i]; i++;
            result += word2[j]; j++;
        }
        while(i < len1){
            result += word1[i]; i++;
        }
        while(j < len2){
            result += word2[j]; j++;
        }
        return result;
    }
};

 

시간복잡도

 word1, word2를 각각 한 번씩 순회하므로 word1 length를 n, word2 length를 m이라 두면 O(n+m)이다.

 

공간복잡도

 result string의 크기는 n+m이므로 O(n+m)

 

 

 

 

 

 

저작자표시 (새창열림)

'PS > PS Log' 카테고리의 다른 글

23.04.20. 풀었던 문제들  (0) 2023.04.20
23.04.19. 풀었던 문제들  (0) 2023.04.19
23.04.17. 풀었던 문제들  (0) 2023.04.17
23.04.16. 풀었던 문제들  (0) 2023.04.16
23.04.14. 풀었둔 문제들  (0) 2023.04.14
    hyelie
    hyelie

    티스토리툴바