LeetCode //C - 354. Russian Doll Envelopes

news/2024/9/23 6:26:31/

354. Russian Doll Envelopes

You are given a 2D array of integers envelopes where envelopes[i] = [ w i , h i w_i, h_i wi,hi] represents the width and the height of an envelope.

One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope’s width and height.

Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).

Note: You cannot rotate an envelope.
 

Example 1:

**Input:**envelopes = [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

Example 2:

Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1

Constraints:
  • 1 < = e n v e l o p e s . l e n g t h < = 1 0 5 1 <= envelopes.length <= 10^5 1<=envelopes.length<=105
  • envelopes[i].length == 2
  • 1 < = w i , h i < = 1 0 5 1 <= wi, hi <= 10^5 1<=wi,hi<=105

From: LeetCode
Link: 354. Russian Doll Envelopes


Solution:

Ideas:

1. Sort the Envelopes:

  • Sort the envelopes by width in ascending order. If two envelopes have the same width, sort them by height in descending order. This way, envelopes with the same width can’t be placed one inside the other, which simplifies the logic for the LIS step.

2. Find the Longest Increasing Subsequence:

  • After sorting, the problem boils down to finding the longest increasing sequence based on the height. This can be efficiently performed using a dynamic programming approach or more optimally with a method that uses binary search.

3. Dynamic Programming with Binary Search:

  • You can use a binary search technique to maintain a list of potential ending heights for LIS of different lengths, improving the time complexity from O(n^2) (pure dynamic programming) to O(n log n).
Code:
// Comparator function for qsort
int compare(const void *a, const void *b) {int *envelopeA = *(int **)a;int *envelopeB = *(int **)b;if (envelopeA[0] == envelopeB[0]) // If widths are the same, sort by height in descending orderreturn envelopeB[1] - envelopeA[1];return envelopeA[0] - envelopeB[0]; // Otherwise, sort by width in ascending order
}int maxEnvelopes(int** envelopes, int envelopesSize, int* envelopesColSize) {if (envelopesSize == 0) return 0;// Sort envelopesqsort(envelopes, envelopesSize, sizeof(int*), compare);// This will store the increasing heights (dynamic array for the potential LIS)int *dp = (int *)malloc(envelopesSize * sizeof(int));int len = 0;for (int i = 0; i < envelopesSize; i++) {int height = envelopes[i][1];// Binary search for the height in the dp arrayint left = 0, right = len;while (left < right) {int mid = (left + right) / 2;if (dp[mid] < height)left = mid + 1;elseright = mid;}// Replace element or append new lengthif (right >= len) {dp[len++] = height;} else {dp[right] = height;}}free(dp); // Clean up the dynamic arrayreturn len;
}

http://www.ppmy.cn/news/1428142.html

相关文章

Docker - HelloWorld

原文地址&#xff0c;使用效果更佳&#xff01; Docker - HelloWorld | CoderMast编程桅杆https://www.codermast.com/dev-tools/docker/docker-helloworld.html 开始之前 在学习本小节之前&#xff0c;你必须确保你正确安装了 Docker&#xff0c;正确安装 Docker 是后续学习的…

Docker容器逃逸-特权模式-危险挂载-Procfs

Docker容器逃逸-特权模式-危险挂载 Docker这个概念&#xff1a; Docker 容器与虚拟机类似&#xff0c;但二者在原理上不同&#xff0c;容器是将操作系统层虚拟化&#xff0c;虚拟机则是虚拟化硬件&#xff0c;因此容器更具有便携性、高效地利用服务器。 ‍ Docker会遇到的安…

每日一题(4.20)

目录 Leecode-118-杨辉三角题目示例解题思路代码实现 Leecode-238-除自身以外数组的乘积题目示例解题思路代码实现 Leecode-118-杨辉三角 题目 给定一个非负整数 numRows&#xff0c;生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中&#xff0c;每个数是它左上方和右上…

vue-Router 路由(常量路由)

1、安装 pnpm i vue-router 2、新建文件&#xff1a;src/routes.ts import { RouteRecordRaw } from vue-routerexport const constantRoute: RouteRecordRaw[] [{//path: /,redirect: /login,},{//path: /login,component: () > import(/views/Login/index.vue),name…

Android--ConnectivityManager使用

一、前言 Android10之后官方废弃了通过WifiManager连接WIFI的方式&#xff0c;现在要使用ConnectivityManager连接WIFI 二、连接WIFI public class MainActivity extends AppCompatActivity {private static final String TAG"lkx";Overrideprotected void onCrea…

uniapp uni.navigateBack 连带返回问题记录

uniapp uni.navigateBack 连带返回问题记录 问题描述 去除原生导航栏&#xff0c;使用自定义导航&#xff0c;并使用自定义返回按钮&#xff0c;通过方法handleBack.navigateBack()返回到上一页。 共有3层页面&#xff0c;A -> B -> C。都是自定义导航栏。均使用navig…

APP开发_ js 控制手机横屏或竖屏

1 Android 控制手机横屏或者竖屏的方法 1.1 配置 AndroidManifest.xml 以横屏模式为例&#xff1a; 在 Android 开发中&#xff0c;如果想让应用或某个特定的 Activity 在运行时以横屏模式显示&#xff0c;可以通过修改 Activity 的 AndroidManifest.xml 文件中的配置来实现…

pandas 中的 tolist() 和 to_list()

在使用pandas的时候&#xff0c;有时候会需要将pandas中的数据类型转换为python中的list&#xff0c;而pandas也提供了tolist()和to_list()这两个方法来实现这一需求 几乎可以认为pandas中的tolist()和to_list()用法没有差别 还顺便介绍了numpy中的tolist()方法&#xff0c;其…