【力扣 - 和为K的子数组】

题目描述

给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的子数组的个数 。

子数组是数组中元素的连续非空序列。

示例 1:

输入:nums = [1,1,1], k = 2
输出:2

示例 2:

输入:nums = [1,2,3], k = 3
输出:2

提示:

1 <= nums.length <= 2 * 10^4
-1000 <= nums[i] <= 1000
-10^7 <= k <= 10^7

方法一:枚举

思路和算法

考虑以 i 结尾和为 k 的连续子数组个数,我们需要统计符合条件的下标 j 的个数,其中 0≤j≤i[j..i] 这个子数组的和恰好为 k

我们可以枚举 [0..i]里所有的下标 j来判断是否符合条件,可能有读者会认为假定我们确定了子数组的开头和结尾,还需要 O(n) 的时间复杂度遍历子数组来求和,那样复杂度就将达到 O(n3) 从而无法通过所有测试用例。但是如果我们知道 [j,i] 子数组的和,就能 O(1) 推出 [j−1,i] 的和,因此这部分的遍历求和是不需要的,我们在枚举下标 j 的时候已经能 O(1) 求出 [j,i] 的子数组之和。

代码

/**
 * Function to count the number of subarrays that sum up to the target value k
 * @param nums: array of integers
 * @param numsSize: size of the input array
 * @param k: target sum value
 * @return the number of subarrays that sum up to k
 */
int subarraySum(int* nums, int numsSize, int k) {
    int count = 0; // Initialize the count of subarrays that sum up to k

    // Iterate through each index as the starting point of the subarray
    for(int leftindex = 0; leftindex < numsSize; ++leftindex)
    {
        int sum = 0; // Initialize the sum of the subarray starting at leftindex

        // Calculate the sum of subarrays starting from leftindex and ending at different points
        for(int rightindex = leftindex; rightindex >= 0; --rightindex)
        {
            sum = sum + nums[rightindex]; // Add the element at rightindex to the sum

            // Check if the current sum equals the target value k
            if(sum == k)
            {
                count++; // Increment the count as a subarray with sum k is found
            }
        }
    }
    return count; // Return the total count of subarrays with sum equal to k
}

方法二:前缀和 + 哈希表优化

思路和算法

我们可以基于方法一利用数据结构进行进一步的优化,我们知道方法一的瓶颈在于对每个 i,我们需要枚举所有的 j 来判断是否符合条件,这一步是否可以优化呢?答案是可以的。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

// Structure to represent a node in the hash table
struct Node {
    int key;
    int value;
    struct Node* next;
};

// Function to create a new node for the hash table
struct Node* createNode(int key, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = NULL;
    return newNode;
}

// Function to count the number of subarrays that sum up to the target value k
int subarraySum(int* nums, int numsSize, int k) {
    // Initialize an array of pointers to nodes for the hash table
    struct Node* map[numsSize + 1];
    for (int i = 0; i <= numsSize; i++) {
        map[i] = NULL;
    }
    
    // Initialize the first node in the hash table
    map[0] = createNode(0, 1);
    int count = 0, pre = 0;
    
    // Iterate through the input array to calculate subarray sums
    for (int i = 0; i < numsSize; i++) {
        pre += nums[i];
        
        // Calculate the key for the current subarray sum
        int key = pre - k;
        int index = (key % (numsSize + 1) + numsSize + 1) % (numsSize + 1); // Ensure non-negative index
        struct Node* current = map[index];
        
        // Traverse the linked list at the computed index
        while (current != NULL) {
            if (current->key == key) {
                count += current->value;
            }
            current = current->next;
        }
        
        // Calculate the new index for the current sum
        int newIndex = (pre % (numsSize + 1) + numsSize + 1) % (numsSize + 1); // Ensure non-negative index
        if (map[newIndex] == NULL) {
            map[newIndex] = createNode(pre, 1);
        } else {
            struct Node* temp = map[newIndex];
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = createNode(pre, 1);
        }
    }
    
    return count;
}

方法三:哈希表(不超时)

前面两个方法都超时了。
利用malloc动态开辟一个数组空间,作为哈希表,数组下标代表哈希key值,数组下标对应的值代表key值指向的元素值,再对数组求前缀和,将前缀和将key值存入哈希数组中,前缀和 p[i] - p[j] = k 就表示数组i - j 满足要求,即p[i] - k = p[j],每次求出当前前缀和,就在哈希数组中判断是否存在p[j],累加p[j]存在个数,并返回

