302. Smallest Rectangle Enclosing Black Pixels [leetcode]

本文介绍了一种通过二分查找解决最小矩形面积问题的算法。该算法首先确定黑色像素区域的水平边界和垂直边界,然后计算边界之间的矩形面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]
and  x = 0 y = 2 ,

Return 6


Solution


最主要是知道一个定理   refer : here

If there are only one black pixel region, then in a projected 1D array all the black pixels are connected.

这样就用正常的 binary search 就好了

class Solution {
    int getHorizan(const vector<vector<char>>& image, int left, int right,  bool flag){
        
        while(left <= right){
            int mid = (left + right)/2 , k = 0;
            
            while(  k < image.size() && image[k][mid] == '0')  k++;
            
            if( k != image.size()){
                flag ? right = mid - 1 : left = mid + 1;
            }
            else {
                flag ? left = mid + 1 : right = mid - 1;
            }
        }
        return flag ? left : right;
    }
    
    int getVertical(const vector<vector<char>>& image, int top, int bottom, bool flag){
        
        while( top <= bottom){
            int mid = ( top + bottom) / 2 , k = 0;
            
            while(k < image[0].size() && image[mid][k] == '0' ){
                k++;
            }
            
            if( k != image[0].size() ){
               flag ? bottom = mid -1 : top = mid + 1;
            }
            else {
               flag ? top = mid + 1 : bottom = mid - 1;
            }
        }
        return flag ? top : bottom;
    }
    
public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        if(image.empty() || image[0].empty()) return 0;
        int M = image.size(), N = image[0].size();
        
        int leftBoard = getHorizan(image, 0, y, true);
        int rightBoard = getHorizan(image, y, N - 1, false);
        
        int topBoard = getVertical(image, 0, x, true);
        int bottomBoard = getVertical(image, x, M -1, false);

        
        return (rightBoard - leftBoard + 1) *( bottomBoard - topBoard + 1);
    }
};

Solution 2 

还有一个比较straigtforward的方法,就是  bfs
时间复杂度明显不如binary search 
需要注意几点

1. unordered_set 中的key不能是pair
2. 计算 visited 的idx时 , x*N, 而不是 x*M
class Solution {
public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        const int M = image.size(), N = image[0].size();
        
        queue<pair<int,int>> toVisit;
        vector<bool> visited ( M*N, false);
        
        vector<pair<int,int>> lib = {
            make_pair(0,1), make_pair(0,-1), make_pair(-1,0), make_pair(1,0)
        };
        
        int leftMost = y, rightMost = y , upMost = x, downMost = x;
        toVisit.push(make_pair(x,y));
        int idx = x*N + y;
        visited[idx] = true;
        
        while(!toVisit.empty()){
            pair<int,int> curPair = toVisit.front();
            int curX = curPair.first, curY = curPair.second;
            toVisit.pop();
            leftMost = min(leftMost, curY);
            rightMost = max(rightMost, curY);
            upMost = min(upMost, curX);
            downMost = max(downMost, curX);
            
            for( auto p : lib){
                int nextX = curX + p.first, nextY = curY + p.second;
                if(nextX == -1 ||nextX == M || nextY == N || nextY == -1) continue;
                
                int idx = nextX*N + nextY;
                if(!visited[idx] && image[nextX][nextY] == '1') {
                    toVisit.push(make_pair(nextX, nextY));
                    visited[idx] = true;
                }
            }
        }
        
        return (rightMost - leftMost + 1) * (downMost - upMost + 1);
        
    }
};
     





资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 在苹果的生态系统中,IAP(应用内购买)是苹果应用商店(App Store)中应用开发者常采用的一种盈利模式,允许用户在应用内直接购买虚拟商品或服务。苹果为开发者提供了一份详细的人民币(CNY)IAP定价表,这份定价表具有以下特点: 价格分级:定价表由多个价格等级组成,开发者可根据虚拟商品的价值选择相应等级,等级越高,价格越高。例如,低等级可能对应基础功能解锁,高等级则对应高级服务或大量虚拟道具。 税收与分成:苹果会从应用内购买金额中抽取30%作为服务费或佣金,这是苹果生态的固定规则。不过,开发者实际到手的收入会因不同国家和地区的税收政策而有所变化,但定价表中的价格等级本身是固定的,便于开发者统一管理。 多级定价策略:通过设置不同价格等级,开发者可以根据商品或服务的类型与价值进行合理定价,以满足不同消费能力的用户需求,从而最大化应用的总收入。例如,一款游戏可以通过设置不同等级的虚拟货币包,吸引不同付费意愿的玩家。 特殊等级:除了标准等级外,定价表还包含备用等级和特殊等级(如备用等级A、备用等级B等),这些等级可能是为应对特殊情况或促销活动而设置的额外价格点,为开发者提供了更灵活的定价选择。 苹果IAP定价表是开发者设计应用内购机制的重要参考。它不仅为开发者提供了标准的收入分成模型,还允许开发者根据产品特性设定价格等级,以适应市场和满足不同用户需求。同时,开发者在使用定价表时,还需严格遵守苹果的《App Store审查指南》,包括30%的分成政策、使用苹果支付接口、提供清晰的产品描述和定价信息等。苹果对应用内交易有严格规定,以确保交易的透明性和安全性。总之,苹果IAP定价表是开发者在应用内购设计中不可或缺的工具,但开发者也需密切关注苹果政策变化,以确保应用的合规运营和收益最大化。
"SmallestRectangle1"通常是指在计算机科学和算法领域中,寻找二维平面上的最小包围矩形的问题。这个问题可以有多种变体,但核心思想是在给定的一组点或形状中找到能够完全包含这些点或形状的最小矩形。 解决这个问题的方法可以包括以下步骤: 1. **确定点集的范围**: - 找到所有点的最小和最大x坐标以及最小和最大y坐标。 - 这些坐标确定了一个初始的矩形边界。 2. **旋转和计算面积**: - 通过旋转这些点,找到能够使矩形面积最小的旋转角度。 - 计算每个旋转角度下的矩形面积,找到最小值。 3. **使用凸包**: - 对于更复杂的情况,可以使用凸包算法来减少计算量。凸包是一组点集的最小凸多边形。 - 在凸包上应用旋转和面积计算。 以下是一个简单的Python示例代码,展示如何找到二维平面上的一组点的最小包围矩形: ```python import numpy as np def smallest_rectangle(points): # 找到点集的边界 min_x, min_y = np.min(points, axis=0) max_x, max_y = np.max(points, axis=0) # 初始矩形面积 initial_area = (max_x - min_x) * (max_y - min_y) # 旋转角度 angles = np.linspace(0, np.pi/2, 1000) min_area = initial_area best_angle = 0 for theta in angles: # 旋转矩阵 rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) # 旋转后的点 rotated_points = np.dot(points, rotation_matrix) # 旋转后的边界 min_x_rot, min_y_rot = np.min(rotated_points, axis=0) max_x_rot, max_y_rot = np.max(rotated_points, axis=0) # 旋转后的面积 area = (max_x_rot - min_x_rot) * (max_y_rot - min_y_rot) if area < min_area: min_area = area best_angle = theta return min_area, best_angle # 示例点集 points = np.array([[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]) area, angle = smallest_rectangle(points) print(f"最小面积: {area}, 最佳旋转角度: {angle}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值