Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/g1401_1500/s1447_simplified_fractions/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g1401_1500.s1447_simplified_fractions;

// #Medium #Math #2022_03_28_Time_33_ms_(69.60%)_Space_67.7_MB_(81.94%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<String> simplifiedFractions(int n) {

List<String> result = new ArrayList<>();

if (n == 1) {
return result;
}
StringBuilder str = new StringBuilder();
for (int denom = 2; denom <= n; denom++) {
for (int num = 1; num < denom; num++) {
if (checkGCD(num, denom) == 1) {
result.add(str.append(num).append("/").append(denom).toString());
}
str.setLength(0);
}
}
return result;
}

private int checkGCD(int a, int b) {

if (a < b) {
return checkGCD(b, a);
}
if (a == b || a % b == 0 || b == 1) {
return b;
}

return checkGCD(a % b, b);
}
}
31 changes: 31 additions & 0 deletions src/main/java/g1401_1500/s1447_simplified_fractions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
1447\. Simplified Fractions

Medium

Given an integer `n`, return _a list of all **simplified** fractions between_ `0` _and_ `1` _(exclusive) such that the denominator is less-than-or-equal-to_ `n`. You can return the answer in **any order**.

**Example 1:**

**Input:** n = 2

**Output:** ["1/2"]

**Explanation:** "1/2" is the only unique fraction with a denominator less-than-or-equal-to 2.

**Example 2:**

**Input:** n = 3

**Output:** ["1/2","1/3","2/3"]

**Example 3:**

**Input:** n = 4

**Output:** ["1/2","1/3","1/4","2/3","3/4"]

**Explanation:** "2/4" is not a simplified fraction because it can be simplified to "1/2".

**Constraints:**

* `1 <= n <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g1401_1500.s1448_count_good_nodes_in_binary_tree;

// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #2022_03_28_Time_2_ms_(99.63%)_Space_60.1_MB_(26.46%)

import com_github_leetcode.TreeNode;

public class Solution {
private int count = 0;

private void traverse(TreeNode root, int max) {
if (root == null) {
return;
}
if (root.val >= max) {
count += 1;
max = root.val;
}
traverse(root.left, max);
traverse(root.right, max);
}

public int goodNodes(TreeNode root) {
traverse(root, Integer.MIN_VALUE);
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
1448\. Count Good Nodes in Binary Tree

Medium

Given a binary tree `root`, a node _X_ in the tree is named **good** if in the path from root to _X_ there are no nodes with a value _greater than_ X.

Return the number of **good** nodes in the binary tree.

**Example 1:**

**![](https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png)**

**Input:** root = [3,1,4,3,null,1,5]

**Output:** 4

**Explanation:** Nodes in blue are **good**.

Root Node (3) is always a good node.

Node 4 -> (3,4) is the maximum value in the path starting from the root.

Node 5 -> (3,4,5) is the maximum value in the path

Node 3 -> (3,1,3) is the maximum value in the path.

**Example 2:**

**![](https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png)**

**Input:** root = [3,3,null,4,2]

**Output:** 3

**Explanation:** Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.

**Example 3:**

**Input:** root = [1]

**Output:** 1

**Explanation:** Root is considered as **good**.

**Constraints:**

* The number of nodes in the binary tree is in the range `[1, 10^5]`.
* Each node's value is between `[-10^4, 10^4]`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g1401_1500.s1449_form_largest_integer_with_digits_that_add_up_to_target;

// #Hard #Array #Dynamic_Programming #2022_03_28_Time_8_ms_(77.46%)_Space_42.2_MB_(81.69%)

import java.util.Arrays;

public class Solution {
public String largestNumber(int[] cost, int target) {
int[][] dp = new int[10][5001];
Arrays.fill(dp[0], -1);

for (int i = 1; i <= cost.length; i++) {
for (int j = 1; j <= target; j++) {
if (cost[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
int temp =
(dp[i - 1][j - cost[i - 1]] == -1)
? -1
: 1 + dp[i - 1][j - cost[i - 1]];
int t = (dp[i][j - cost[i - 1]] == -1) ? -1 : 1 + dp[i][j - cost[i - 1]];
if (t != -1 && temp == -1) {
temp = t;
} else {
temp = Math.max(t, temp);
}

if (dp[i - 1][j] == -1) {
dp[i][j] = temp;
} else if (temp == -1) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.max(temp, dp[i - 1][j]);
}
}
}
}
if (dp[9][target] == -1) {
return "0";
}
int i = 9;
StringBuilder result = new StringBuilder();
while (target > 0) {

if ((target - cost[i - 1] >= 0 && dp[i][target - cost[i - 1]] + 1 == dp[i][target])
|| (target - cost[i - 1] >= 0
&& dp[i - 1][target - cost[i - 1]] + 1 == dp[i][target])) {
result.append(i);
target -= cost[i - 1];
} else {
i--;
}
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
1449\. Form Largest Integer With Digits That Add up to Target

Hard

Given an array of integers `cost` and an integer `target`, return _the **maximum** integer you can paint under the following rules_:

* The cost of painting a digit `(i + 1)` is given by `cost[i]` (**0-indexed**).
* The total cost used must be equal to `target`.
* The integer does not have `0` digits.

Since the answer may be very large, return it as a string. If there is no way to paint any integer given the condition, return `"0"`.

**Example 1:**

**Input:** cost = [4,3,2,5,6,7,2,5,5], target = 9

**Output:** "7772"

**Explanation:** The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2\*3+ 3\*1 = 9. You could also paint "977", but "7772" is the largest number.

**Digit cost**

1 -> 4

2 -> 3

3 -> 2

4 -> 5

5 -> 6

6 -> 7

7 -> 2

8 -> 5

9 -> 5

**Example 2:**

**Input:** cost = [7,6,5,5,5,6,8,7,8], target = 12

**Output:** "85"

**Explanation:** The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12.

**Example 3:**

**Input:** cost = [2,4,6,2,4,6,4,4,4], target = 5

**Output:** "0"

**Explanation:** It is impossible to paint any integer with total cost equal to target.

**Constraints:**

* `cost.length == 9`
* `1 <= cost[i], target <= 5000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g1401_1500.s1450_number_of_students_doing_homework_at_a_given_time;

