九章算法7:Array & Numbers

背包九章(动态规划学习) 背包九章论述了背包问题以及其衍生的动态规划问题,有一定的深度,尤其到后面几个章节。适合于志在acm的同仁们学习。 立即下载

九章算法7:Array & Numbers

内容基于九章算法课件
(http://www.jiuzhang.com/)

Merge two sorted array

Given two sorted integer arrays A and B, merge B into A as one sorted array.
A = [1, 2, 3, empty, empty], B = [4, 5]
After merge, A will be filled as [1, 2, 3, 4, 5]

我的第一反应,直接insert啊…naive啊…
老师说:数组问题, 最好不要进行数组的整体移动的操作.
所以我们这次不谁小谁出列,我们进行谁大谁出列,从后面进行操作~
遇到新题的思维方法:

   1.如果比较直接,如上面的 merge two sorted array, 但是好像直接做又不是那么特别好,可以考虑在直接的第一眼思维得出的解法上面稍加变动, 比如: 从左到右变成从右到左, 最小变最大, 一个指针变俩....
   2.如果不是很直接的,考虑和之前哪道题像亲戚.比如合并k个数组就可以联想道合并2个数组,能不能在亲戚题的解法上进行改进解决这道题.
   3.如果不直接又"举目无亲",根据题目的叙述看神马学过的算法或者数据结构貌似可以用上的,比如:求最大,最小,方案数以及可行性的想动态规划,k个数求最小用heap
   4.如果还是想不出来,试试其它的能扯上的数据结构和算法....还是不行就硬想吧...

Median of a two sorted array

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.

Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5.

Given A=[1,2,3] and B=[4,5], the median is 3.

在线性时间复杂度之后, 还有一种更有的logn的解法.
logn用的是二分法, 二分法的本质是, 通过O(1)的时间把一个n的问题变为 n2 的问题.

这道题可以理解为一个更广泛的问题, 在logk的时间里找到第k大…诸如此类的有找到median, 找第1大,第2大…第k大的问题.
这道题里面: k=m+n2
这里面的算法是这样的:

1.首先我们想用二分法找到第k个数,即用logk的时间找到第k大的数,这道题稍微难理解的一点,我个人认为是如下:

正常的二分法是经过一次运算,有一半的数据量被筛除淘汰了,比如一棵二叉树上查找,你往下左走一步,右边的那颗子树就出局了;往右走一步,左面那个子树的数据就都不要了;

而这道题稍微拐一点弯的地方就在于,数据量本身直观的不是k,而且好像扔掉了 k2 ,剩下的数据量跟二分之一有关,但好像也不是一半,然后稀里糊涂的就有点绕懵了…

我们换一个角度理解这道题运用的二分法思想,假设A,B两个排过序的数组都无限长,那么我们想找到两个数组的第k大,备选的是A的前k大的数和B的前k大的数,你也不知道在哪个里,不过肯定就这两个里面跑不了.我们通过一次O(1)的运算,排除了 k2 个肯定不是第k大的,由找第k大的元素变成找第(k- k2 )即 k2 大的,所以搜索范围变成了在A剩下的数里面前 k2 大的,和B剩下的里面 k2 大的. 总共的筛选范围缩小了一半, oh yeah!

我觉得我用这种方法捋清了一点,但是说完感觉没什么卵用…

2.具体程序的逻辑如下:

 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1] , 若n为偶数,则中位数(a[n / 2] + a[n / 2 + 1]) / 2
如果我们可以在两个数列中求出第K小的元素,便可以解决该问题

不妨设数列A元素个数为n,数列B元素个数为m,各自升序排序,求第k小元素
取A[k / 2] B[k / 2] 比较, 

如果 A[k / 2] > B[k / 2] 那么,所求的元素必然不在B的前k / 2个元素中(证明反证法)

k - k / 2小元素,于是得到了数据规模变小的同类问题,递归解决

如果 k / 2 大于某数列个数,所求元素必然不在另一数列的前k / 2个元素中,同上操作就好。  

卖股票

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Given an example [3,2,3,1,2], return 1

Maximum subarray

Given an array of integers, find a contiguous subarray which has the largest sum.
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

对于所有位置n, 求前n个数的和, 然后每个数减前面最小的, 搞定啦~

这两道题就像孙红雷和牛头梗,分开看的时候有点分马牛不相及,放一起仔细一看就是好兄弟啊~

maximum subarray II

Given an array of integers, find two non-overlapping subarrays which have the largest sum.
The number in each subarray should be contiguous.
Return the largest sum.

For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

每个位置砍一刀,左边做一次上道题,右边再做一次,得解~

Subarray sum

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

加个hashtable, 搞定~

Subarray sum closest

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.

Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4].

