-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily160.java
More file actions
71 lines (57 loc) · 2.11 KB
/
Copy pathdaily160.java
File metadata and controls
71 lines (57 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Solution 1 - FAIL
class solution {
public long res = 0;
public long maxmatrixsum(int[][] matrix) {
/**
back track
find intial sum of matrix, set that as max
start with 0, 0
for each adjacent element, flip and calculate sum
recurse down w that
update max sum
weird doesn"t work
look at where most negatives are?
greedy
how?
**/
long sum = 0;
for (int[] r : matrix) {
for (int n : r) {
sum += n;
}
}
res = sum;
return res;
}
}
// Solution 2
class Solution {
public long maxMatrixSum(int[][] matrix) {
// Step 1: Initialize variables
int min = Integer.MAX_VALUE; // To track the smallest absolute value in the matrix
long sum = 0; // To accumulate the total sum of absolute values
int negCount = 0; // To count the number of negative elements in the matrix
// Step 2: Iterate over each element in the matrix
for (int i = 0; i < matrix.length; i++) { // Loop through rows
for (int j = 0; j < matrix[0].length; j++) { // Loop through columns
// Check if the current element is negative
if (matrix[i][j] < 0) {
negCount++; // Increment the negative count
}
// Compute the absolute value of the current element
int ab = Math.abs(matrix[i][j]);
// Update the smallest absolute value seen so far
min = Math.min(min, ab);
// Add the absolute value to the total sum
sum += ab;
}
}
// Step 3: Determine the result based on the number of negative elements
if (negCount % 2 == 0) {
// If the count of negative elements is even, return the total sum
return sum;
}
// If the count of negative elements is odd, subtract twice the smallest absolute value
return sum - 2 * min;
}
}