// #Easy #Array #2022_03_28_Time_0_ms_(100.00%)_Space_40.3_MB_(86.45%)

public class Solution {
public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int count = 0;
for (int i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1450\. Number of Students Doing Homework at a Given Time

Easy

Given two integer arrays `startTime` and `endTime` and given an integer `queryTime`.

The `ith` student started doing their homework at the time `startTime[i]` and finished it at time `endTime[i]`.

Return _the number of students_ doing their homework at time `queryTime`. More formally, return the number of students where `queryTime` lays in the interval `[startTime[i], endTime[i]]` inclusive.

**Example 1:**

**Input:** startTime = [1,2,3], endTime = [3,2,7], queryTime = 4

**Output:** 1

**Explanation:** We have 3 students where:

The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.

The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.

The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

**Example 2:**

**Input:** startTime = [4], endTime = [4], queryTime = 4

**Output:** 1

**Explanation:** The only student was doing their homework at the queryTime.

**Constraints:**

* `startTime.length == endTime.length`
* `1 <= startTime.length <= 100`
* `1 <= startTime[i] <= endTime[i] <= 1000`
* `1 <= queryTime <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g1401_1500.s1451_rearrange_words_in_a_sentence;

// #Medium #String #Sorting #2022_03_28_Time_21_ms_(89.94%)_Space_42.8_MB_(98.28%)

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Solution {
public String arrangeWords(String text) {
TreeMap<Integer, List<String>> map = new TreeMap<>();
String[] words = text.split(" ");
for (String word : words) {
int len = word.length();
map.putIfAbsent(len, new ArrayList<>());

map.get(len).add(word.toLowerCase());
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Map.Entry<Integer, List<String>> len : map.entrySet()) {
List<String> strings = map.get(len.getKey());
for (String str : strings) {
if (first) {
str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
first = false;
}
sb.append(str).append(" ");
}
}
return sb.substring(0, sb.length() - 1);
}
}
Loading