Skip to content

Commit 81759de

Browse files
committed
update
1 parent 79eb466 commit 81759de

7 files changed

+209
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// AC: 187 ms
2+
// Memory: 0 KB
3+
// Factorial replace: 4! = 2! * 2!, 6! = 5! * 3!, 8! = 7! * 2! * 2! * 2!, 9! = 7! * 3! * 3! * 2!
4+
// T:O(n), S:O(n)
5+
//
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Scanner;
10+
11+
public class Codeforces_0515C_Drazil_and_Factorial {
12+
public static void main(String[] args) {
13+
Scanner sc = new Scanner(System.in);
14+
int n = sc.nextInt();
15+
String num = sc.next();
16+
List<Integer> digits = new ArrayList<>();
17+
for (int i = 0; i < num.length(); i++) {
18+
char c = num.charAt(i);
19+
if (c == '2' || c == '3' || c == '5' || c == '7') {
20+
digits.add(c - '0');
21+
} else if (c == '4') {
22+
digits.add(3);
23+
digits.add(2);
24+
digits.add(2);
25+
} else if (c == '6') {
26+
digits.add(5);
27+
digits.add(3);
28+
} else if (c == '8') {
29+
digits.add(7);
30+
digits.add(2);
31+
digits.add(2);
32+
digits.add(2);
33+
} else if (c == '9') {
34+
digits.add(7);
35+
digits.add(3);
36+
digits.add(3);
37+
digits.add(2);
38+
}
39+
}
40+
digits.sort(Collections.reverseOrder());
41+
StringBuilder ret = new StringBuilder();
42+
for (int digit : digits) {
43+
ret.append(digit);
44+
}
45+
46+
System.out.println(ret);
47+
}
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// AC: Runtime 7 ms Beats 50.42% of users with Java
2+
// Memory 44.70 MB Beats 46.78% of users with Java
3+
// Calculate by each cell.
4+
// T:O(m * n * 9), S:O(m * n)
5+
//
6+
class Solution {
7+
public int[][] imageSmoother(int[][] img) {
8+
int row = img.length, col = img[0].length;
9+
int[][] ret = new int[row][col];
10+
for (int i = 0; i < row; i++) {
11+
for (int j = 0; j < col; j++) {
12+
int sum = 0, count = 0;
13+
for (int k = i - 1; k <= i + 1; k++) {
14+
for (int t = j - 1; t <= j + 1; t++) {
15+
if (k >= 0 && k < row && t >= 0 && t < col) {
16+
sum += img[k][t];
17+
count++;
18+
}
19+
}
20+
}
21+
ret[i][j] = (int) Math.floor(sum * 1.0 / count);
22+
}
23+
}
24+
25+
return ret;
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// AC: Runtime 906 ms Beats 5.08% of users with Java
2+
// Memory 70.56 MB Beats 83.62% of users with Java
3+
// PriorityQueue & inner class sorting.
4+
// T:O(logn), S:O(n)
5+
//
6+
class FoodRatings {
7+
HashMap<String, Food> foodInfo;
8+
HashMap<String, PriorityQueue<Food>> cuisineToFood;
9+
10+
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
11+
foodInfo = new HashMap<>();
12+
cuisineToFood = new HashMap<>();
13+
for (int i = 0; i < foods.length; i++) {
14+
Food food = new Food(foods[i], cuisines[i], ratings[i]);
15+
foodInfo.put(foods[i], food);
16+
PriorityQueue<Food> pq = new PriorityQueue<>((a, b) ->
17+
!a.rating.equals(b.rating) ? (b.rating - a.rating) : a.name.compareTo(b.name));
18+
cuisineToFood.putIfAbsent(cuisines[i], pq);
19+
cuisineToFood.get(cuisines[i]).add(food);
20+
}
21+
}
22+
23+
public void changeRating(String food, int newRating) {
24+
PriorityQueue<Food> pq = cuisineToFood.get(foodInfo.get(food).cuisine);
25+
pq.remove(foodInfo.get(food));
26+
foodInfo.get(food).rating = newRating;
27+
pq.add(foodInfo.get(food));
28+
}
29+
30+
public String highestRated(String cuisine) {
31+
return cuisineToFood.get(cuisine).peek().name;
32+
}
33+
34+
static class Food {
35+
Integer rating;
36+
String name, cuisine;
37+
38+
Food(String name, String cuisine, Integer rating) {
39+
this.rating = rating;
40+
this.name = name;
41+
this.cuisine = cuisine;
42+
}
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// AC: Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 58.48 MB Beats 21.79% of users with Java
3+
// Greedy.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public long maxArrayValue(int[] nums) {
8+
int len = nums.length;
9+
long curMax = nums[len - 1], ret = curMax;
10+
for (int i = len - 1; i >= 1; i--) {
11+
if (curMax >= nums[i - 1]) {
12+
curMax += nums[i - 1];
13+
} else {
14+
curMax = nums[i - 1];
15+
}
16+
ret = Math.max(ret, curMax);
17+
}
18+
19+
return ret;
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// AC: Runtime 59 ms Beats 87.90% of users with Java
2+
// Memory 55.86 MB Beats 97.17% of users with Java
3+
// Sliding-window.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int maxSubarrayLength(int[] nums, int k) {
8+
HashMap<Integer, Integer> count = new HashMap<>();
9+
int leftPos = 0, ret = 0, len = nums.length;
10+
for (int i = 0; i < len; i++) {
11+
count.merge(nums[i], 1, Integer::sum);
12+
if (count.get(nums[i]) > k) {
13+
while (count.get(nums[i]) > k) {
14+
count.merge(nums[leftPos], -1, Integer::sum);
15+
leftPos++;
16+
}
17+
}
18+
ret = Math.max(ret, i - leftPos + 1);
19+
}
20+
21+
return ret;
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// AC: Runtime 4 ms Beats 100.00% of users with Java
2+
// Memory 44.24 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(n^2), S:O(n^2)
5+
//
6+
class Solution {
7+
public int[] findMissingAndRepeatedValues(int[][] grid) {
8+
int row = grid.length, repeated = 0;
9+
long sum = 0, target = (1 + (long) row * row) * row * row / 2;
10+
HashSet<Integer> record = new HashSet<>();
11+
for (int[] rowItem : grid) {
12+
for (int i : rowItem) {
13+
sum += i;
14+
if (record.contains(i)) {
15+
repeated = i;
16+
} else {
17+
record.add(i);
18+
}
19+
}
20+
}
21+
22+
return new int[]{repeated, (int) (target - sum + repeated)};
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// AC: Runtime 24 ms Beats 33.33% of users with Java
2+
// Memory 56.77 MB Beats 66.67% of users with Java
3+
// .
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public int[][] divideArray(int[] nums, int k) {
8+
Arrays.sort(nums);
9+
int len = nums.length;
10+
for (int i = 0; i < len / 3; i++) {
11+
if (nums[3 * i + 2] - nums[3 * i] > k) {
12+
return new int[0][];
13+
}
14+
}
15+
int[][] ret = new int[len / 3][3];
16+
for (int i = 0; i < len / 3; i++) {
17+
ret[i] = new int[]{nums[3 * i], nums[3 * i + 1], nums[3 * i + 2]};
18+
}
19+
20+
return ret;
21+
}
22+
}

0 commit comments

Comments
 (0)