Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81b1150

Browse files
Added tasks 371, 372, 373.
1 parent 4ad6095 commit 81b1150

File tree

9 files changed

+333
-0
lines changed

9 files changed

+333
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0301_0400.s0371_sum_of_two_integers;
2+
3+
// #Medium #Top_Interview_Questions #Math #Bit_Manipulation
4+
5+
public class Solution {
6+
public int getSum(int a, int b) {
7+
int ans = 0;
8+
int memo = 0;
9+
int exp = 0;
10+
int count = 0;
11+
while (count < 32) {
12+
int val1 = a & 1;
13+
int val2 = b & 1;
14+
int val = sum(val1, val2, memo);
15+
memo = val >> 1;
16+
val = val & 1;
17+
a = a >> 1;
18+
b = b >> 1;
19+
ans = ans | (val << exp);
20+
exp = plusOne(exp);
21+
count = plusOne(count);
22+
}
23+
return ans;
24+
}
25+
26+
private int sum(int val1, int val2, int val3) {
27+
int count = 0;
28+
if (val1 == 1) {
29+
count = plusOne(count);
30+
}
31+
if (val2 == 1) {
32+
count = plusOne(count);
33+
}
34+
if (val3 == 1) {
35+
count = plusOne(count);
36+
}
37+
return count;
38+
}
39+
40+
private int plusOne(int val) {
41+
return (-(~val));
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
371\. Sum of Two Integers
2+
3+
Medium
4+
5+
Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
6+
7+
**Example 1:**
8+
9+
**Input:** a = 1, b = 2
10+
11+
**Output:** 3
12+
13+
**Example 2:**
14+
15+
**Input:** a = 2, b = 3
16+
17+
**Output:** 5
18+
19+
**Constraints:**
20+
21+
* `-1000 <= a, b <= 1000`
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package g0301_0400.s0372_super_pow;
2+
3+
// #Medium #Math #Divide_and_Conquer
4+
5+
public class Solution {
6+
private static final int MOD = 1337;
7+
8+
public int superPow(int a, int[] b) {
9+
int phi = phi(MOD);
10+
int arrMod = arrMod(b, phi);
11+
if (isGreaterOrEqual(b, phi)) {
12+
// Cycle has started
13+
// cycle starts at phi with length phi
14+
return exp(a % MOD, phi + arrMod);
15+
}
16+
return exp(a % MOD, arrMod);
17+
}
18+
19+
private int phi(int n) {
20+
float result = n;
21+
for (int p = 2; p * p <= n; p++) {
22+
if (n % p > 0) {
23+
continue;
24+
}
25+
while (n % p == 0) {
26+
n /= p;
27+
}
28+
result *= 1.0 - 1.0 / p;
29+
}
30+
if (n > 1) {
31+
// if starting n was also prime (so it was greater than sqrt(n))
32+
result *= (1.0 - (1.0 / n));
33+
}
34+
return (int) result;
35+
}
36+
37+
// Returns true if number in array is greater than integer named phi
38+
private boolean isGreaterOrEqual(int[] b, int phi) {
39+
int cur = 0;
40+
for (int j : b) {
41+
cur = cur * 10 + j;
42+
if (cur >= phi) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
// Returns number in array mod phi
50+
private int arrMod(int[] b, int phi) {
51+
int res = 0;
52+
for (int j : b) {
53+
res = (res * 10 + j) % phi;
54+
}
55+
return res;
56+
}
57+
58+
// Binary exponentiation
59+
private int exp(int a, int b) {
60+
int y = 1;
61+
while (b > 0) {
62+
if (b % 2 == 1) {
63+
y = (y * a) % MOD;
64+
}
65+
a = (a * a) % MOD;
66+
b /= 2;
67+
}
68+
return y;
69+
}
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
372\. Super Pow
2+
3+
Medium
4+
5+
Your task is to calculate <code>a<sup>b</sup></code> mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
6+
7+
**Example 1:**
8+
9+
**Input:** a = 2, b = \[3\]
10+
11+
**Output:** 8
12+
13+
**Example 2:**
14+
15+
**Input:** a = 2, b = \[1,0\]
16+
17+
**Output:** 1024
18+
19+
**Example 3:**
20+
21+
**Input:** a = 1, b = \[4,3,3,8,5,2\]
22+
23+
**Output:** 1
24+
25+
**Example 4:**
26+
27+
**Input:** a = 2147483647, b = \[2,0,0\]
28+
29+
**Output:** 1198
30+
31+
**Constraints:**
32+
33+
* <code>1 <= a <= 2<sup>31</sup> - 1</code>
34+
* `1 <= b.length <= 2000`
35+
* `0 <= b[i] <= 9`
36+
* `b` doesn't contain leading zeros.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0301_0400.s0373_find_k_pairs_with_smallest_sums;
2+
3+
// #Medium #Array #Heap_Priority_Queue
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
private static class Node {
11+
long sum;
12+
List<Integer> al;
13+
int index;
14+
15+
Node(int index, int num1, int num2) {
16+
this.sum = (long) num1 + (long) num2;
17+
this.al = new ArrayList<>();
18+
this.al.add(num1);
19+
this.al.add(num2);
20+
this.index = index;
21+
}
22+
}
23+
24+
public List<List<Integer>> ksmallestPairs(int[] nums1, int[] nums2, int k) {
25+
PriorityQueue<Node> queue = new PriorityQueue<>((a, b) -> a.sum < b.sum ? -1 : 1);
26+
List<List<Integer>> res = new ArrayList<>();
27+
for (int i = 0; i < nums1.length && i < k; i++) {
28+
queue.add(new Node(0, nums1[i], nums2[0]));
29+
}
30+
for (int i = 1; i <= k && !queue.isEmpty(); i++) {
31+
Node cur = queue.poll();
32+
res.add(cur.al);
33+
int next = cur.index;
34+
int lastNum1 = cur.al.get(0);
35+
if (next + 1 < nums2.length) {
36+
queue.add(new Node(next + 1, lastNum1, nums2[next + 1]));
37+
}
38+
}
39+
return res;
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
373\. Find K Pairs with Smallest Sums
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
6+
7+
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
8+
9+
Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> _with the smallest sums_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = \[1,7,11\], nums2 = \[2,4,6\], k = 3
14+
15+
**Output:** \[\[1,2\],\[1,4\],\[1,6\]\]
16+
17+
**Explanation:** The first 3 pairs are returned from the sequence: \[1,2\],\[1,4\],\[1,6\],\[7,2\],\[7,4\],\[11,2\],\[7,6\],\[11,4\],\[11,6\]
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = \[1,1,2\], nums2 = \[1,2,3\], k = 2
22+
23+
**Output:** \[\[1,1\],\[1,1\]\]
24+
25+
**Explanation:** The first 2 pairs are returned from the sequence: \[1,1\],\[1,1\],\[1,2\],\[2,1\],\[1,2\],\[2,2\],\[1,3\],\[1,3\],\[2,3\]
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = \[1,2\], nums2 = \[3\], k = 3
30+
31+
**Output:** \[\[1,3\],\[2,3\]\]
32+
33+
**Explanation:** All possible pairs are returned from the sequence: \[1,3\],\[2,3\]
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
39+
* `nums1` and `nums2` both are sorted in **ascending order**.
40+
* `1 <= k <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0301_0400.s0371_sum_of_two_integers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void getSum() {
11+
assertThat(new Solution().getSum(1, 2), equalTo(3));
12+
}
13+
14+
@Test
15+
void getSum2() {
16+
assertThat(new Solution().getSum(2, 3), equalTo(5));
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0301_0400.s0372_super_pow;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void superPow() {
11+
assertThat(new Solution().superPow(2, new int[] {3}), equalTo(8));
12+
}
13+
14+
@Test
15+
void superPow2() {
16+
assertThat(new Solution().superPow(2, new int[] {1, 0}), equalTo(1024));
17+
}
18+
19+
@Test
20+
void superPow3() {
21+
assertThat(new Solution().superPow(1, new int[] {4, 3, 3, 8, 5, 2}), equalTo(1));
22+
}
23+
24+
@Test
25+
void superPow4() {
26+
assertThat(new Solution().superPow(2147483647, new int[] {2, 0, 0}), equalTo(1198));
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0301_0400.s0373_find_k_pairs_with_smallest_sums;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void ksmallestPairs() {
13+
assertThat(
14+
new Solution().ksmallestPairs(new int[] {1, 7, 11}, new int[] {2, 4, 6}, 3),
15+
equalTo(
16+
new ArrayList<>(
17+
Arrays.asList(
18+
Arrays.asList(1, 2),
19+
Arrays.asList(1, 4),
20+
Arrays.asList(1, 6)))));
21+
}
22+
23+
@Test
24+
void ksmallestPairs2() {
25+
assertThat(
26+
new Solution().ksmallestPairs(new int[] {1, 1, 2}, new int[] {1, 2, 3}, 2),
27+
equalTo(new ArrayList<>(Arrays.asList(Arrays.asList(1, 1), Arrays.asList(1, 1)))));
28+
}
29+
30+
@Test
31+
void ksmallestPairs3() {
32+
assertThat(
33+
new Solution().ksmallestPairs(new int[] {1, 2}, new int[] {3}, 3),
34+
equalTo(new ArrayList<>(Arrays.asList(Arrays.asList(1, 3), Arrays.asList(2, 3)))));
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /