Skip to content

Commit fc4213d

Browse files
committed
new soln
1 parent 43b99e9 commit fc4213d

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

Diff for: 1029.TwoCitySchedCost.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 1029. Two City Scheduling
2+
// A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti], the cost of flying the ith person to city a is aCosti, and the cost of flying the ith person to city b is bCosti.
3+
4+
// Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.
5+
// Example 1:
6+
// Input: costs = [[10,20],[30,200],[400,50],[30,20]]
7+
// Output: 110
8+
// Explanation:
9+
// The first person goes to city A for a cost of 10.
10+
// The second person goes to city A for a cost of 30.
11+
// The third person goes to city B for a cost of 50.
12+
// The fourth person goes to city B for a cost of 20.
13+
// The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
14+
// Example 2:
15+
// Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
16+
// Output: 1859
17+
// Example 3:
18+
// Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
19+
// Output: 3086
20+
// Constraints:
21+
// 2 * n == costs.length
22+
// 2 <= costs.length <= 100
23+
// costs.length is even.
24+
// 1 <= aCosti, bCosti <= 1000
25+
26+
public class Solution {
27+
public int TwoCitySchedCost(int[][] costs) {
28+
//Sort on the basis of how much more I will be profited if I choose city 1 insted of city 2;
29+
costs = costs.OrderBy(x => x[0]-x[1]).ToArray();
30+
int cost = 0;
31+
for(int i = 0; i < costs.Length; i++){
32+
if(i<costs.Length/2)
33+
cost += costs[i][0];
34+
else
35+
cost += costs[i][1];
36+
}
37+
return cost;
38+
}
39+
}

Diff for: 290.WordPattern.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 290. Word Pattern
2+
// Given a pattern and a string s, find if s follows the same pattern.
3+
// Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
4+
// Example 1:
5+
// Input: pattern = "abba", s = "dog cat cat dog"
6+
// Output: true
7+
// Example 2:
8+
// Input: pattern = "abba", s = "dog cat cat fish"
9+
// Output: false
10+
// Example 3:
11+
// Input: pattern = "aaaa", s = "dog cat cat dog"
12+
// Output: false
13+
// Constraints:
14+
// 1 <= pattern.length <= 300
15+
// pattern contains only lower-case English letters.
16+
// 1 <= s.length <= 3000
17+
// s contains only lowercase English letters and spaces ' '.
18+
// s does not contain any leading or trailing spaces.
19+
// All the words in s are separated by a single space.
20+
21+
public class Solution {
22+
public bool WordPattern(string pattern, string s) {
23+
Dictionary <char,string> map1 = new Dictionary<char,string>();
24+
Dictionary <string,char> map2 = new Dictionary<string,char>();
25+
string[] sArr = s.Split(" ");
26+
if(sArr.Length != pattern.Length)
27+
return false;
28+
for(int i = 0; i < pattern.Length; i++){
29+
if(!map1.ContainsKey(pattern[i]) && !map2.ContainsKey(sArr[i])){
30+
map1[pattern[i]] = sArr[i];
31+
map2[sArr[i]] = pattern[i];
32+
}
33+
else if(map1.ContainsKey(pattern[i]) && map1[pattern[i]] == sArr[i]
34+
&& map2.ContainsKey(sArr[i]) && map2[sArr[i]] == pattern[i])
35+
continue;
36+
else
37+
return false;
38+
}
39+
return true;
40+
}
41+
}

Diff for: 520.DetectCapitalUse.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 520. Detect Capital
2+
// We define the usage of capitals in a word to be right when one of the following cases holds:
3+
// All letters in this word are capitals, like "USA".
4+
// All letters in this word are not capitals, like "leetcode".
5+
// Only the first letter in this word is capital, like "Google".
6+
// Given a string word, return true if the usage of capitals in it is right.
7+
// Example 1:
8+
// Input: word = "USA"
9+
// Output: true
10+
// Example 2:
11+
// Input: word = "FlaG"
12+
// Output: false
13+
// Constraints:
14+
// 1 <= word.length <= 100
15+
// word consists of lowercase and uppercase English letters.
16+
17+
public class Solution {
18+
public bool DetectCapitalUse(string word) {
19+
bool flag = false;
20+
for(int i = 0; i < word.Length; i++){
21+
if(i == 0){
22+
if(word[i] >= 'A' && word[i] <= 'Z')
23+
flag = true;
24+
else
25+
flag = false;
26+
}
27+
else if(i == 1){
28+
if(flag && word[i] >= 'a' && word[i] <= 'z')
29+
flag = false;
30+
else if(flag && word[i] >= 'A' && word[i] <= 'Z')
31+
continue;
32+
else if(!flag && word[i] >= 'a' && word[i] <= 'z')
33+
continue;
34+
else
35+
return false;
36+
}
37+
else{
38+
if(flag && word[i] >= 'A' && word[i] <= 'Z')
39+
continue;
40+
else if(!flag && word[i] >= 'a' && word[i] <= 'z')
41+
continue;
42+
else
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
}

0 commit comments

Comments
 (0)