짬뽕얼큰하게의 맨땅에 헤딩 :: '2025/03/01 글 목록

'2025/03/01'에 해당되는 글 2건

Leetcode Problem:

Summary

  • The problem requires applying operations to a given array of non-negative integers, where in each operation, if the current element is equal to the next element, the current element is multiplied by 2 and the next element is set to 0
  • The operations are applied sequentially, and after all operations, the 0's are shifted to the end of the array.

Approach

  • The approach used is to iterate over the array, apply the operations, and then shift the 0's to the end
  • The operations are applied sequentially, and the 0's are shifted to the end by adding them to the end of the array.

Complexity

  • O(n), where n is the size of the array, because each element is visited once.

Explanation

  • The solution code starts by initializing an index to 0 and an empty array to store the result
  • It then enters a while loop that continues until the index is greater than or equal to the size of the array
  • Inside the loop, it skips the 0's by incrementing the index until it finds a non-zero element
  • If the current element is equal to the next element, it multiplies the current element by 2 and increments the index
  • It then pushes the current element to the result array and increments the index
  • After the while loop, it shifts the 0's to the end of the array by adding them to the end of the array
  • Finally, it returns the result array.

Solution Code:


class Solution {
public:
    vector applyOperations(vector& nums) {
        int idx = 0;
        vector ans;
        while(idx < nums.size()){
            while(idx < nums.size() && nums[idx] == 0){
                idx++;
            }
            if(idx >= nums.size()) break;
            int num = nums[idx];
            if((idx + 1) < nums.size() && (nums[idx] == nums[idx+1])){
                num <<= 1;
                idx++;
            }
            ans.push_back(num);
            idx++;
        }
        int ansSize = ans.size();
        while(ansSize < nums.size()){
            ans.push_back(0);
            ansSize++;
        }
        return ans;
    }
};
블로그 이미지

짬뽕얼큰하게

,

Leetcode Problem:

Summary

  • Given an undirected tree with n nodes, edges, values, and k, find the maximum number of components in any valid split of the tree such that the value of each connected component is divisible by k.

Approach

  • This solution uses a depth-first search (DFS) traversal to explore the tree and find the maximum number of components
  • It maintains two arrays, undirectAdj and directAdj, to store the adjacency list of the tree
  • The solution also uses two flags, visited and visited2, to keep track of visited nodes during the traversal
  • The traversal function is called recursively to explore the tree, and the solution updates the values of the nodes based on the divisibility of their sum by k.

Complexity

  • O(n log n) due to the sorting of the leaf nodes in the leafs array.

Explanation

  • The solution starts by building the adjacency list of the tree using the undirectAdj and directAdj arrays
  • Then, it calls the traversal function to explore the tree and find the leaf nodes
  • The traversal function uses a recursive approach to explore the tree, and it updates the values of the nodes based on the divisibility of their sum by k
  • The solution maintains two arrays, leafs, to store the leaf nodes of the tree
  • The solution iterates over the leaf nodes and updates their values based on the divisibility of their sum by k
  • Finally, the solution returns the maximum number of components in any valid split of the tree.

Solution Code:


class Solution {
public:
    vector undirectAdj[30000];
    vector directAdj[30000];
    vector leafs[2];
    bool visited[30000];
    bool visited2[30000];
    int in[30000];
    int succeed = 0;
    void traversal(int root){
        visited[root] = true;
        int nextCnt = 0;
        for(int i = 0 ; i < undirectAdj[root].size(); i++){
            int next = undirectAdj[root][i];
            if(visited[next]){
                continue;
            }
            nextCnt++;
            directAdj[next].push_back(root);
            in[root]++;
            traversal(next);
        }
        if(nextCnt == 0){
            leafs[0].push_back(root);
        }
    }
    int maxKDivisibleComponents(int n, vector>& edges, vector& values, int k) {
        
        for(int i = 0 ; i < edges.size(); i++){
            int a = edges[i][0];
            int b = edges[i][1];
            undirectAdj[a].push_back(b);
            undirectAdj[b].push_back(a);
        }
        int root = 0;
        traversal(root);
        int leafIdx = 0;
        int ans = 0;
        bool end = false;
        while(true){
            leafs[leafIdx^1].clear();
            for(int i = 0 ; i < leafs[leafIdx].size(); i++){
                int cur = leafs[leafIdx][i];
                if(succeed >= n-1 && cur == root){
                    end = true;
                    break;
                }
                if(cur == root){
                    continue;
                }
                int next = directAdj[cur][0];
                if(visited2[cur]) continue;
                visited2[cur] = true;
                succeed++;
                if(values[cur] % k){ 
                    values[next] = (values[next] + values[cur]) % k;
                } else{
                    ans++;
                }
                in[next]--;
                if(in[next] == 0){
                    leafs[leafIdx^1].push_back(next);
                }
            }
            if(end) break;
            leafIdx ^= 1;
        }
        
        if(values[root] % k){
            return 1;
        } else{
            return ans + 1;
        }
    }
};

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

Merge Two 2D Arrays by Summing Values  (0) 2025.03.02
Apply Operations to an Array  (0) 2025.03.01
Length of Longest Fibonacci Subsequence  (0) 2025.02.27
Reverse Odd Levels of Binary Tree  (0) 2025.02.27
Max Chunks To Make Sorted  (0) 2025.02.27
블로그 이미지

짬뽕얼큰하게

,