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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Solutions in various programming languages are provided. Enjoy it.
11. [Maximum Product Subarray](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/11-Maximum-Product-Subarray)
12. [Combination Sum III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/12-Combination-Sum-III)
13. [Insert Interval](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/13-Insert-Interval)
14. [House Robber](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/14-House-Robber)
15. [Length of Last Word](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/15-Length-of-Last-Word)
16. [Maximum XOR of Two Numbers in an Array](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/16-Maximum-XOR-of-Two-Numbers-in-an-Array)
17. [Robot Bounded In Circle](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/17-Robot-Bounded-In-Circle)

## August LeetCoding Challenge
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Expand Down
12 changes: 12 additions & 0 deletions September-LeetCoding-Challenge/14-House-Robber/House-Robber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Solution {
public int Rob(int[] nums) {
if(nums.Length == 0) return 0;

var dp = new int[nums.Length+1];
dp[1] = nums[0];
for(int i=1; i<nums.Length; i++){
dp[i+1] = Math.Max(dp[i], dp[i-1] + nums[i]);
}
return dp[nums.Length];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
public class Solution {

//Solution 1
public class Solution {
public int LengthOfLastWord(string s) {
if(s.Length == 0) return 0;

var l = s.Split(' ').ToList();
var i = l.Count - 1;
while(i >= 0){
if(l[i] == ""){
i--;
}
else{
return l[i].Length;
}
}

return 0;
}
}


//Solution 2
public int LengthOfLastWord(string s)
{
if (s.Length == 0) return 0;

int beginPosition = 0;
int endPosition = 0;
bool hasWord = false;
bool hasSpaceBeforeLastWord = false;

//Start by the end of string
for (int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == ' ' && !hasWord)
{
//This condition improves performance
continue;
}
else if (s[i] != ' ' && !hasWord)
{
hasWord = true;
endPosition = i;
continue;
}
else if (s[i] == ' ' && hasWord)
{
beginPosition = i;
hasSpaceBeforeLastWord = true;
break;
}
}

//One word case
if (endPosition - beginPosition == 0 && hasWord)
return 1;

//No words case
if (endPosition - beginPosition == 0 && !hasWord)
return 0;

//Has word and has spaces before last words
if (endPosition - beginPosition != 0 && hasWord && hasSpaceBeforeLastWord)
return endPosition - beginPosition;

//Has word and has no space before last words
if (endPosition - beginPosition != 0 && hasWord && !hasSpaceBeforeLastWord)
return endPosition - beginPosition + 1;

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
public int FindMaximumXOR(int[] nums) {
int max = 0, mask=0;
for(int i=31; i>=0; i--){
mask |= (1 << i);

var hs = new HashSet<int>();
foreach(int num in nums){
hs.Add(num & mask);
}

int tmp = max | (1 << i);
foreach(int prefix in hs){
if(hs.Contains(tmp ^ prefix)){
max = tmp;
break;
}
}
}
return max;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Solution {
public bool IsRobotBounded(string instructions) {
int x = 0;
int y = 0;
var dirs = new int[4][]{new int[]{0,1}, new int[]{1, 0}, new int[]{0,-1}, new int[]{-1,0}};
int d = 0;

foreach(char c in instructions){
if(c == 'L'){
d = (d+3)%4;
}else if(c == 'R'){
d = (d+1)%4;
}else{
x += dirs[d][0];
y += dirs[d][1];
}
}

return x == 0 && y == 0 || d > 0;
}
}