Skip to content

Commit 1d4d943

Browse files
committed
Add the Swap for Largest Number problem under the Greedy section
1 parent 0715864 commit 1d4d943

9 files changed

+64
-11
lines changed

src/greedy/InsertInterval.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Approach: Follow Solution Link; Time Complexity: O(N)
1010

1111
public class InsertInterval {
12-
12+
1313
public int[][] insert(int[][] intervals, int[] injected) {
1414

1515
List<int[]> results = new ArrayList<>();

src/greedy/JobSequencing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public int compute(int[][] jobs) {
1717
int profit = 0, size = jobs.length;
1818
boolean[] days = new boolean[size];
1919

20-
for (int job = 0; job < size; job++) {
20+
for (int job = 0; job < size; job += 1) {
2121

2222
int deadline = Math.min(size, jobs[job][0]);
2323

2424
// Try to do Jobs on their Last Day of Deadline
25-
for (int day = deadline - 1; day >= 0; day--) {
25+
for (int day = deadline - 1; day >= 0; day -= 1) {
2626

2727
// Check if Day is Free
2828
if (days[day] == false) {

src/greedy/MaxCircularSum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public int compute(int[] nums) {
2727

2828
// If all Numbers are NEGATIVE
2929
if (maxSum <= 0) return maxSum;
30+
3031
return Math.max(maxSum, totalSum - minSum);
3132
}
3233
}

src/greedy/MergeIntervals.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class MergeIntervals {
1313

1414
public int[][] compute(int[][] intervals) {
1515

16-
// Sort all the Intervals in Ascending Order w.r.t. their Start Positions
17-
Arrays.parallelSort(intervals, Comparator.comparingInt(interval -> interval[0]));
16+
// Sort all the Intervals in Ascending Order w.r.t. the Start Positions
17+
Arrays.sort(intervals, Comparator.comparingInt(interval -> interval[0]));
1818

1919
List<int[]> results = new ArrayList<>();
2020

src/greedy/MinimumArrows.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public int compute(int[][] points) {
1616

1717
int arrows = 1, previous = 0, size = points.length;
1818

19-
for (int current = 1; current < size; current++) {
19+
for (int current = 1; current < size; current += 1) {
2020

2121
// Verify if the current Balloon is NOT
2222
// overlapping with the current Balloon

src/greedy/MostProfitBoxes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Time Complexity: O(NlogN), Space Complexity: O(1) or O(N) depending on Sorting Algorithm
1010

1111
public class MostProfitBoxes {
12-
12+
1313
public int compute(int[][] boxTypes, int truckSize) {
1414

1515
// Sort ALL the Box types in Non-increasing Order w.r.t. their Units

src/greedy/ScheduleMeetings.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
// Approach: Sort, Check if Overlapping & Add when NOT so; Time Complexity: O(N + NlogN + N) = O(NlogN)
1212

1313
public class ScheduleMeetings {
14-
14+
1515
public static List<Integer> maximumMeetings(int[] start, int[] finish) {
1616

1717
List<Integer> result = new ArrayList<>();
1818

1919
int previous = -1, size = finish.length;
2020
int[][] meets = new int[size][3];
2121

22-
for (int time = 0; time < size; time++) {
22+
for (int time = 0; time < size; time += 1) {
2323

2424
meets[time][0] = time + 1;
2525
meets[time][1] = start[time];
@@ -28,7 +28,7 @@ public static List<Integer> maximumMeetings(int[] start, int[] finish) {
2828

2929
Arrays.sort(meets, Comparator.comparingInt(meet -> meet[2]));
3030

31-
for (int index = 0; index < size; index++) {
31+
for (int index = 0; index < size; index += 1) {
3232

3333
if (previous < meets[index][1]) {
3434

src/greedy/SeparateIntervals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public int compute(int[][] intervals) {
1616

1717
int overlapped = 1, previous = 0, size = intervals.length;
1818

19-
for (int current = 1; current < size; current++) {
19+
for (int current = 1; current < size; current += 1) {
2020

2121
// Verify if the current Interval is NOT
2222
// overlapping with the current Interval

src/greedy/SwapForLargest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package greedy;
2+
3+
// Problem Link: https://leetcode.com/problems/maximum-swap/
4+
// Solution Link: https://leetcode.com/problems/maximum-swap/solutions/107068/java-simple-solution-o-n-time/
5+
6+
// Approach: Use BUCKETS to Store INDICES of All DIGITS; Time Complexity: O(N)
7+
8+
public class SwapForLargest {
9+
10+
public int compute(int num) {
11+
12+
String number = String.valueOf(num);
13+
char[] digits = number.toCharArray();
14+
15+
int digit, top = 9, bucket;
16+
int size = digits.length;
17+
18+
int[] buckets = new int[10];
19+
20+
for (int index = 0; index < size; index += 1) {
21+
22+
digit = digits[index] - '0';
23+
24+
buckets[digit] = index;
25+
}
26+
27+
for (int index = 0; index < size; index += 1) {
28+
29+
digit = digits[index] - '0';
30+
31+
for (int mark = top; mark > digit; mark -= 1) {
32+
33+
bucket = buckets[mark];
34+
35+
if (bucket > index) {
36+
37+
char backup = digits[index];
38+
digits[index] = digits[bucket];
39+
digits[bucket] = backup;
40+
41+
String largest = new String(digits);
42+
43+
return Integer.valueOf(largest);
44+
}
45+
}
46+
47+
top = digit;
48+
}
49+
50+
return num;
51+
}
52+
}

0 commit comments

Comments
 (0)