perl 哈希数组的哈希_第一个元素使用哈希在数组中出现K次

perl 哈希数组的哈希

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

Problem statement:

问题陈述:

Find the first element occurring K times in the array.

查找数组中出现K次的第一个元素。

Example:

例:

Input array= [2, 3, 2, 3, 2, 3]
K=3
The first element occurring k times is 2. 
Though both 2 and 3 have occurred 3 times since 2 came first, 
so the answer would be 2Input array= [1 2 3 1 2]
K=3
None of the elements has occurrences 3, 
so the answer would be -1. 

Solution:

解:

We can solve this problem using a hash map (hash table). One thing is clear that to find out the first element occurring k times we need to keep track of frequencies for each unique key.

我们可以使用哈希映射(哈希表)解决此问题。 很明显,要找出出现k次的第一个元素,我们需要跟踪每个唯一键的频率。

So how can we design the problem with the hash table and what will be the hash function?

那么我们如何设计哈希表的问题以及哈希函数是什么呢?

Okay, so here each element is our key and the hash table is has the size of the range of the array. So, if the range of the array is [0,99] then the hash table size would be 100.
What will be our hash function and how would we map the keys to the corresponding location in the hash table?

好的,所以这里的每个元素都是我们的键,哈希表具有数组范围的大小。 因此,如果数组的范围为[0,99],则哈希表大小将为100。
我们的哈希函数将是什么?如何将键映射到哈希表中的对应位置?

The hash function h(x)=x here but instead of storing the key itself using linear probing, we will keep the frequency only as we only require that.

这里的哈希函数h(x)= x ,而不是仅使用线性探测来存储密钥本身,我们将仅保留频率,因为我们只需要频率。

So, for example, if we have three 2s as our key, the hash table will store the count (frequency) at location 2.

因此,例如,如果我们有三个2作为密钥,则哈希表将在位置2存储计数(频率)。

So, after the hash table is created,

因此,在创建哈希表之后,

We can check the frequency for each element starting from left to right, one by one. The one satisfying the constraint, i.e., having frequency k will be our answer. In this way, we will get the first element occurring k times. And if none of the elements satisfies then the answer would be -1.

我们可以从左到右逐一检查每个元素的频率。 满足约束,即具有频率k的那个将是我们的答案。 这样,我们将获得第一个出现k次的元素。 如果所有元素都不满足,则答案将为-1。

So the algorithm will be,

因此算法将是

Step 1:

第1步:

Create the hash table like below:
Initially hash table is empty
For each key in input array:
Hash[key]++

Step 2:

第2步:

For each element e:
if(hash[e]==k)
return e;
End for

Dry run with the example:

空运行示例:

Input array is = [2, 3, 2, 3, 2, 3]
K=3
After creating the hash table as step1 we will have
Index	Value
2	3
3	3
Now step2
While scanning for first element having frequency K
2 has frequency 3 and thus we return 3 immediately.
For example 2:
Input array= [1 2 3 1 2]
K=3
After creating the hash table as step1 we will have
Index	Value
1	2
2	2
3	1
So after scanning we find that none of the elements 
has frequency 3, thus answer will be -1.

C++ implementation:

C ++实现:

// C++ program to find first element 
// occurring k times in the array
#include <bits/stdc++.h>
using namespace std;
int first_element(vector<int> arr, int n, int k)
{
//create a hash table where for each key 
//the hash function is h(x)=x
//we will use stl map as hash table and 
//will keep frequencies stored
//so say two keys are mapping to the same location, 
//then the location will have value 2
//instead of the keys itself
map<int, int> hash;
//for each number
for (auto i : arr) { 
hash[i]++;
}
//now for any number if we find 
//it has frequency k, we simply return
for (auto i : arr) {
if (hash[i] == k)
return i;
}
//otherwise return -1
return -1;
}
int main()
{
int n, k;
cout << "Enter number of elements\n";
cin >> n;
cout << "Enter k\n";
cin >> k;
vector<int> arr(n, 0);
cout << "Input the array elements\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Minimum number of operations required to make all elements same is: ";
cout << first_element(arr, n, k) << endl;
return 0;
}

Output:

输出:

