Given a int array, please find the sub array wich has maximum sum, return the index, length and sum

1.    Question: Given a intarray, please find the sub array which has maximum sum of these elements,return the index, length and sum

2.    Solution: Let's define(sum1, index1, length1) and (sum2, index2, length2) to identify the previous Maximussum(sum2) and current Maximus sum(sum1), if the sum1 is greater or equal sum2,make the (sum2=sum1, index2=index2, length2=length1), then try to find the lagersum1 until every elements are iterated. Before iterate the last element, the sum2is always greater or equal to sum1;

 

Defect: Thissolution can’t cover that case there are 2 more than 2 sub array has the Maximussum.

·        initialization:

int sum1;
int index1=0;
int length1=1;

int sum2;
int index2=0;
int length2=1;

sum1=array [0];
sum2=array[0];

·        Determine if we should add array[i]into Sum1

 

 

Sum1

Array[i]

Determination  logic

Test scenario

0  

>=

>=

Add array[i] to Sum1, then

(sum2, index2, length2) =(sum1, index1, length1)

Case:

Sum1=10;

Array[i]=2;

 

New  sum1=12;

Compare  Sum2 and Sum1:

Sum2=  Sum2<=Sum1?Sum1:Sum2

0  

>=

If sum1+array[i]>=0, add array[i] to sum1, but don’t do (sum2, index2, length2) =(sum1,  index1, length1)

 

If sum1+array[i]<0, we can say the array[i] is Negative  equity, after add it, the sum1 even become negative

 

Since the sum2 keep the Maximus also,  initial the sum1 again to try to find  larger sum greater than sum2

Case:

Sum1=10;

Array[i]=-8;

 

New  sum1=2;

Sum2(not  change)

Case:

Sum1=10;

Array[i]=-12;

 

New  sum1=-12;

Sum2(not  change)

0  

>=   

Set sum1=array[i];

Case

Sum1=-10;

Array[i]=2;

 

New sum1=2,

Sum2=  Sum2<=Sum1?Sum1:Sum2

0  

<  

If array[i]>sum1,

Then sum1=array[i];

Case:

Sum1=-10

Array[i]=-8;

 

Sum1=-8;

Sum2=Sum2<=Sum1?Sum1:Sum2

 

 

3.      Implement by C#

 

4.  public class GetMaxSum

5.      {

6.          private  int index;

7.          private  int length;

8.          private  int sum;

9.   

10.         public  int Index

11.         {

12.             get

13.             {

14.                 return  index;

15.             }

16.             set

17.             {

18.                 index = value;

19.             }

20.         }

21.         public  int Length

22.         {

23.             get

24.             {

25.                 return  length;

26.             }

27.             set

28.             {

29.                 length = value;

30.             }

31.         }

32.         public  int Sum

33.         {

34.             get

35.             {

36.                 return  sum;

37.             }

38.             set

39.             {

40.                 sum = value;

41.             }

42.         }

43.  

44.         public  GetMaxSum()

45.         {

46.             index = default(int);

47.             length = default(int);

48.             sum = default(int);

49.         }

50.  

51.         public  void MaxSumOfSub(int[]  array)

52.         {

53.             if  (array.Length == 0)

54.             {

55.                 return;

56.             }

57.  

58.             int  sum1;

59.             int  index1 = 0;

60.             int  length1 = 1;

61.             int  sum2;

62.             int  index2 = 0;

63.             int  length2 = 1;

64.  

65.             sum1 = array[0];

66.             sum2 = array[0];

67.  

68.             for  (int i = 1; i <= array.Length - 1; i++)

69.             {

70.                 if  (sum1 >= sum2)

71.                 {

72.                     sum2 = sum1;

73.                     length2 = length1;

74.                     index2 = index1;

75.                 }

76.  

77.                 if  (sum1 >= 0 && array[i] >= 0)

78.                 {

79.                     sum1 += array[i];

80.                     length1++;

81.                 }

82.                 if  (sum1 >= 0 && array[i] < 0)

83.                 {

84.                     if  (sum1 + array[i] >= 0)

85.                     {

86.                         sum1 += array[i];

87.                         length1++;

88.                     }

89.                     else

90.                     {

91.                         sum1 = array[i];

92.                         index1 = i;

93.                         length1 = 1;

94.                     }

95.                 }

96.                 if  (sum1 < 0 && array[i] >= 0)

97.                 {

98.                     sum1 = array[i];

99.                     index1 = i;

100.                              length1 = 1;

101.                          }

102.                          if  (sum1 < 0 && array[i] < 0)

103.                          {

104.                              if  (sum1 <= array[i])

105.                              {

106.                                  sum1 = array[i];

107.                                  index1 = i;

108.                                  length1 = 1;

109.                              }

110.                          }

111.                      }

112.           

113.                      if  (sum1 >= sum2)

114.                      {

115.                          sum = sum1;

116.                          index = index1;

117.                          Length = length1;

118.                      }

119.                      else

120.                      {

121.                          sum = sum2;

122.                          index = index2;

123.                          length = length2;

124.                      }

125.           

126.                  }

127.           

128.           

129.              }

