日本一区二区不卡精品,网友分享久久综合亚洲欧美成人,最新国内自拍在线视频,精品凹凸视频在线观看一区,日韩免费一区二区三区伦理片,极品人妻淫av888,久久久噜噜噜久久综合,亚洲激情精品成人在线,六月丁香伊人色婷婷综合网

今熱點(diǎn):LeetCode 1975. Maximum Matrix Sum

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


(資料圖片僅供參考)

Choose any two?adjacent?elements of?matrixand?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.

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

剩下就是幾個(gè)變量,求和項(xiàng)(所有數(shù)取絕對(duì)值),負(fù)數(shù)的數(shù)量,最小的數(shù)(絕對(duì)值之后);

然后分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.

關(guān)鍵詞:

熱點(diǎn)

娛樂(lè)圖賞