原文:
给定一个由 n 个对组成的 arr[] ,每个对由一个字符串和对应于的整数值组成。任务是找到最大绝对和 并打印子阵元素对应的字符串。
示例:
输入: arr[] = {(“极客”,4)、(“为”、-3)、(“极客”,2)、(“教程”,3)、(“程序”,-4)} 输出:极客为极客教程 说明:子阵的最大绝对和为{arr[0],..arr[3]},求和为 6。因此,这些值之间对应的字符串是“极客”、“for”、“极客”和“教程”。
输入: arr[]= {(“练习”、-7)、(“makes”,2)、(“men perfect”,5)} 输出:练习 说明:子阵的最大绝对和为{arr[0]},有和 7。因此,对应的字符串是“练习”。
天真法:最简单的方法是生成所有可能的子阵找到最大和子阵。然后,打印对应于该子阵列的字符串。 时间复杂度:o(n2) 辅助空间: o(1)
高效方法:最优思路是使用进行一些修改,使其能够处理负值,并在绝对最小值和绝对最大值之间选择最大值。
按照以下步骤解决问题:
- 初始化变量, res = 0 ,存储最终答案, start = 0 , end = 0 存储所需子阵列的起始和结束索引。
- 再初始化两个变量,比如 posprefix 和 negprefix ,存储之前的正前缀值和负前缀值。
- 遍历阵列 arr[] 并执行以下操作
- 如果当前元素为负,并且如果 arr[i] negprefix > res 的值,则更新 res 的值,开始和结束索引。
- 如果当前元素为正,并且如果 arr[i] posprefix > res 的值,则更新 res 的值,开始和结束索引。
- 检查将当前元素添加到 negprefix 是否使其大于或等于 0 ,然后更新start=i 1并设置 negprefix = 0 否则,将当前值添加到 negprefix 。
- 检查将当前元素添加到 posprefix 是否使其小于或等于 0 ,然后更新 start = i 1 并设置 posprefix = 0 否则,将当前值添加到 posprefix 。
- 最后,遍历【开始,结束】范围内的数组,打印相应的字符串。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to print strings corresponding
// to the elements present in the subarray
// with maximum absolute sum
void maximumabssum(pair* arr,
int n)
{
int start = 0, end = 0, res = 0,
negindex = 0, posindex = 0,
negprefix = 0, posprefix = 0;
// traverse the array
for (int i = 0; i < n; i ) {
if (arr[i].second < 0) {
// if adding current element
// to negative
// prefix makes it > res
// then update the values
if (res < abs(arr[i].second
negprefix)) {
res = abs(arr[i].second
negprefix);
start = negindex;
end = i;
}
}
else {
// if adding current element to
// positive prefix exceeds res
if (res < abs(arr[i].second
posprefix)) {
res = abs(arr[i].second
posprefix);
start = posindex;
end = i;
}
}
// since negprefix > 0, there is
// no benefit in adding it to a
// negative value
if (negprefix arr[i].second > 0) {
negprefix = 0;
negindex = i 1;
}
// since negative negative
// generates a larger negative value
else {
negprefix = arr[i].second;
}
// since positive positive
// generates a larger positive number
if (posprefix arr[i].second >= 0) {
posprefix = arr[i].second;
}
// since pos_prefix < 0, there is
// no benefit in adding it to
// a positive value
else {
posprefix = 0;
posindex = i 1;
}
}
// print the corresponding strings
for (int i = start; i <= end; i ) {
cout << arr[i].first << " ";
}
}
// driver code
int main()
{
// given array
pair arr[] = { { "geeks", 4 },
{ "for", -3 },
{ "geeks", 2 },
{ "tutorial", 3 },
{ "program", -4 } };
// size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// function call to print
// string corresponding to
// maximum absolute subarray sum
maximumabssum(arr, n);
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
class gfg
{
static class pair
{
e first;
r second;
public pair(e first, r second)
{
this.first = first;
this.second = second;
}
}
// function to print strings corresponding
// to the elements present in the subarray
// with maximum absolute sum
static void maximumabssum(pair []arr,
int n)
{
int start = 0, end = 0, res = 0,
negindex = 0, posindex = 0,
negprefix = 0, posprefix = 0;
// traverse the array
for (int i = 0; i < n; i )
{
if (arr[i].second < 0)
{
// if adding current element
// to negative
// prefix makes it > res
// then update the values
if (res < math.abs(arr[i].second
negprefix)) {
res = math.abs(arr[i].second
negprefix);
start = negindex;
end = i;
}
}
else
{
// if adding current element to
// positive prefix exceeds res
if (res < math.abs(arr[i].second
posprefix)) {
res = math.abs(arr[i].second
posprefix);
start = posindex;
end = i;
}
}
// since negprefix > 0, there is
// no benefit in adding it to a
// negative value
if (negprefix arr[i].second > 0) {
negprefix = 0;
negindex = i 1;
}
// since negative negative
// generates a larger negative value
else {
negprefix = arr[i].second;
}
// since positive positive
// generates a larger positive number
if (posprefix arr[i].second >= 0) {
posprefix = arr[i].second;
}
// since pos_prefix < 0, there is
// no benefit in adding it to
// a positive value
else {
posprefix = 0;
posindex = i 1;
}
}
// print the corresponding strings
for (int i = start; i <= end; i ) {
system.out.print(arr[i].first " ");
}
}
// driver code
@suppresswarnings("unchecked")
public static void main(string[] args)
{
// given array
@suppresswarnings("rawtypes")
pair arr[] = { new pair( "geeks", 4 ),
new pair( "for", -3 ),
new pair( "geeks", 2 ),
new pair( "tutorial", 3 ),
new pair( "program", -4 ) };
// size of the array
int n = arr.length;
// function call to print
// string corresponding to
// maximum absolute subarray sum
maximumabssum(arr, n);
}
}
// this code is contributed by shikhasingrajput
python 3
# python3 program for the above approach
# function to print strings corresponding
# to the elements present in the subarray
# with maximum absolute sum
def maximumabssum(arr, n):
start, end, res = 0, 0, 0
negindex, posindex = 0, 0
negprefix, posprefix = 0, 0
# traverse the array
for i in range(n):
if (arr[i][1] < 0):
# if adding current element
# to negative
# prefix makes it > res
# then update the values
if (res < abs(arr[i][1] negprefix)):
res = abs(arr[i][1] negprefix)
start = negindex
end = i
else:
# if adding current element to
# positive prefix exceeds res
if (res < abs(arr[i][1] posprefix)):
res = abs(arr[i][1] posprefix)
start = posindex
end = i
# since negprefix > 0, there is
# no benefit in adding it to a
# negative value
if (negprefix arr[i][1] > 0):
negprefix = 0
negindex = i 1
# since negative negative
# generates a larger negative value
else:
negprefix = arr[i][1]
# since positive positive
# generates a larger positive number
if (posprefix arr[i][1] >= 0):
posprefix = arr[i][1]
# since pos_prefix < 0, there is
# no benefit in adding it to
# a positive value
else:
posprefix = 0
posindex = i 1
# print the corresponding strings
for i in range(start, end 1):
print(arr[i][0], end = " ")
# driver code
if __name__ == '__main__':
# given array
arr = [ [ "geeks", 4 ],
[ "for", -3 ],
[ "geeks", 2 ],
[ "tutorial", 3 ],
[ "program", -4 ] ]
# size of the array
n = len(arr)
# function call to print
# string corresponding to
# maximum absolute subarray sum
maximumabssum(arr, n)
# this code is contributed by mohit kumar 29.
c
// c# program for the above approach
using system;
using system.collections.generic;
public class gfg
{
public class pair
{
public string first;
public int second;
public pair(string first, int second)
{
this.first = first;
this.second = second;
}
}
// function to print strings corresponding
// to the elements present in the subarray
// with maximum absolute sum
static void maximumabssum(pair []arr,
int n)
{
int start = 0, end = 0, res = 0,
negindex = 0, posindex = 0,
negprefix = 0, posprefix = 0;
// traverse the array
for (int i = 0; i < n; i )
{
if (arr[i].second < 0)
{
// if adding current element
// to negative
// prefix makes it > res
// then update the values
if (res < math.abs(arr[i].second
negprefix)) {
res = math.abs(arr[i].second
negprefix);
start = negindex;
end = i;
}
}
else
{
// if adding current element to
// positive prefix exceeds res
if (res < math.abs(arr[i].second
posprefix)) {
res = math.abs(arr[i].second
posprefix);
start = posindex;
end = i;
}
}
// since negprefix > 0, there is
// no benefit in adding it to a
// negative value
if (negprefix arr[i].second > 0) {
negprefix = 0;
negindex = i 1;
}
// since negative negative
// generates a larger negative value
else {
negprefix = arr[i].second;
}
// since positive positive
// generates a larger positive number
if (posprefix arr[i].second >= 0) {
posprefix = arr[i].second;
}
// since pos_prefix < 0, there is
// no benefit in adding it to
// a positive value
else {
posprefix = 0;
posindex = i 1;
}
}
// print the corresponding strings
for (int i = start; i <= end; i ) {
console.write(arr[i].first " ");
}
}
// driver code
public static void main(string[] args)
{
// given array
pair []arr = { new pair( "geeks", 4 ),
new pair( "for", -3 ),
new pair( "geeks", 2 ),
new pair( "tutorial", 3 ),
new pair( "program", -4 ) };
// size of the array
int n = arr.length;
// function call to print
// string corresponding to
// maximum absolute subarray sum
maximumabssum(arr, n);
}
}
// this code is contributed by rajput-ji
java 描述语言
output:
geeks for geeks tutorial
时间复杂度:o(n) t5辅助空间:** o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处