Skip to content

Commit ec67c7f

Browse files
committed
updated
1 parent c50e22e commit ec67c7f

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*******************************************
2+
Author : LHearen
3+
4+
Time : 2016-03-12 15:49
5+
Description : A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
6+
7+
Buildings Skyline Contour
8+
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
9+
10+
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
11+
12+
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
13+
14+
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
15+
16+
Notes:
17+
18+
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
19+
The input list is already sorted in ascending order by the left x position Li.
20+
The output list must be sorted by the x position.
21+
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]
22+
Source : https://leetcode.com/problems/the-skyline-problem/
23+
*******************************************/
24+
//https://briangordon.github.io/2014/08/the-skyline-problem.html
25+
//http://www.geeksforgeeks.org/divide-and-conquer-set-7-the-skyline-problem/
26+
int** getSkyline(int** buildings, int rSize, int cSize, int* returnSize)
27+
{
28+
29+
}

LuoSonglei/week-96/307-RangeSumQuery.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ sumRange(0, 2) -> 8
1515
The array is only modifiable by the update function.
1616
You may assume the number of calls to update and sumRange function is distributed evenly.
1717
Source : https://leetcode.com/problems/range-sum-query-mutable/
18+
Reference : http://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
1819
*******************************************/
1920
#include <stdlib.h>
2021
struct NumArray
@@ -23,13 +24,15 @@ struct NumArray
2324
int size;
2425
};
2526

27+
//partial sums are stored in numArray->sums;
28+
//index of sums are 1 more than that in nums;
2629
void updateElement(struct NumArray* numArray, int i, int val)
2730
{
2831
i++;
2932
while(i <= numArray->size)
3033
{
3134
numArray->sums[i] += val;
32-
i += (i & -i);
35+
i += (i & -i); //move to its parent i UpdateView;
3336
}
3437
}
3538

@@ -53,14 +56,16 @@ void update(struct NumArray* numArray, int i, int val)
5356
updateElement(numArray, i, d);
5457
}
5558

59+
//partial sums are stored in numArray->sums;
60+
//index of sums are 1 more than that in nums;
5661
int getSum(struct NumArray* numArray, int i)
5762
{
5863
int sum = 0;
5964
i++;
6065
while(i > 0)
6166
{
6267
sum += numArray->sums[i];
63-
i -= (i & -i);
68+
i -= (i & -i); //move to its parent in getSumView;
6469
}
6570
return sum;
6671
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************
2+
Author : LHearen
3+
4+
Time : 2016-03-09 16:38
5+
Description : Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
6+
7+
The update(i, val) function modifies nums by updating the element at index i to val.
8+
Example:
9+
Given nums = [1, 3, 5]
10+
11+
sumRange(0, 2) -> 9
12+
update(1, 2)
13+
sumRange(0, 2) -> 8
14+
Note:
15+
The array is only modifiable by the update function.
16+
You may assume the number of calls to update and sumRange function is distributed evenly.
17+
Source : https://leetcode.com/problems/range-sum-query-mutable/
18+
Reference : http://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
19+
*******************************************/
20+
#include <stdlib.h>
21+
struct NumArray
22+
{
23+
int *nums, *sums;
24+
int size;
25+
};
26+
27+
//partial sums are stored in numArray->sums;
28+
//index of sums are 1 more than that in nums;
29+
void updateElement(struct NumArray* numArray, int i, int val)
30+
{
31+
i++;
32+
while(i <= numArray->size)
33+
{
34+
numArray->sums[i] += val;
35+
i += (i & -i); //move to its parent i UpdateView;
36+
}
37+
}
38+
39+
struct NumArray* NumArrayCreate(int *nums, int size)
40+
{
41+
struct NumArray *t = (struct NumArray*)malloc(sizeof(struct NumArray));
42+
t->nums = (int*)malloc(sizeof(int)*size);
43+
memcpy(t->nums, nums, sizeof(int)*size);
44+
t->size = size;
45+
t->sums = (int*)malloc(sizeof(int)*(size+1));
46+
memset(t->sums, 0, sizeof(int)*(size+1));
47+
for(int i = 0; i <= size; i++)
48+
updateElement(t, i, nums[i]);
49+
return t;
50+
}
51+
52+
void update(struct NumArray* numArray, int i, int val)
53+
{
54+
int d = val-numArray->nums[i];
55+
numArray->nums[i] = val;
56+
updateElement(numArray, i, d);
57+
}
58+
59+
//partial sums are stored in numArray->sums;
60+
//index of sums are 1 more than that in nums;
61+
int getSum(struct NumArray* numArray, int i)
62+
{
63+
int sum = 0;
64+
i++;
65+
while(i > 0)
66+
{
67+
sum += numArray->sums[i];
68+
i -= (i & -i); //move to its parent in getSumView;
69+
}
70+
return sum;
71+
}
72+
int sumRange(struct NumArray* numArray, int i, int j)
73+
{
74+
return getSum(numArray, j)-getSum(numArray, i-1);
75+
}
76+
77+
void NumArrayFree(struct NumArray* numArray)
78+
{
79+
free(numArray->nums);
80+
free(numArray->sums);
81+
free(numArray);
82+
}

0 commit comments

Comments
 (0)