五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 1975. Maximum Matrix Sum

2023-04-20 12:54 作者:您是打尖兒還是住店呢  | 我要投稿

You are given an?n x n?integer?matrix. You can do the following operation?any?number of times:

  • Choose any two?adjacent?elements of?matrix?and?multiply?each of them by?-1.

Two elements are considered?adjacent?if and only if they share a?border.

Your goal is to?maximize?the summation of the matrix's elements. Return?the?maximum?sum of the matrix's elements using the operation mentioned above.

?

Example 1:

Input: matrix = [[1,-1],[-1,1]]

Output: 4

Explanation:?

We can follow the following steps to reach sum equals 4:?

- Multiply the 2 elements in the first row by -1.?

- Multiply the 2 elements in the first column by -1.

Example 2:

Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]

Output: 16

Explanation:?

We can follow the following step to reach sum equals 16:?

- Multiply the 2 last elements in the second row by -1.

?

因為2個相鄰的數(shù)字可以同時乘以-1,所以我們就可以將任意的數(shù)字組合乘以-1,這時候就要計算數(shù)組中一共有多少個負數(shù),如果是偶數(shù)個,那么一定可以全部取正,如果是奇數(shù)個,我們就讓最小的那個數(shù)字變成負數(shù)即可;

剩下就是幾個變量,求和項(所有數(shù)取絕對值),負數(shù)的數(shù)量,最小的數(shù)(絕對值之后);

然后分2種情況依次返回即可;

題目不算太難的。

Constraints:

  • n == matrix.length == matrix[i].length

  • 2 <= n <= 250

  • -105?<= matrix[i][j] <= 105

Runtime:?6 ms, faster than?93.02%?of?Java?online submissions for?Maximum Matrix Sum.

Memory Usage:?53 MB, less than?36.05%?of?Java?online submissions for?Maximum Matrix Sum.


LeetCode 1975. Maximum Matrix Sum的評論 (共 條)

分享到微博請遵守國家法律
蕲春县| 巴南区| 柳林县| 崇信县| 克拉玛依市| 上栗县| 金沙县| 秀山| 平湖市| 镇康县| 札达县| 南涧| 类乌齐县| 龙井市| 邢台市| 新和县| 建宁县| 昌都县| 鹿泉市| 安陆市| 慈利县| 宕昌县| 紫阳县| 石景山区| 黄大仙区| 武功县| 乌兰浩特市| 朔州市| 太谷县| 琼结县| 昔阳县| 清水河县| 龙游县| 海伦市| 乳山市| 铅山县| 洪湖市| 宣化县| 石渠县| 澄城县| 花莲市|