RUN 1:
Enter number of elements
6
Enter k
3
Input the array elements
2 3 2 3 2 3
Minimum number of operations required to make all elements same is: 2
RUN 2:
Enter number of elements
5
Enter k
3
Input the array elements
1 2 3 1 2
Minimum number of operations required to make all elements same is: -1

翻译自: https://www.includehelp.com/data-structure-tutorial/first-element-occurring-k-times-in-the-array-using-hashing.aspx

perl 哈希数组的哈希

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542756.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图片md5修改工具_如何修改视频和图片的MD5,用电脑自带的命令

首先说下&#xff0c;md5到底是啥&#xff0c;它是一段固定长度的数据。无论原始数据是多长或多短&#xff0c;其MD5值都是128bit。另外md5是确定性&#xff0c;一个原始数据的MD5值是唯一的&#xff0c;同一个原始数据不可能会计算出多个不同的MD5值&#xff1b;类似人类的身份…

iOS - UISearchController

前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearchController")interface UISearchDisplayController : NSObjectavailable(iOS, introduced3.0, deprecated8.0, message"UISearchDisplayController has bee…

浮点数转换为整数四舍五入_定义宏以将浮点值四舍五入为C中最接近的整数

浮点数转换为整数四舍五入Given a float value and we have to round the value to the nearest integer with the help of Macro in C language. 给定一个浮点值&#xff0c;我们必须借助C语言中的Macro将其舍入到最接近的整数。 Macro Definition: 宏定义&#xff1a; #def…

c语言遍历文件内容_C语言学习第28篇---动态内存分配剖析

为什么C语言要动态分配内存的意义&#xff1f;1.C语言中的一切操作都是基于内存的2.变量和数组都是内存的别名---内存分配由编译器在编译期间决定的---定义数组的时候必须指定数组长度---数组长度是在编译期就必须确定的需求&#xff1a;程序运行的过程中&#xff0c;可能需要使…

重启mysql的命令 linux_linux重启mysql命令

如何启动/停止/重启MySQL一、 启动方式1、使用 service 启动&#xff1a;service mysqld start2、使用 mysqld 脚本启动&#xff1a;/etc/inint.d/mysqld start3、使用 safe_mysqld 启动&#xff1a;safe_mysqld&二、停止1、使用 service 启动&#xff1a;service mysqld s…

tomcat 多项目多HOST配置

一、场景&#xff1a;使用一个tomcat部署多个项目&#xff0c;并且分别使用不同域名进行访问。二、详细配置tomcat/conf/server.xml 中写<Engine name"Catalina" defaultHost"localhost">***********************************<Host name"biz…

javascript原型_使用JavaScript的示例报告卡Web应用程序原型

javascript原型Hi! At times, beginners always find it hard getting the application of the theory they learn In programming or a particular language. 嗨&#xff01; 有时&#xff0c;初学者总是很​​难在编程或特定语言中应用他们学到的理论。 In this article, we…

vb.net cad 块表最后的实体_21个绘图命令+7个技巧,3分钟让你成为CAD高手

绘制图纸需要用到CAD&#xff0c;CAD制图在生活中也是广泛运用&#xff0c;那么学习CAD到底难不难呢&#xff1f;在这里要告诉CAD新手们&#xff0c;世上无难事&#xff0c;可以用3分钟让你成为CAD高手。21个绘图命令A&#xff1a;绘圆弧B&#xff1a;定义块C&#xff1a;画圆D…

本地tomcat启动war包_「shell脚本」懒人运维之自动升级tomcat应用(war包)

准备&#xff1a;提前修改war包里的相关配置&#xff0c;并上传到服务器&#xff1b;根据要自动升级的tomcat应用修改或添加脚本相关内容&#xff1b;tomcat启动脚本如是自己写的&#xff0c;要统一格式命名&#xff0c;如&#xff1a;xxx、xxxTomcat 等&#xff1b;拿到生产使…

python将txt转为字符串_python做第一只小爬虫

“受尽苦难而不厌&#xff0c;此乃修罗之路”本文技术含量过低&#xff0c;请谨慎观看之前用R语言的Rcurl包做过爬虫&#xff0c;给自己的第一感觉是比较费劲&#xff0c;看着看着发际线就愈加亮眼&#xff0c;最后果断丢之。不过好的是和python爬取原理基本一致&#xff0c;且…