排个序,具体写题的时候再补充.

写程序的主体事项

分好模块,每个函数模块就干一件事情,不要把所有的东西堆在一起,容易出问题还不易读懂.

Two sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.

You may assume that each input would have exactly one solution

numbers=[2, 7, 11, 15], target=9
return [1, 2]

O(n)时间O(n) 空间的很容易达到.
O(1)空间的时候,时间就不再是O(n)了,而是O(nlogn)了

前后两个指针head和tail, 如果相加大于target, tail左移,否则head右移.

题外话:二分法也是两根指针:进行了一次操作,把一根指到了中间而已

3 Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Notice

Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

The solution set must not contain duplicate triplets.

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)
(-1, -1, 2)

排序一下, for a这个最小的,找b+c = -a的

Partition array

Given an array nums of integers and an int k, partition the array (i.e move the elements in “nums”) such that:

All elements < k are moved to the left
All elements >= k are moved to the right
Return the partitioning index, i.e the first index i nums[i] >= k.

Notice

You should do really partition in array nums instead of just counting the numbers of integers smaller than k.

If all elements in nums are smaller than k, then return nums.length

If nums = [3,2,2,1] and k=2, a valid answer is 1.
大于的右移,小于的左移

Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second.

Notice

It’s NOT necessary to keep the original order of lower-case letters and upper case letters.

For “abAcD”, a reasonable answer is “acbAD”

这道题的注意点是, swap是一个不能保证元素之间相对位置的操作,各种sort是否stable如下

 1.quick sort 不稳定
 2.merge sort 稳定
 3.各种O(n^2)的算法:冒泡,选择, 插入等   稳定
 4.heap sort 不稳定

这道题的总结是,不知道啥是选择和插入排序…回头看看..

Sort colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Notice

You are not suppose to use the library’s sort function for this problem.
You should do it in-place (sort numbers in the original array).

Given [1, 0, 1, 2], sort it in-place to [0, 1, 1, 2].

  1. counting sort 数数有多少个0, 多少个1, 多少个2
  2. 先partition等于0 和不等于0 的 分两部分,然后再把不等于0 的那部分再partition

Sort Colors II

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, … k.

Notice

You are not suppose to use the library’s sort function for this problem.

Given colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].

数组排序…尼玛…直接的我难以置信…

