Skip to content

Commit

Permalink
Add lc0204
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Feb 15, 2024
1 parent 0f5e08a commit 36bccec
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`).
| 69 | [Sqrt(x)](src/lc0069.c) | Mathematics | Square root | `sqrt` |
| 75 | [Sort Colors](src/lc0075.c) | Sorting | Sort | `qsort` |
| 77 | [Combinations](src/lc0077.c) | Backtracking | Matrix | `CombinationIterator` |
| 204 | [Count Primes](src/lc0204.c) | Mathematics | Count | `Sieve`, `binary_search_rank` |
| 273 | [Integer to English Words](src/lc0273.c) | Mathematics | String | `words_to_string` |
| 367 | [Valid Perfect Square](src/lc0367.c) | Mathematics | Boolean | `math_is_polygonal` |
| 744 | [Find Smallest Letter Greater Than Target](src/lc0744.c) | Binary search | Character | `binary_search_min` |
Expand Down
23 changes: 21 additions & 2 deletions lib/binary_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Searches a sorted collection for the least element greater than or equal to
* a given value. This function uses the binary search algorithm.
*
* @param item the required minimum value.
* @param min the required minimum value.
* @param items the sorted collection of items.
* @param length the number of items.
* @param itemSize the size of each item.
Expand All @@ -19,7 +19,26 @@
* given minimum.
*/
Object binary_search_min(
Object item,
Object min,
Array items,
size_t length,
size_t itemSize,
Comparer comparer);

/**
* Determines the number of elements less than a given value in a sorted
* collection. This function uses the binary search algorithm.
*
* @param max the required maximum value.
* @param items the sorted collection of items.
* @param length the number of items.
* @param itemSize the size of each item.
* @param comparer the item comparer. The `items` argument must be sorted using
* the same comparer.
* @return The number of items less than the given maximum.
*/
size_t binary_search_rank(
Object max,
Array items,
size_t length,
size_t itemSize,
Expand Down
2 changes: 1 addition & 1 deletion lib/combination_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void combination_begin(
iterator->n = n;
iterator->end = false;

for (size_t i = 0; i < iterator->k; i++)
for (int i = 0; i < iterator->k; i++)
{
iterator->subset[i] = 0;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ bool list_contains(List instance, Object item, EqualityComparer itemComparer)
return false;
}

void list_clear(List instance)
{
instance->count = 0;
}

void list_reverse(List instance)
{
if (!instance->count)
Expand Down Expand Up @@ -148,12 +143,17 @@ bool list_sequence_equal(List left, List right, EqualityComparer itemComparer)
return true;
}

void list_clear(List instance)
{
instance->count = 0;
}

void finalize_list(List instance)
{
list_clear(instance);
free(instance->items);

instance->items = NULL;
instance->itemSize = 0;
instance->count = 0;
instance->capacity = 0;
}
14 changes: 7 additions & 7 deletions lib/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ Exception list_ensure_capacity(List instance, size_t capacity);
*/
bool list_contains(List instance, Object item, EqualityComparer itemComparer);

/**
* Removes all elements from the list.
*
* @param instance the `List` instance.
*/
void list_clear(List instance);

/**
* Reverses the order of the elements in the list.
*
Expand All @@ -88,6 +81,13 @@ void list_reverse(List instance);
*/
bool list_sequence_equal(List left, List right, EqualityComparer itemComparer);

/**
* Removes all elements from the list.
*
* @param instance the `List` instance.
*/
void list_clear(List instance);

/**
* Frees all resources.
*
Expand Down
7 changes: 6 additions & 1 deletion lib/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,21 @@ bool priority_queue_try_dequeue(
return true;
}

void priority_queue_clear(PriorityQueue instance)
{
instance->count = 0;
}

void finalize_priority_queue(PriorityQueue instance)
{
priority_queue_clear(instance);
free(instance->items);
free(instance->priorities);

instance->items = NULL;
instance->itemSize = 0;
instance->priorities = NULL;
instance->prioritySize = 0;
instance->count = 0;
instance->capacity = 0;
instance->priorityComparer = NULL;
}
8 changes: 8 additions & 0 deletions lib/priority_queue.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed under the MIT License.

#include <stddef.h>
#include "comparer.h"
#include "exception.h"
#include "object.h"
Expand Down Expand Up @@ -90,6 +91,13 @@ bool priority_queue_try_dequeue(
Object item,
Object priority);

/**
* Removes all elements from the priority queue.
*
* @param instance the `PriorityQueue` instance.
*/
void priority_queue_clear(PriorityQueue instance);

/**
* Frees all resources.
*
Expand Down
2 changes: 1 addition & 1 deletion src/lc0023.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
return head.next;
}

void finalize()
void finalizeMergeKLists()
{
finalize_priority_queue(&priorityQueue);
}
39 changes: 39 additions & 0 deletions src/lc0204.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed under the MIT License.

// Count Primes

#include "../lib/binary_search.h"
#include "../lib/euler.h"
#include "../lib/sieve.h"

bool lazy;
struct Sieve primes;

int countPrimes(int n)
{
if (n <= 2)
{
return 0;
}

if (!lazy)
{
euler_ok(sieve(&primes, 5000000));

lazy = true;
}

long long m = n;

return binary_search_rank(
&m,
primes.primes.items,
primes.primes.count,
sizeof(long long),
long_long_comparer);
}

void finalizeCountPrimes()
{
finalize_sieve(&primes);
}
4 changes: 2 additions & 2 deletions tools/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ Get-Content '../data/id0081.txt' | ./../id0081.exe
Get-Content '../data/id0081.txt' | ./../id0082.exe
Get-Content '../data/id0081.txt' | ./../id0083.exe

for ($i = 84; $i -le 88; $i++)
for ($i = 84; $i -le 87; $i++)
{
Invoke-Expression "./../id00${i}.exe"
}

./../id0089.exe

for ($i = 91; $i -le 92; $i++)
for ($i = 91; $i -le 93; $i++)
{
Invoke-Expression "./../id00${i}.exe"
}
4 changes: 2 additions & 2 deletions tools/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ cat ../data/id0081.txt | $run ./../id0081
cat ../data/id0081.txt | $run ./../id0082
cat ../data/id0081.txt | $run ./../id0083

for i in {0084..0088};
for i in {0084..0087};
do
$run ./../id${i}
done

$run ./../id0089

for i in {0091..0092};
for i in {0091..0093};
do
$run ./../id${i}
done

0 comments on commit 36bccec

Please sign in to comment.