// Function to count the number of subarrays that sum up to the target value k
int subarraySum(int *nums, int numsSize, int k) {
    int count = 0;
    
    // Create a large array to act as a simple hash table, initialized to all zeros
    int *maps = (int *)calloc(10000001 * 2, sizeof(int));
    
    // Set the pointer to the middle position to accommodate negative prefix sums
    int *map = maps + 10000001 * 1;
    
    // Initialize the prefix sum with an additional sum at index 0
    int sum = 0;
    map[sum]++;
    
    // Calculate prefix sums and count subarrays with target sum k
    for (int i = 0; i < numsSize; i++) {
        sum += nums[i];
        
        // Check if a previous prefix sum exists that can form a subarray with sum k
        if (map[sum - k] > 0) {
            count += map[sum - k];
        }
        
        // Update the count of prefix sums encountered
        map[sum]++;
    }
    
    // Free the memory allocated for the hash table array
    free(maps);
    
    return count;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ElementUI两个小坑

1.form表单绑定的是一个对象&#xff0c;表单里的一个输入项是对象的一个属性之一&#xff0c;修改输入项&#xff0c;表单没刷新的问题&#xff0c; <el-form :model"formData" :rules"rules" ref"editForm" class"demo-ruleForm"…

【机器学习】机器学习创建算法第1篇:机器学习算法课程定位、目标【附代码文档】

机器学习&#xff08;算法篇&#xff09;完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;机器学习算法课程定位、目标&#xff0c;K-近邻算法&#xff0c;1.1 K-近邻算法简介&#xff0c;1.2 k近邻算法api初步使用定位,目标,学习目标,1 什么是K-近邻算法,…

Java旋转矩阵

题目&#xff1a; 给你一幅由 N N 矩阵表示的图像&#xff0c;其中每个像素的大小为 4 字节。请你设计一种算法&#xff0c;将图像旋转 90 度。 不占用额外内存空间能否做到&#xff1f; 示例 1: 给定 matrix [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵&…

山姆・阿尔特曼重返OpenAI董事会;Car-GPT:LLMs能否最终实现自动驾驶?

&#x1f989; AI新闻 &#x1f680; 山姆・阿尔特曼重返OpenAI董事会 摘要&#xff1a;经历长达数月的审查后&#xff0c;山姆・阿尔特曼已重返OpenAI董事会&#xff0c;并作为返回条件之一&#xff0c;OpenAI还新增了三名外部女性董事会成员。这标志着公司正努力摆脱去年11…

开源模型应用落地-业务优化篇(八)

一、前言 在之前的学习中&#xff0c;我相信您已经学会了一些优化技巧&#xff0c;比如分布式锁、线程池优化、请求排队、服务实例扩容和消息解耦等等。现在&#xff0c;我要给您介绍最后一篇业务优化的内容了。这个优化方法是通过定时统计问题的请求频率&#xff0c;然后将一些…

交叉编译x264 zlib ffmpeg以及OpenCV等 以及解决交叉编译OpenCV时ffmpeg始终为NO的问题

文章目录 环境编译流程nasm编译x264编译zlib编译libJPEG编译libPNG编译libtiff编译 FFmpeg编译OpenCV编译问题1解决方案 问题2解决方案 总结 环境 系统&#xff1a;Ubutu 18.04交叉编译链&#xff1a;gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu 我的路径/opt/toolch…

【Flutter】报错Target of URI doesn‘t exist ‘package:flutter/material.dart‘

运行别人项目 包无法导入报错&#xff1a;Target of URI doesn’t exist ‘package:flutter/material.dart’ 解决方法 flutter packages get成功 不会报错

php CI框架异常报错通过钉钉自定义机器人发送

php CI框架异常报错通过钉钉自定义机器人发送 文章目录 php CI框架异常报错通过钉钉自定义机器人发送前言一、封装一个异常监测二、封装好钉钉信息发送总结 前言 我们在项目开发中&#xff0c;经常会遇到自己测试接口没问题&#xff0c;上线之后就会测出各种问题&#xff0c;主…

K8s — PVC|PV Terminating State

在本文中&#xff0c;我们将讨论PV和PVC一直Terminating的状态。 何时会Terminting? 在以下情况下&#xff0c;资源将处于Terminating状态。 在删除Bounded 状态的PVC之前&#xff0c;删除了对应的PV&#xff0c;PV在删除后是Terminting状态。删除PVC时&#xff0c;仍有引用…

python向多个用户发送文字、图片内容邮件和excel附件

话不多说&#xff0c;直接上代码&#xff0c;需要把发件里面的smtp_info替换为自己的信息&#xff0c;其中password是指邮箱在开通POP/SMTP功能后获取的授权码&#xff01; import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import M…

案例分析篇05:数据库设计相关28个考点(9~16)(2024年软考高级系统架构设计师冲刺知识点总结系列文章)

专栏系列文章推荐: 2024高级系统架构设计师备考资料(高频考点&真题&经验)https://blog.csdn.net/seeker1994/category_12601310.html 【历年案例分析真题考点汇总】与【专栏文章案例分析高频考点目录】(2024年软考高级系统架构设计师冲刺知识点总结-案例分析篇-…

web3D三维引擎(Direct3D、OpenGL、UE、U3D、threejs)基础扫盲

Hi&#xff0c;我是贝格前端工场的老司机&#xff0c;本文介绍文web3D的几个引擎&#xff0c;做个基础扫盲&#xff0c;如果还不能解决问题&#xff0c;可以私信我&#xff0c;搞私人订制呦。 三维引擎是指用于创建和渲染三维图形的软件框架。它们通常提供了图形处理、物理模拟…

案例分析篇02:软件架构设计考点之特定领域软件架构、架构评估、架构视图(2024年软考高级系统架构设计师冲刺知识点总结)

专栏系列文章推荐: 2024高级系统架构设计师备考资料(高频考点&真题&经验)https://blog.csdn.net/seeker1994/category_12601310.html 【历年案例分析真题考点汇总】与【专栏文章案例分析高频考点目录】(2024年软考高级系统架构设计师冲刺知识点总结-案例分析篇-…

2024RKDC,新一代AIOT 处理器RK3576发布 !

触觉智能已成功推出RK3576相关开发板核心板&#xff0c;RK3576采用瑞芯微八核芯片&#xff0c;专为 AI0I设计&#xff0c;可用于平板电脑、AI0T应用程序、电子墨水显示器、Arm PC和汽车电子中。集成独立的6TOPS NPU&#xff0c;支持4K视频编解码&#xff0c;性能定位于RK3588和…

smart-doc 社区 Committer 晋升公告

我们非常荣幸地宣布&#xff0c;经过 PMC 委员会的提名和讨论&#xff0c;社区成员李星志&#xff08;GitHub ID: netdied&#xff09;、陈琪&#xff08;GitHub ID: chenqi146&#xff09;和李兵&#xff08;GitHub ID: abing22333&#xff09;正式晋升为同程旅行 smart-doc 开…

redis概述和安装

1 、redis概述和安装 1.1、安装redis 1. 下载redis2. 地址 : https://download.redis.io/releases/ 3. 选择需要的版本1.2 将 redis 安装包拷贝到 /opt/ 目录 1.3. 解压 tar -zvxf redis-6.2.1.tar.gz1.4. 安装gcc yum install gcc1.5. 进入目录 cd redis-6.2.11.6 编译 …

微信小程序云开发教程——墨刀原型工具入门(素材面板)

引言 作为一个小白&#xff0c;小北要怎么在短时间内快速学会微信小程序原型设计&#xff1f; “时间紧&#xff0c;任务重”&#xff0c;这意味着学习时必须把握微信小程序原型设计中的重点、难点&#xff0c;而非面面俱到。 要在短时间内理解、掌握一个工具的使用&#xf…

“2024成都国际电子信息产业展览会”新西部、新重构、新机遇

随着西部大开发和科技兴国的战略深入实施&#xff0c;四川的经济发展已经取得了令人瞩目的成就。作为西部的核心&#xff0c;四川在能源化工、装备制造、航天科技等产业方面均处于国内领先地位&#xff0c;同时也是全国重要的基础电子装备基地。这一发展背景为2024年成都国际电…

ETL的数据挖掘方式

ETL的基本概念 数据抽取&#xff08;Extraction&#xff09;&#xff1a;从不同源头系统中获取所需数据的步骤。比如从mysql中拿取数据就是一种简单的抽取动作&#xff0c;从API接口拿取数据也是。 数据转换&#xff08;Transformation&#xff09;&#xff1a;清洗、整合和转…

常用云产品连接

阿里云常用云产品 云服务器 阿里云&#xff1a;云服务器ECS_云主机_服务器托管_计算-阿里云 对象存储 阿里云&#xff1a;对象存储 OSS_云存储服务_企业数据管理_存储-阿里云 短信服务 阿里云&#xff1a;短信服务_企业短信营销推广_验证码通知-阿里云 CDN服务 阿里云&…