File tree Expand file tree Collapse file tree 7 files changed +187
-0
lines changed
3648.minimum-sensors-to-cover-grid
3649.number-of-perfect-pairs
3650.minimum-cost-path-with-edge-reversals
3651.minimum-cost-path-with-teleportations
3653.xor-after-range-multiplication-queries-i
3654.minimum-sum-after-divisible-sum-deletions Expand file tree Collapse file tree 7 files changed +187
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minSensors (int n , int m , int k ) {
3
+ int size = k * 2 + 1 ;
4
+ int x = n / size + (n % size == 0 ? 0 : 1 );
5
+ int y = m / size + (m % size == 0 ? 0 : 1 );
6
+ return x * y ;
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ long long perfectPairs (vector<int >& nums) {
4
+ int n = nums.size ();
5
+
6
+ vector<long long > arr;
7
+ for (auto num : nums){
8
+ long long x = num;
9
+ arr.push_back (abs (x));
10
+ }
11
+ sort (arr.begin (), arr.end ());
12
+
13
+ long long count = 0 ;
14
+ int j = 0 ;
15
+
16
+ for (int i = 0 ; i < n; i++){
17
+ while (j < n && arr[j] <= 2 * arr[i]){
18
+ j++;
19
+ }
20
+ count += (j - i - 1 );
21
+ }
22
+ return count;
23
+ }
24
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public long perfectPairs (int [] nums ) {
3
+ int n = nums .length ;
4
+ long [] arr = new long [n ];
5
+ for (int i = 0 ; i < n ; i ++) {
6
+ arr [i ] = Math .abs ((long ) nums [i ]);
7
+ }
8
+ java .util .Arrays .sort (arr );
9
+
10
+ long count = 0 ;
11
+ int j = 0 ;
12
+ for (int i = 0 ; i < n ; i ++) {
13
+ while (j < n && arr [j ] <= 2 * arr [i ]) {
14
+ j ++;
15
+ }
16
+ count += (j - i - 1 );
17
+ }
18
+ return count ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private static final int INF = Integer .MAX_VALUE / 2 ;
3
+
4
+ public int minCost (int n , int [][] edges ) {
5
+ var graph = new ArrayList <List <int []>>();
6
+ for (int i = 0 ; i < n ; i ++) {
7
+ graph .add (new ArrayList <>());
8
+ }
9
+
10
+ for (int [] edge : edges ) {
11
+ int from = edge [0 ], to = edge [1 ], cost = edge [2 ];
12
+ graph .get (from ).add (new int []{to , cost });
13
+ graph .get (to ).add (new int []{from , cost * 2 });
14
+ }
15
+
16
+ int [] d = new int [n ];
17
+ Arrays .fill (d , INF );
18
+ d [0 ] = 0 ;
19
+
20
+ var q = new PriorityQueue <int []>((p1 , p2 ) ->
21
+ p1 [0 ] == p2 [0 ] ? Integer .compare (p1 [1 ], p2 [1 ]) : Integer .compare (p1 [0 ], p2 [0 ]));
22
+ q .offer (new int []{0 , 0 });
23
+
24
+ while (!q .isEmpty ()) {
25
+ int [] curr = q .poll ();
26
+ int distance = curr [0 ], from = curr [1 ];
27
+ if (distance > d [from ]) continue ;
28
+
29
+ for (int [] edge : graph .get (from )) {
30
+ int to = edge [0 ], cost = edge [1 ];
31
+ if (d [to ] > d [from ] + cost ) {
32
+ d [to ] = d [from ] + cost ;
33
+ q .offer (new int []{d [to ], to });
34
+ }
35
+ }
36
+ }
37
+
38
+ return d [n - 1 ] == INF ? -1 : d [n - 1 ];
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minCost (int [][] grid , int k ) {
3
+ int m =grid .length ;
4
+ int n =grid [0 ].length ;
5
+ int [][][]dp =new int [m +1 ][n +1 ][k +1 ];
6
+
7
+ for (int i =0 ;i <=m ;i ++){
8
+ for (int j =0 ;j <=n ;j ++){
9
+ for (int l =0 ;l <=k ;l ++){
10
+ dp [i ][j ][l ]=(int )1e9 ;
11
+ }
12
+ }
13
+ }
14
+
15
+ int N =(int )1e4 ;
16
+ int []pbest =new int [N +1 ];
17
+ Arrays .fill (pbest ,(int )1e9 );
18
+
19
+ for (int l =0 ;l <=k ;l ++){
20
+ int []currbest =new int [N +1 ];
21
+ Arrays .fill (currbest ,(int )1e9 );
22
+ for (int i =m -1 ;i >=0 ;i --){
23
+ for (int j =n -1 ;j >=0 ;j --){
24
+
25
+ // base case
26
+ if (i ==m -1 && j ==n -1 ){
27
+ dp [i ][j ][l ]=0 ;
28
+
29
+ }
30
+
31
+ // transitions
32
+ else {
33
+ int mini =(int )1e9 ;
34
+ if (i +1 <=m -1 ) mini =Math .min (mini ,grid [i +1 ][j ]+dp [i +1 ][j ][l ]);
35
+ if (j +1 <=n -1 ) mini =Math .min (mini ,grid [i ][j +1 ]+dp [i ][j +1 ][l ]);
36
+ if (l >0 ){
37
+ mini =Math .min (mini ,0 +pbest [grid [i ][j ]] );
38
+ }
39
+ dp [i ][j ][l ]=mini ;
40
+ }
41
+ currbest [grid [i ][j ]]=Math .min (currbest [grid [i ][j ]],dp [i ][j ][l ]);
42
+
43
+
44
+ }
45
+ }
46
+
47
+
48
+ pbest =new int [N +1 ];
49
+ Arrays .fill (pbest ,(int )1e9 );
50
+ pbest [0 ]=currbest [0 ];
51
+
52
+ for (int i =1 ;i <=N ;i ++){
53
+ pbest [i ]=Math .min (pbest [i -1 ],currbest [i ]);
54
+ }
55
+
56
+ }
57
+
58
+ return dp [0 ][0 ][k ];
59
+
60
+ }
61
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private static final int MOD = 1000000007 ;
3
+ public int xorAfterQueries (int [] nums , int [][] q ) {
4
+ for (int [] query : q ) {
5
+ int l = query [0 ];
6
+ int r = query [1 ];
7
+ int k = query [2 ];
8
+ int v = query [3 ];
9
+ while (l <= r ) {
10
+ nums [l ] = (int )(((long )nums [l ] * v ) % MOD );
11
+ l += k ;
12
+ }
13
+ }
14
+ int ans = 0 ;
15
+ for (int num : nums ) {
16
+ ans ^= num ;
17
+ }
18
+ return ans ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public long minArraySum (int [] A , int k ) {
3
+ long [] dp = new long [k ];
4
+ Arrays .fill (dp , Long .MAX_VALUE );
5
+ dp [0 ] = 0 ;
6
+ long res = 0 ;
7
+ for (int a : A ) {
8
+ res += a ;
9
+ int index = (int ) (res % k );
10
+ res = dp [index ] = Math .min (dp [index ], res );
11
+ }
12
+ return res ;
13
+ }
14
+ }
You can’t perform that action at this time.
0 commit comments