c#查找列表指定元素的索引_在集合的指定索引处插入元素 在C#中

c#查找列表指定元素的索引Given a Collection<T> of Integer and we have to insert an element at given index. 给定Integer的Collection <T>&#xff0c;我们必须在给定的索引处插入一个元素。 To insert an element in Collection<T>, we use Insert() …

跨域技术(JSONP与CROS)

JSONP 我们发现&#xff0c;Web页面上调用js文件时不受是否跨域的影响&#xff0c;凡是拥有"src"这个属性的标签都拥有跨域的能力&#xff0c;比如<script>、<img>、<iframe>。那就是说如果要跨域访问数据&#xff0c;就服务端只能把数据放在js格式…

python3 array为什么不能放不同类型的数据_小白入门Python数据科学全教程lt;一gt;...

前言本文讲解了从零开始学习Python数据科学的全过程&#xff0c;涵盖各种工具和方法你将会学习到如何使用python做基本的数据分析你还可以了解机器学习算法的原理和使用说明先说一段题外话。我是一名数据科学家&#xff0c;在用SAS做分析超过5年后&#xff0c;我决定走出舒适区…

c winform 上传文件到mysql_C# winform DevExpress上传图片到数据库【转】

实现功能如下图&#xff1a;注明&#xff1a;此文使用的是DevExpress控件&#xff0c;winform 原生控件也是一样使用方法。1.点击选择图片按钮&#xff0c;功能为通过对话框选择要上传的文件&#xff0c;并将该文件在下面的PictureEdit中显示出来。具体代码如下&#xff1a;pri…

V 8 nfs+drbd+heartbeat

V 8 nfsdrbdheartbeatnfsdrbdheartbeat&#xff0c;nfs或分布式存储mfs只要有单点都可用此方案解决在企业实际生产场景中&#xff0c;nfs是中小企业最常用的存储架构解决方案之一&#xff0c;该架构方案部署简单、维护方便&#xff0c;只需通过配inotifyrsync简单而高效的数据同…

nodemailer使用_如何使用Nodemailer使用HTML作为内容发送电子邮件 Node.js

nodemailer使用Prerequisite: 先决条件&#xff1a; How to send emails using Nodemailer | Node.js 如何使用Nodemailer发送电子邮件。 Node.js How to send emails with attachments using Nodemailer | Node.js 如何使用Nodemailer发送带有附件的电子邮件。 Node.js This …

angularjs 元素重复指定次数_[LeetCode] 442. 数组中重复的数据

[LeetCode] 442. 数组中重复的数据题目链接&#xff1a; https://leetcode-cn.com/problems/find-all-duplicates-in-an-array难度&#xff1a;中等通过率&#xff1a;61.5%题目描述:给定一个整数数组 a&#xff0c;其中1 ≤ a[i] ≤ n &#xff08; n 为数组长度&#xff09;,…

docker 安装mysql 实战文档_docker 安装mysql

PassJava (佳必过) 项目全套学习教程连载中&#xff0c;关注公众号第一时间获取。docker 安装mysql1.下载镜像sudo docker pull mysql:5.7ubuntuVM-0-13-ubuntu:~$ sudo docker pull mysql:5.75.7: Pulling from library/mysqlc499e6d256d6: Pull complete22c4cdf4ea75: Pull c…

python 补前导零_Python正则表达式| 程序从IP地址中删除前导零

python 补前导零Given an IP address as input, write a Python program to remove leading zeros from it. 给定一个IP地址作为输入&#xff0c;编写一个Python程序以从中删除前导零。 Examples: 例子&#xff1a; Input: 216.08.094.196Output: 216.8.94.196Input: 216.08…

眼球追踪

眼球追踪类似于头部追踪&#xff0c;但是图像的呈现取决于使用者眼睛所看的方向。例如&#xff0c;人们可以用“眼神”完成一种镭射枪的瞄准。眼球追踪技术很受VR专家们密切关注。Oculus创始人帕尔默拉奇就曾称其为“VR的心脏”。对于人眼位置的检测&#xff0c;能够为当前所处…