Skip to content

Commit 93853d0

Browse files
committed
new soln
1 parent 433cbc8 commit 93853d0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

1235.JobScheduling.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 1235. Maximum Profit in Job Scheduling
2+
// We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].
3+
// You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.
4+
// If you choose a job that ends at time X you will be able to start another job that starts at time X.
5+
// Example 1:
6+
// Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
7+
// Output: 120
8+
// Explanation: The subset chosen is the first and fourth job.
9+
// Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
10+
// Example 2:
11+
// Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
12+
// Output: 150
13+
// Explanation: The subset chosen is the first, fourth and fifth job.
14+
// Profit obtained 150 = 20 + 70 + 60.
15+
// Example 3:
16+
// Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
17+
// Output: 6
18+
public class Solution {
19+
int[] maxProfit;
20+
public int JobScheduling(int[] startTime, int[] endTime, int[] profit)
21+
{
22+
maxProfit = new int[profit.Length];
23+
(int start, int end, int profit)[] jobs = new (int, int, int)[profit.Length];
24+
for(int i = 0; i < profit.Length; i++)
25+
{
26+
jobs[i] = (startTime[i], endTime[i], profit[i]);
27+
}
28+
jobs = jobs.OrderBy(x => x.start).ToArray();
29+
Array.Fill(maxProfit, -1);
30+
return Scheduling(jobs, 0, 0);
31+
}
32+
public int Scheduling((int start, int end, int profit)[] jobs, int idx, int time)
33+
{
34+
if (maxProfit[idx] == -1) {
35+
int exProfit = idx + 1 < jobs.Length ? Scheduling(jobs, idx + 1, time) : 0;
36+
int inProfit = jobs[idx].profit;
37+
for(int i = idx+1; i < jobs.Length; i++) {
38+
if (jobs[i].start >= jobs[idx].end)
39+
{
40+
inProfit += Scheduling(jobs, i, time + jobs[idx].end);
41+
break;
42+
}
43+
}
44+
maxProfit[idx] = Math.Max(inProfit, exProfit);
45+
}
46+
return maxProfit[idx];
47+
}
48+
}

93.RestoreIpAddresses.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 93. Restore IP Addresses
2+
// A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
3+
// For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
4+
// Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.
5+
// Example 1:
6+
// Input: s = "25525511135"
7+
// Output: ["255.255.11.135","255.255.111.35"]
8+
// Example 2:
9+
// Input: s = "0000"
10+
// Output: ["0.0.0.0"]
11+
// Example 3:
12+
// Input: s = "101023"
13+
// Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
14+
15+
public IList<string> RestoreIpAddresses(string s)
16+
{
17+
IList<string> result = new List<string>();
18+
string ip = "";
19+
//Though there are 3 loops, entire string will be iterated 3*3*3 i.e 27 times only.
20+
for (int i = 1; i < 4; i++)
21+
{
22+
for (int j = 1; j < 4; j++)
23+
{
24+
for (int k = 1; k < 4; k++)
25+
{
26+
if (i + j + k < s.Length) //to ensure size of D is atleast 1
27+
{
28+
//Converting in int first will ensure each part has maximum onr 0's.
29+
int A = Int32.Parse(s.Substring(0, i));
30+
int B = Int32.Parse(s.Substring(i, j));
31+
int C = Int32.Parse(s.Substring(i + j, k));
32+
long D = Int64.Parse(s.Substring(i + j + k, s.Length - (i + j + k))); //making it long as last part value make exceed 32 bits.
33+
if (A <= 255 && B <= 255 &&
34+
C <= 255 && D <= 255)
35+
{
36+
ip = A + "." + B + "." + C + "." + D;
37+
//re-checking length as in case of multiple 0's in single part, length will get reduced, and this value should be droped;
38+
if (ip.Length == s.Length + 3)
39+
result.Add(ip);
40+
}
41+
}
42+
}
43+
}
44+
}
45+
return result;
46+
}

0 commit comments

Comments
 (0)