130.                         

 

 

4. Test case:

input {1,2,3,4,5}:

input {1,2,3,4,5,-1,1,2}

input {1,2,3,4,5,-1,-2,-3,-4,-5,1}

input {1,2,3,4,5,,-1,-2,-3,-4,-5,2,100}

input {-5,-4,-3,-2,-1}

input{-5,0}

input{-5,5,0}

......

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
解释以下代码ISR(PCINT2_vect) { uint8_t mask; uint8_t pin; uint16_t cTime,dTime; static uint16_t edgeTime[8]; static uint8_t PCintLast; pin = PIND; mask = pin ^ PCintLast; // doing a ^ between the current interruption and the last one indicates wich pin changed sei(); // re enable other interrupts at this point, the rest of this interrupt is not so time critical and can be interrupted safely PCintLast = pin; // we memorize the current state of all PINs [D0-D7] cTime = micros(); // micros() return a uint32_t, but it is not usefull to keep the whole bits => we keep only 16 bits if (mask & 1<<2) { //indicates the bit 2 of the arduino port [D0-D7], that is to say digital pin 2, if 1 => this pin has just changed if (!(pin & 1<<2)) { //indicates if the bit 2 of the arduino port [D0-D7] is not at a high state (so that we match here only descending PPM pulse) dTime = cTime-edgeTime[2]; if (900<dTime && dTime<2200) rcValue[2] = dTime; // just a verification: the value must be in the range [1000;2000] + some margin } else edgeTime[2] = cTime; // if the bit 2 of the arduino port [D0-D7] is at a high state (ascending PPM pulse), we memorize the time } if (mask & 1<<4) { //same principle for other channels // avoiding a for() is more than twice faster, and it's important to minimize execution time in ISR if (!(pin & 1<<4)) { dTime = cTime-edgeTime[4]; if (900<dTime && dTime<2200) rcValue[4] = dTime; } else edgeTime[4] = cTime; } if (mask & 1<<5) { if (!(pin & 1<<5)) { dTime = cTime-edgeTime[5]; if (900<dTime && dTime<2200) rcValue[5] = dTime;//map(dTime,1016,2020,-128,128); } else edgeTime[5] = cTime; } if (mask & 1<<6) { if (!(pin & 1<<6)) { dTime = cTime-edgeTime[6]; if (900<dTime && dTime<2200) rcValue[6] = dTime;//map(dTime,1016,2020,-128,128); } else edgeTime[6] = cTime; } }
07-17
这段代码是一个中断服务程序(ISR),它处理引脚变化中断(Pin Change Interrupt)。下面是对代码的逐行解释: 1. `uint8_t mask;`、`uint8_t pin;`、`uint16_t cTime, dTime;`:定义了一些变量,用于存储引脚状态和时间信息。 2. `static uint16_t edgeTime[8];`、`static uint8_t PCintLast;`:定义了静态变量,用于存储引脚状态的变化时间和上一次中断时的引脚状态。 3. `pin = PIND;`:将当前的PORTD引脚状态读取到变量`pin`中。 4. `mask = pin ^ PCintLast;`:通过对当前引脚状态和上一次中断时的引脚状态进行异或操作,得到一个表示哪些引脚发生了变化的掩码。 5. `sei();`:重新开启其他中断。在这一点上,该中断的剩余部分不那么时间关键,可以安全地被其他中断打断。 6. `PCintLast = pin;`:将当前引脚状态存储到静态变量`PCintLast`中,以备下一次中断时使用。 7. `cTime = micros();`:获取当前的微秒计数值,存储在变量`cTime`中。 8. 下面的代码块通过检查掩码的各位来确定哪些引脚发生了变化,并根据变化的类型(上升沿或下降沿)更新相应的数据。 - `if (mask & 1<<2)`:检查第2位是否为1,即引脚2是否发生了变化。 - `if (!(pin & 1<<2))`:检查引脚2是否处于低电平状态,即检测到下降沿。 - `dTime = cTime-edgeTime[2]; if (900<dTime && dTime<2200) rcValue[2] = dTime;`:计算引脚2变化的时间间隔,并将其存储在变量`dTime`中。如果`dTime`的值在900和2200之间,就将其存储在数组`rcValue`的第2个元素中。 - `else edgeTime[2] = cTime;`:如果引脚2处于高电平状态,即检测到上升沿,则将当前时间存储在`edgeTime[2]`中。 通过类似的方式,代码块处理了引脚4、5和6的变化,并相应地更新了数据。 综上所述,这段代码是一个中断服务程序,用于处理引脚变化中断。它根据引脚的上升沿和下降沿来更新相应的数据,并将其存储在相应的数组中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值