Skip to content

Commit

Permalink
solution 38 and 238
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoflynnautomation committed Apr 6, 2024
1 parent 59df324 commit d6fbdd1
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 32 deletions.
63 changes: 31 additions & 32 deletions src/LeetCode.Solutions/217_ContainsDuplicate.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
namespace LeetCode.Solutions
{
/// <summary>
/// https://leetcode.com/problems/contains-duplicate/
///
/// Given an integer array nums, return true if any value appears at least twice in the array,
/// and return false if every element is distinct.
///
/// Example 1:
/// Input: nums = [1,2,3,1]
/// Output: true
///
/// Example 2:
/// Input: nums = [1,2,3,4]
/// Output: false
namespace LeetCode.Solutions;

/// Example 3:
/// Input: nums = [1,1,1,3,3,4,3,2,4,2]
/// Output: true
/// </summary>
/// <summary>
/// https://leetcode.com/problems/contains-duplicate/
///
/// Given an integer array nums, return true if any value appears at least twice in the array,
/// and return false if every element is distinct.
///
/// Example 1:
/// Input: nums = [1,2,3,1]
/// Output: true
///
/// Example 2:
/// Input: nums = [1,2,3,4]
/// Output: false

[Level(Level.Easy)]
public class _217_ContainsDuplicate
{
public bool ContainsDuplicate(int[] nums)
{
var uniqueNumbers = new HashSet<int>();
/// Example 3:
/// Input: nums = [1,1,1,3,3,4,3,2,4,2]
/// Output: true
/// </summary>

for (var i = 0; i < nums.Length; i++)
{
if (uniqueNumbers.Contains(nums[i])) return true;
uniqueNumbers.Add(nums[i]);
}
[Level(Level.Easy)]
public class _217_ContainsDuplicate
{
public bool ContainsDuplicate(int[] nums)
{
var uniqueNumbers = new HashSet<int>();

return false;
for (var i = 0; i < nums.Length; i++)
{
if (uniqueNumbers.Contains(nums[i])) return true;
uniqueNumbers.Add(nums[i]);
}

return false;
}
}

}
44 changes: 44 additions & 0 deletions src/LeetCode.Solutions/238_ProductOfArrayExceptSelf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace LeetCode.Solutions;

/// <summary>
/// 238. Product of Array Except Self
/// https://leetcode.com/problems/product-of-array-except-self/
/// Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
///
/// </summary>
[Level(Level.Medium)]
public class _238_ProductOfArrayExceptSelf
{
[TimeComplexity("O(n)")]
[SpaceComplexity("O(1)")]
public int[] ProductExceptSelf(int[] nums)
{

int n = nums.Length;
var result = new int[n];

// initialize the result array with 1
for (var i = 0; i < n; i++)
{
result[i] = 1;
}

// calculate the prefix product
int prefixProduct = 1;
for (int i = 0; i < n; i++)
{
result[i] *= prefixProduct;
prefixProduct *= nums[i];
}

// calculate the suffix product
int suffixProduct = 1;
for (int i = n - 1; i >= 0; i--)
{
result[i] *= suffixProduct;
suffixProduct *= nums[i];
}

return result;
}
}
48 changes: 48 additions & 0 deletions src/LeetCode.Solutions/36_ValidSudoku.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace LeetCode.Solutions;

/// <summary>
/// 36. Valid Sudoku
/// https://leetcode.com/problems/valid-sudoku/
/// Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
/// Each row must contain the digits 1-9 without repetition.
/// Each column must contain the digits 1-9 without repetition.
/// Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
/// Note:
/// A Sudoku board (partially filled) could be valid but is not necessarily solvable.
/// Only the filled cells need to be validated according to the mentioned rules.
/// </summary>

[Level(Level.Medium)]

public class _36_ValidSudoku
{
public bool IsValidSudoku(char[][] board)
{
var rows = new HashSet<string>();
var columns = new HashSet<string>();
var boxes = new HashSet<string>();

for (var i = 0; i < 9; i++)
{
for (var j = 0; j < 9; j++)
{
char cell = board[i][j];
if (cell == '.')
{
continue;
}

var rowKey = $"{cell} in row {i}";
var columnKey = $"{cell} in column {j}";
var boxKey = $"{cell} in box {i / 3} - {j / 3}";

if (!rows.Add(rowKey) || !columns.Add(columnKey) || !boxes.Add(boxKey))
{
return false;
}
}
}

return true;
}
}
22 changes: 22 additions & 0 deletions src/LeetCode.Tests/238_ProductOfArrayExceptSelf_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace LeetCode.Tests;

public class _238_ProductOfArrayExceptSelf_Tests
{
[Test]
public void Case1()
{
var solution = new _238_ProductOfArrayExceptSelf();
var result = solution.ProductExceptSelf(new[] { 1, 2, 3, 4 });

result.Should().Equal(new[] { 24, 12, 8, 6 });
}

[Test]
public void Case2()
{
var solution = new _238_ProductOfArrayExceptSelf();
var result = solution.ProductExceptSelf(new[] { -1, 1, 0, -3, 3 });

result.Should().Equal(new[] { 0, 0, 9, 0, 0 });
}
}
49 changes: 49 additions & 0 deletions src/LeetCode.Tests/36_ValidSudoku_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace LeetCode.Tests;

public class _36_ValidSudoku_Tests
{
[Test]
public void Case1()
{
var board = new char[][] {
['5','3','.','.','7','.','.','.','.'],
['6','.','.','1','9','5','.','.','.'],
['.','9','8','.','.','.','.','6','.'],
['8','.','.','.','6','.','.','.','3'],
['4','.','.','8','.','3','.','.','1'],
['7','.','.','.','2','.','.','.','6'],
['.','6','.','.','.','.','2','8','.'],
['.','.','.','4','1','9','.','.','5'],
['.','.','.','.','8','.','.','7','9']

};

var solution = new _36_ValidSudoku();
var result = solution.IsValidSudoku(board);

result.Should().BeTrue();
}

[Test]
public void Case2()
{

var board = new char[][]{
['8','3','.','.','7','.','.','.','.'],
['6','.','.','1','9','5','.','.','.'],
['.','9','8','.','.','.','.','6','.'],
['8','.','.','.','6','.','.','.','3'],
['4','.','.','8','.','3','.','.','1'],
['7','.','.','.','2','.','.','.','6'],
['.','6','.','.','.','.','2','8','.'],
['.','.','.','4','1','9','.','.','5'],
['.','.','.','.','8','.','.','7','9']
};

var solution = new _36_ValidSudoku();
var result = solution.IsValidSudoku(board);

result.Should().BeFalse();

}
}

0 comments on commit d6fbdd1

Please sign in to comment.