짬뽕얼큰하게의 맨땅에 헤딩 :: 짬뽕얼큰하게의 맨땅에 헤딩

summary

  • Given an integer array, return true if there are three consecutive odd numbers in the array, otherwise return false.

approach

  • The solution uses a counter to keep track of the number of consecutive odd numbers
  • It iterates over the array, incrementing the counter whenever it encounters an odd number and resetting it whenever it encounters an even number
  • If the counter reaches 3, it immediately returns true
  • If it iterates over the entire array without finding three consecutive odd numbers, it returns false.

complexity

  • O(n) where n is the number of elements in the array

explain

  • The solution has a time complexity of O(n) because it makes a single pass over the array
  • The space complexity is O(1) because it uses a constant amount of space to store the counter.

Solution Code:


class Solution {
public:
    bool threeConsecutiveOdds(vector& arr) {
        int cnt = 0;
        for(int i = 0 ; i < arr.size(); i++){
            if(arr[i] & 1){
                cnt++;
            } else {
                cnt = 0;
            }
            if (cnt >= 3) return true;
        }
        return false;
    }
};
블로그 이미지

짬뽕얼큰하게

,

summary

  • This problem involves finding the maximum number of edges that can be removed from an undirected graph so that it can still be fully traversed by both Alice and Bob.

approach

  • The approach used in this solution is to first separate the edges into three types: Type 1 (can be traversed by Alice only), Type 2 (can be traversed by Bob only), and Type 3 (can be traversed by both Alice and Bob)
  • Then, we use disjoint sets to keep track of the connected components of the graph for each type of edge
  • We remove the maximum number of edges of Type 3 while ensuring that the graph remains fully traversable by both Alice and Bob.

complexity

  • O(n log n + m log m + n log n) where n is the number of nodes and m is the number of edges

explain

  • The solution starts by separating the edges into three types and initializing two disjoint sets for Alice and Bob
  • It then sorts the edges of each type by their type and uses the disjoint sets to keep track of the connected components of the graph
  • The solution then removes the maximum number of edges of Type 3 while ensuring that the graph remains fully traversable by both Alice and Bob
  • If the graph is not fully traversable, the solution returns -1
  • The time complexity is O(n log n + m log m + n log n) due to the sorting and disjoint set operations.

Solution Code:




class Solution {
public:
    int u1[100001];
    int u2[100001];
    
    vector> edgesType1;
    vector> edgesType2;
    static bool cmp1(vector &a, vector &b){
        return a[0] > b[0];
    }
    static bool cmp2(vector &a, vector &b){
        return a[0] > b[0];
    }
    int disjoint(int* u, int x){
        if(u[x] == x) return x;
        return u[x] = disjoint(u, u[x]);
    }
    int maxNumEdgesToRemove(int n, vector>& edges) {
        edgesType1.clear();
        edgesType2.clear();
        int edgeSize = edges.size();
        for(int i = 1; i <= n; i++){
            u1[i] = i;
            u2[i] = i;
        }
        for(int i = 0; i < edges.size(); i++){
            if(edges[i][0] == 1){
                edgesType1.push_back(edges[i]);
            } else if (edges[i][0] == 2){
                edgesType2.push_back(edges[i]);
            } else{
                edgesType1.push_back(edges[i]);
                edgesType2.push_back(edges[i]);
            }
        }
        sort(edgesType1.begin(), edgesType1.end(), cmp1);
        sort(edgesType2.begin(), edgesType2.end(), cmp2);

        int type3 = 0;
        int cnt = 0;
        for (int idx = 0 ; idx < edgesType1.size(); idx++) {
            int type = edgesType1[idx][0];
            int a = edgesType1[idx][1];
            int b = edgesType1[idx][2];
            if(disjoint(u1, a) == disjoint(u1, b)){
                continue;
            }
            cnt++; 
            u1[disjoint(u1, a)] = disjoint(u1, b);
            if (type == 3){
                type3 += 1;
            }
            if (cnt == n -1) break;
        }
        if (cnt != n-1) return -1;
        cnt = 0;
        for (int idx = 0 ; idx < edgesType2.size(); idx++) {
            int type = edgesType2[idx][0];
            int a = edgesType2[idx][1];
            int b = edgesType2[idx][2];
            if(disjoint(u2, a) == disjoint(u2, b)){
                continue;
            }
            cnt++; 
            u2[disjoint(u2, a)] = disjoint(u2, b);
            if (cnt == n -1) break;
        }
        if (cnt != n-1) return -1;

        return edges.size() - (n - 1) - (n - 1) + type3;
    }
};

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

Intersection of Two Arrays II  (0) 2025.02.02
Three Consecutive Odds  (0) 2025.02.02
Find Center of Star Graph  (0) 2025.02.02
Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit  (0) 2025.02.02
Special Array I  (0) 2025.02.02
블로그 이미지

짬뽕얼큰하게

,

summary

  • Finding the Center of a Star Graph

approach

  • The solution iterates over each edge in the graph and increments the count of the nodes at each end of the edge
  • It then checks if the count of any node exceeds 1
  • If so, that node is the center of the star graph and the function returns it.

complexity

  • O(n)

explain

  • The time complexity of this solution is O(n) because in the worst case, we might have to iterate over all n edges
  • The space complexity is also O(n) because in the worst case, we might have to store the count of all n nodes.

Solution Code:


class Solution {
public:
    int findCenter(vector>& edges) {
        int cnt[100001] = {0};
        for(int i = 0 ; i < edges.size(); i++){
            int a = edges[i][0];
            int b = edges[i][1];
            cnt[a]++;
            cnt[b]++;
            if (cnt[a] >= 2) return a;
            if (cnt[b] >= 2) return b;
        }
        return 0;
    }
};
블로그 이미지

짬뽕얼큰하게

,