-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay62
91 lines (60 loc) · 1.96 KB
/
Day62
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//486A - Calculating Function
import java.util.Scanner;
public class CalculatingFunction {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long result;
if (n % 2 == 0) {
result = n / 2;
} else {
result = -(n + 1) / 2;
}
System.out.println(result);
}
}
//Magnets
import java.util.Scanner;
public class MagnetGroups {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
int groupCount = 0;
String previousType = null;
for (int i = 0; i < n; i++) {
String currentType = scanner.nextLine();
if (!currentType.equals(previousType)) {
groupCount++;
previousType = currentType;
}
}
System.out.println(groupCount);
}
}
//Problem 3 : 566. Reshape the Matrix
Problem : https://leetcode.com/problems/reshape-the-matrix/description/
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int rows = mat.length;
int cols = mat[0].length;
if((rows * cols) != (r * c)) return mat;
int[][] output = new int[r][c];
int output_rows = 0;
int output_cols = 0;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
output[output_rows][output_cols] = mat[i][j];
output_cols++;
if(output_cols == c)
{
output_cols = 0;
output_rows++;
}
}
}
return output;
}
}