leetcode和oj-Dynamic-Programming-9C:九章算法动态规划专题相关代码 leetcode 和 oj 简介 :balloon: 题目为九章算法DP专题中讲到的题目。 :balloon: 每道题目有好几个程序,这是因为,大部分题目我都是先自己做,然后再对照老师的讲解,修改自己的程序。 :balloon: 另外吐槽一下lint code的OJ,有一些题目,我的程序写的不对,但是提交也可以通过,所以我建议,同一个题目可以在lint code和leet code上提交两次。 第一次课 给出不同面额的硬币以及一个总金额. 写一个方法来计算给出的总金额可以换取的最少的硬币数量. 如果已有硬币的任意组合均无法与总金额面额相等, 那么返回 -1. Example 样例1 输入: [1, 2, 5] 11 输出: 3 解释: 11 = 5 + 5 + 1 DP解法 :balloon: 这个是一开始我自己写的程序,写的还是比较差劲。:face_with_tears_of_joy: class Solution { public: int coinChange(vector&lt;int&gt;&amp; coins, int amount) { sort(coins.begin(),coins.end()); //不得不忍受一个排序的时间复杂度 if(amount == 0) { return 0; } 立即下载

相关推荐

SRC漏洞挖掘实战:从攻击面管理到业务逻辑漏洞的精准狩猎

在网络安全领域,漏洞挖掘是识别和利用软件系统中安全弱点的核心实践。其基本原理在于通过系统化的方法,识别和评估一个应用或系统的攻击面,并从中定位安全防护的薄弱环节。这项技术的价值在于能够主动发现潜在风险,防患于未然,对于保障企业数字资产安全至关重要。典型的应用场景包括对Web应用、移动端API以及各类在线业务系统的安全性评估。在实际的漏洞挖掘,尤其是针对安全应急响应中心(SRC)的实战中,高效的策略往往聚焦于**边缘资产**的发现与利用,以及对**业务逻辑漏洞**的模式化测试。掌握这些经过验证的思路,能帮助安

babing2770的博客 414

TypeScript学习笔记3-数组

一般来说,数组里面的元素都应该是同类型的typescript中有多种数组形式杂乱数组元组多维数组。

STATICHIT静砸的博客 474

ESP32-S3 USB下载模式深度解析:从UART0到TinyUSB的硬件设计与避坑指南

本文深度解析ESP32-S3的三种USB下载模式:UART0、Hardware CDC和基于TinyUSB的USB-OTG CDC。详细探讨各模式的硬件设计要点、电平异常问题、eFuse配置及避坑指南,帮助开发者优化设计,提升量产效率和系统稳定性。

gold的博客 1288

前端数组

Typescrtpt中数组的使用 1、数组的声明 let arr1:array&lt;number&gt; let arr2:number[] 2、数组初始化 let arr1:array&lt;number&gt;=new array&lt;number&gt;(); let arr2:number[]=[1,2,3]; let arr3=[1,2,3,4,5]; 3、数组元素的操作 初始化数组 let arr:array&lt;number&gt;=new array&lt;number&gt;

dlpmmk的博客 414

智能车八邻域图像算法_二

智能车八邻域图像算法 文章目录智能车八邻域图像算法前言一、一些想法二、生长方向的使用1.点的生长方向的表示2. 用点的生长方向来做初步判断1.引入库2.读入数据总结 前言 上一篇里面讲过用八邻域来获得赛道边界:智能车八邻域图像算法 当已经能稳定获得赛道边界时,就可以进行元素识别,获得偏差了。 这里就介绍一下我的一些思路。 早在前两年,优快云上就有一篇关于八邻域如何处理元素的文章第十五届全国大学生智能汽车竞赛-双车组三轮图像处理总结,写得很好,我当时也参考了这篇文章,但是我觉得不是很详细,所以我这里想写得

技术缝合怪的博客 1万+

2024年上半年网络工程师下午真题及答案解析

位于MPLS域边缘、连接其他网络的LSR称为( 3 ),区域内部的LSR称为核心LSR(CoreLSR)IP报文进入MPLS网络时,MPLS入口设备分析IP报文的内容并为其添加合适的标签,所有MPLS网络中的LSR根据标签转发数据,当该IP报文离开MPLS网络时,标签由出口LER弹出。LSP的出口LER称为( 6 )。某高校网络拓扑如下图所示,两校区核心(CORE-1、CORE-2),出口防火墙(NGFW-1、NGFW-2)通过校区间光缆互联,配置OSPF实现全校路由收敛,两校区相距40km。

bsefef的博客 2471

Leetcode全求和问题

目录 1、编号1 Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they

無名黑洞 1万+

Webug4.0 打靶笔记

一、Webug 的安装 (1)基本信息 WeBug 名称定义为”我们的漏洞”靶场环境 ,基础环境是基于 PHP/mysql 制作搭建而成,中级环境与高级环境分别都是由互联网漏洞事件而收集的漏洞存在的操作环境。部分漏洞是基于Windows操作系统的漏洞所以将WeBug的web环境都装在了一个纯净版的Windows 2003的虚拟机中。在 webug3.0 发布后的四百多天 226安全团队终于在大年初二发布了 webug 的 4.0 版本。 (2)虚拟机的安装 最新超详细VMware虚拟机下载与安装_隐

m0_60716947的博客 6127

javascript 数组类型(Number)

javascript 数组类型(Number)

观剑布则声 1143

中台战略-第五章、中台建设方法论

文章目录第五章、中台建设方法论5.1 中台架构整体策略5.2 业务顶层设计5.3 业务中台设计方法论5.3.1 能力支撑是基础5.3.2 中心自治是承载形式5.3.3 3层模型是骨架5.3.4 5步法是指导思想1. 业务抽象2. 高阶设计3. 组件建模4. 开发交付5. 持续运营5.4 数据中台设计方法论5.4.1 总体规划5.4.2 数据集成5.4.3 模型建设5.4.4 通用研发5.4.5 资产管理5.4.6 数据服务5.5 应用向中台迁移的3中途径和方法5.5.1 方法1:新应用替换旧应用5.5.2

沧海之巅的专栏 1458

详解RT-DETR网络结构/数据集获取/环境搭建/训练/推理/验证/导出/部署

本文给大家带来是利用RT-DETR模训练自己的数据集,RT-DETR是一种新提出的目标检测模型,它利用的自注意力机制来处理图像数据。不同于YOLO通过连续的卷积层直接对图像区域进行分析,RT-DETR采用Transformer架构中的自注意力机制,这允许模型更有效地理解图像中不同部分之间的关系。这种方法使得RT-DETR在处理图像中的复杂场景和多对象环境时,能够展现出更高的准确性和效率。此外,RT-DETR在保持高精度的同时,也针对实时处理进行了优化,使其适合需要快速响应的应用场景。

Snu77的博客 6万+

经典算法题(二)——解Integer Array - 整型数组

本章主要总结与整型数组相关的题。 1.Remove Element Question leetcode: Remove Element | LeetCode OJ lintcode: (172) Remove Element Given an array and a value, remove all occurrences of that value in place and ...

m0_37442062的博客 2479

YOLOv11改进 | 模型知识蒸馏篇 | 利用模型蒸馏改进YOLOv11进行无损涨点 | 离线蒸馏(附代码 + 完整文件 + 解析教程)

这篇文章给大家带来的最新改进是模型的蒸馏,利用教师模型指导学生模型从而进行模型的涨点,本文的内容不仅可以用于论文中,在目前的绝大多数的工作中模型蒸馏是一项非常重要的技术,所以大家可以仔细学习一下本文的内容,本文从YOLOv8的项目文件为例,进行详细的修改教程,文章内包括完整的修改教程,针对小白我出了视频修改教程,如果你还不会我提供了修改后的文件大家直接运行即可,所以说不用担心不会适用!模型蒸馏真正的无损涨点,蒸馏你只看这一篇文章就足够了!欢迎大家订阅我的专栏一起学习YOLO!

Snu77的博客 4318

Merge Two Sorted Arrays(java solution)

descriptionMerge two given sorted integer array A and B into a new sorted integer array.Have you met this question in a real interview? Yes Example A=[1,2,3,4]B=[2,4,5,6]return [1,2,2,3,4,4,5,6] 阶梯思路:

ncst 581

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

深入理解JavaScript中的array、string、number...

数组(array) JavaScript 中,数组可以容纳任何类型的值 多维数组 var a = [1, '2', [3]] a.length // 3 a[0] // 1 a[2][0] // 3 注意:使用 delete 可以删除数组中的元素,但不会改变数组的 length 属性。 稀疏数组 数组的索引可以是数字,可以是字符串 var a = [] a[0] = 1; a['demo'] = 2 a.length // 思考一下:这里为什么是1,不是2 a['demo'] // 2

weixin_47352287的博客 872

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

算法学习篇-18、两数之和

一、题目 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。 样例 Example1: 给出 numbers = [2, 7, 11, 15], target = 9, 返回 [0, 1]. Example2: 给出 numbers = [15, 2, 7, 11], target = 9, 返回 [1, 2]. 二、思路 1、两个数相加要等于target,则一个数

Code P的博客 290

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

7-3 Counting Lower-case Letters (25 分)

7-3 Counting Lower-case Letters (25 分) Given a string consisting of lower-case letters chosen from a-z, you can obtain numbers of occurrence of each letter. Now you are given a string and several inte...

endless/若风 - 计算机软件 3695

js中 String、Array、Number、Math 属性方法汇总

https://blog.youkuaiyun.com/qq_31280709/article/details/70339403

爱码喵的博客 481

TypeScript 数组Array操作

数组是一个很简单的数据结构,但是每次使用TypeScript的数组的时候又总是忘记怎么用了,干脆直接弄成干货,忘了过来看看。 一、数组的声明 let array1:Array&amp;amp;lt;number&amp;amp;gt;; let array2:number[]; 二、数组初始化 let array1:Array&amp;amp;lt;number&amp;amp;gt; = new Array&amp;amp;lt;number&amp;amp;gt;(); let ...

阳仔博客 15万+

Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Coul...

leetcode 解题思路 279

九章算法面试题9 前k大的和

【初阶】有两个数组A和B,每个数组有k个数,从两个数组中各取一个数加起来可以组成k*k个和,求这些和中的前k大。 【进阶】有N个数组,每个数组有k个数,从N个数组中各取一个数加起来可以组成k^N个和,求这些和中的前k大。

九章算法的博客 1604

LeenCode——归并问题的处理( Merge Two Sorted Lists&&Given two sorted integer arrays nums1 and nums2, merge n)

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.这道题的巧妙之处在于,提示中num1数组的容量大于m+n,即两个数组长度之和。 数组nums1和nums2都是从大到小排列,所以可以从数组的尾部开始比较,取出元素最大的插入nums1的末尾。 JS代码: var

ty13438189519的博客 735

Subarray Sum

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.Example Given [-3, 1, 2, -3, 4], return

Coder|2015 524

es6 Array Number 对象

web components 规范已经完成并通过,但并被所有浏览器原生实现。目前。 vue组件提供了纯自定义元素所不具备的一些重要功能,最突出的是跨组件数据流、自定义事件通信以及构建工具集成。 ...

qq_43183527的博客 308

数组面试题之序列

1. QUESTION:  Given an int array which might contain duplicates, find the largest subset of it which form a sequence. Eg. {1,6,10,4,7,9,5} then ans is 4,5,6,7  answer 1. Create an array 'TempArray

花果山 755
上一篇: 九章算法6:Linked List
下一篇: 九章算法8:Data Structure
Stomachache007
博客等级 码龄11年 13粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值