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

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

Leetcode Problem:

Summary

  • Finding Maximum Points in a Grid Based on Queries

Approach

  • This solution uses a priority queue to keep track of the cells in the grid that need to be visited
  • The priority queue is implemented using a binary heap
  • The solution starts from the top left cell and keeps track of the maximum points that can be obtained for each query
  • The time complexity is O(m*n*log(m*n)), where m and n are the dimensions of the grid.

Complexity

  • O(m*n*log(m*n))

Explanation

  • The solution first initializes a visited matrix to keep track of the cells that have been visited
  • It also initializes a priority queue with the top left cell of the grid
  • Then, it enters a loop where it pops the cell with the maximum value from the priority queue and marks it as visited
  • For each cell, it checks all its adjacent cells and pushes them into the priority queue if they have not been visited before
  • Finally, it calculates the maximum points that can be obtained for each query by summing up the points obtained for each cell in the priority queue.

Solution Code:


struct _node{
    int r;
    int c;
    int v;
};

template 
struct _heap{
    T arr[1000001];
    int size;
    _heap(){
        size = 1;
    }
    void push(T a){
        arr[size++] = a;
        int idx = size - 1;
        while(idx > 1 && arr[idx/2].v > arr[idx].v){
            T t = arr[idx/2];
            arr[idx/2] = arr[idx];
            arr[idx] = t;
            idx /= 2;
        }
    }
    T pop(){
        T ret = arr[1];
        arr[1] = arr[--size];
        int i = 1;
        while(true){
            int j = i * 2;
            if(j >= size) break;
            if(j + 1 < size && arr[j].v > arr[j+1].v){
                j++;
            }
            if(arr[i].v > arr[j].v){
                T t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
                i = j;
            } else{
                break;
            }
        }
        return ret;
    }
    bool empty(){
        return size == 1;
    }
};

class Solution {
public:
    _heap<_node> h;
    bool visited[1001][1001];
    int answer[1000001] = {0};
    vector maxPoints(vector>& grid, vector& queries) {
        vector ans;
        int cur_query = 0;
        int dy[] = {0, 1, 0, -1};
        int dx[] = {1, 0, -1, 0};

        visited[0][0] = true;
        h.push({0, 0, grid[0][0]});
        
        while(!h.empty()){
            _node nod = h.pop();
            cur_query = max(cur_query, nod.v);
            answer[cur_query]++;
            for(int i = 0 ; i < 4; i++){
                int nr = nod.r + dy[i];
                int nc = nod.c + dx[i];
                if(nr < 0 || nr >= grid.size() || nc < 0 || nc >= grid[0].size() || visited[nr][nc]){
                    continue;
                }
                visited[nr][nc] = true;
                h.push({nr, nc, grid[nr][nc]});
            }
        }
        for(int i = 1 ; i < 1000001; i++){
            answer[i] = answer[i-1] + answer[i];
        }
        for(int i = 0 ; i < queries.size(); i++){
            ans.push_back(answer[queries[i]-1]);
        }
        return ans;
    }
};

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

Partition Labels  (0) 2025.03.30
Apply Operations to Maximize Score  (0) 2025.03.29
Minimum Index of a Valid Split  (0) 2025.03.28
Minimum Operations to Make a Uni-Value Grid  (0) 2025.03.27
Check if Grid can be Cut into Sections  (0) 2025.03.25
블로그 이미지

짬뽕얼큰하게

,

Leetcode Problem:

Summary

  • Finding the minimum index of a valid split in an array where more than half of the elements have the same value.

Approach

  • The approach used is to iterate over the array and maintain two maps, left and right, to store the count of each number
  • We then iterate over the array again, pushing the current number to the right map and popping it from the left map
  • If the count of the current number in both maps is more than half of the remaining elements, we return the current index
  • If no valid split is found, we return -1.

Complexity

  • O(n log n) due to the use of maps and the nested loop structure

Explanation

  • The solution uses two maps, left and right, to store the count of each number in the array
  • We iterate over the array twice, once to populate the maps and once to find the valid split
  • In the first iteration, we push the current number to the right map and pop it from the left map
  • In the second iteration, we check if the count of the current number in both maps is more than half of the remaining elements
  • If it is, we return the current index
  • If not, we continue to the next iteration
  • If no valid split is found, we return -1.

Solution Code:


struct _node{
    int num;
    int cnt;
};

struct _map{
    vector<_node> m[10000];
    void push(int num){
        int idx = num % 10000;
        for(int i = 0 ; i < m[idx].size(); i++){
            if(m[idx][i].num == num){
                m[idx][i].cnt++;
                return;
            }
        }
        m[idx].push_back({num, 1});
    }
    int getCnt(int num){
        int idx = num % 10000;
        for(int i = 0 ; i < m[idx].size(); i++){
            if(m[idx][i].num == num){
                return m[idx][i].cnt;
            }
        }
        return -1;
    }
    void pop(int num){
        int idx = num% 10000;
        for(int i = 0 ; i < m[idx].size(); i++){
            if(m[idx][i].num == num){
                m[idx][i].cnt--;
                if(m[idx][i].cnt == 0){
                    m[idx][i] = m[idx][m[idx].size() - 1];
                    m[idx].pop_back();
                }
            }
        }
    }
};

class Solution {
public:
    _map left;
    _map right;
    int minimumIndex(vector& nums) {
        for(int i = 0 ; i < nums.size(); i++){
            right.push(nums[nums.size() - i - 1]);
        }
        for(int i = 0 ; i < nums.size(); i++){
            left.push(nums[i]);
            right.pop(nums[i]);
            int n = left.getCnt(nums[i]);
            if(n * 2 <= (i + 1)){
                continue;
            }
            n = right.getCnt(nums[i]);
            if(n == -1) continue;
            if((n * 2) > (nums.size() - i - 1)){
                return i;
            }
        }
        return -1;
    }
};
블로그 이미지

짬뽕얼큰하게

,